Professional Documents
Culture Documents
Contents of Week 02
Data types, Constants Floating Point Constants, Size, Memory Concepts Names, Keywords, Identifiers Declaration and Definition of Variables A Simple Program: Printing a Line of Text Another Simple Program: Adding Two Integers
std::cout << "Enter another number:"; std::cin >> number2; sum = number2 + number3; std::cout << "Sum is: " << sum <<std::endl; } return 0; stream manipulator Concatenating insertion operators
4
Comments
A comment is descriptive text used to help a reader of the program understand its content. Explain programs to other programmers
Improve program readability
Multi-line comment
Start with /* End with */ Also called comment delimiter
Preprocessor Directives
Preprocessor directives
Processed by preprocessor before compiling Begin with # Example
#include <iostream>
Tells preprocessor to include the input/output stream header file <iostream>
White spaces
Blank lines, space characters and tabs Delimiter, used to make programs easier to read Extra spaces are ignored by the compiler
7
Function main
A part of every C++ program
Exactly one function in a program must be main main is a Keyword.
Keyword : A word in code that is reserved by C++ for a specific use.
Statements
Instruct the program to perform an action All statements end with a semicolon (;) Examples :
return 0; std::cout << Welcome to C++!\n ;
return Statement
Because function main() returns an integer value, there must be a statement that indicates what this value is. The statement return 0 ;
indicates that main() returns a value of zero to the operating system. The value 0 indicates the program terminated successfully greater than 0 means an error maybe a different number for a different error if declare our function as int and don't use return will get a warning from the compiler
Namespaces
Namespace: a generalization of scope. C++ allows access to multiple namespaces with the ' :: ' operator A namespace lets us distinguish between two names from different places In C++ the standard library is in the std namespace Things in no namespace are said to be in the Global Namespace
using std::cout;
Copy cout from the std namespace into the global namespace
Stream insertion operator << Value to right (right operand) inserted into left operand.
The string of characters contained between after the operator << shows on computer screen.
14
\t
Horizontal tab. Move the screen cursor to the next tab stop.
15
17
A Simple Program
1
2 3 4 5 6 7 8 9 10 11 12
// Fig. 1.2: fig01_02.cpp // A first program in C++. #include <iostream> // Preprocessor Directive // function main begins program execution int main() { std::cout << "Welcome to C++!\n"; return 0; // indicate that program ended successfully
Welcome to C++!
\a \\ \"
18
19
2 3 4 5 6 7 8 9 10 11 12 13
// Fig. 1.4: fig01_04.cpp // Printing a line with multiple statements. #include <iostream> // function main begins program execution int main() { std::cout << "Welcome "; std::cout << "to C++!\n"; return 0; // indicate that program ended successfully
Welcome to C++!
20
Example
int number1; std::cin >> number1;
Reads an integer typed at the keyboard Stores the integer in variable number1
21
22
std::cout << "Enter second integer\n"; // prompt std::cin >> integer2; // read an integer sum = integer1 + integer2; // assign result to sum
std::cout << "Sum is " << sum << std::endl; // print sum return 0; // indicate that program ended successfully
Memory Concepts
Variable names
Correspond to actual locations in computer's memory
Every variable has name, type, size and value
23
24
Fig. 2| Memory locations after storing values for number1 and number2.
25
Fig. 3 | Memory locations after calculating and storing the sum of number1 and number2.
26
age.cpp
#include <iostream> using namespace std; int main() { int age=26; cout << "I am " << age << " years old" << endl; age++; /* add one */ cout << "Next year I will be " << age << endl; return 0; }
Statements
Instructions Finish with a ;
(semicolon)
#include <iostream> using namespace std;
int main() { int age=26; cout << "I am " << age << " years old" << endl; age++; /* add one */ cout << "Next year I will be " << age << endl; return 0; }
Statement Block
List of instructions Everything between { and }
(curly brackets, braces)
#include <iostream> using namespace std;
int main() { int age=26; cout << "I am " << age << " years old" << endl; age++; /* add one */ cout << "Next year I will be " << age << endl; return 0; }
Functions
One of the building block of the C++ program.
#include <iostream> using namespace std; int main() { int age=26; cout << "I am " << age << " years old" << endl; age++; /* add one */ cout << "Next year I will be " << age << endl; return 0; }
Data Types
int, float, void, unsigned int, long, double, char string, list,
int main() { int age=26; cout << "I am " << age << " years old" << endl; age++; /* add one */ cout << "Next year I will be " << age << endl; return 0; }
void datatype
Special data type to represent nothing or an unknown type of data int main(void)
main function expects nothing Exactly the same as int main()
void main()
main function returns nothing In C++ main() should always return an int
Keywords
Sometimes called reserved words. Are defined as a part of the C++ language. Reserved for specific purpose Have special meaning and is known by compiler Can not be used for anything else! Examples:
char, int, float, void, signed, if, while, case, else class, public, friend, this, operator, new, true
Keywords
#include <iostream> using namespace std; int main() { int age=26; cout << "I am " << age << " years old" << endl; age++; /* add one */ cout << "Next year I will be " << age << endl; return 0; }
Keywords
Cannot be used as identifiers or variable names
C++ Keyw o rd s
Keywords common to the C and C++ programming languages auto continue enum if short switch volatile C++ only keywords asm delete inline private static_cast try wchar_t break default extern int signed typedef while bool dynamic_cast mutable protected template typeid case do float long sizeof union char double for register static unsigned const else goto return struct void
39
Constants
Constant is a fixed value that does not change during the execution of the program. Divided into two types
Numeric Constants Non-Numeric Constants
Numeric Constants
Numbers are referred to as Numeric Constants Consists of
Numeric digits 0-9 Plus (+) or Minus (-) sign If no sign by-default it is assumed to be positive Decimal point No commas or blanks are allowed 30 -500 3.14159 0.25432 +100
Examples
Numeric Constants
Represented in three ways
Integer constant Floating-point constant Exponential real constant
Integer Constant
The numeric constant that doesnt contain a decimal point Can be positive or negative Examples
70 +134 0 -500 7
Floating-Point Constants
The numeric constants that does contain the decimal point Can be either positive or negative Examples
0.3 -56.34 0.008976
Non-Numeric Constants
Used for non-numeric purposes To produce output reports, headings, or printing messages Divided into two types
Character Constants String Constants
Character Constants
Singular! One character defined character set. Surrounded on the single quotation mark. All the alphabetic, numeric and special characters can be character constants except backslash and the single quotation mark. Examples: \\ for backslash A \ for single quote a $
String Constants
A sequence characters surrounded by double quotation marks. Considered a single item. Examples:
UMBC I like ice cream. 123 CAR car
Names
Sometimes called identifiers. Words that are not reserved. User defined words or the programmer supplied names Can be of any length, but on the first 31 are significant (too long is as bad as too short). Are case sensitive:
Must begin with a letter and the rest can be letters, digits, and underscores.
Identifiers
Variable names and object names
age, height, i, j, x, y, cout
int main() { int age=26; cout << "I am " << age << " years old" << endl; age++; /* add one */ cout << "Next year I will be " << age << endl; return 0; }
Variables
Specific storage location in memory where value can be stored A value, which may vary during program execution. A variable is the name used to represent a piece of information.
Declaration statements
int age; string user_name; float height, weight;
int age=26; string user_name="Matt"; float height=1.75, weight=122.5;
Algorithms
Computing problems
Solved by executing a series of actions in a specific order
Program control
Specifies the order in which statements are executed
54
Pseudocode
Pseudocode
Artificial, informal language used to develop algorithms Similar to everyday English