HISTORY

A SAMPLE OF C++ PROGRAM

The C++ program shown in Example 1 is stored in a file named hello.cpp.

#include<iostream>
using namespace std;

//This program displays a "Hell Word" message
int main( )
{         
cout<<"Hello Word"<<endl;
return 0;                        

                        }

                                Analyze the program                                                                     

DATA TYPES

Figure 1.1 lists the four basic C++ data types.

Figure 1.1:Basic C++ Data types
Data type Size (bytes)
char 1
int 2 or 4
float 4
double 8

The other data types you can use in C++ are long, short, or unsigned, as illustrated in Figure 1.2. You use the qualifier unsigned with data types char or int. You can store only positive values in an unsigned char or unsigned int. You use the qualifier long with data types integer and double. You use the qualifier short with data type int.

Figure 1.2 lists other C++ data types.

Figure 1.2 C++ Qualifiers
Data Type Size(bytes)
unsigned char 1
unsigned int 2
unsigned long int 4
short int 2
long int 4
long double 10

VARIABLES

DECLARING A VARIABLE

INPUT/OUTPUT

Figure 1.3: C++ Manipulators
Manipulator Description Example
endl Cause a newline to be inserted into the output streams (move a cursor key to a new line) cout<<"hello word"<<endl;
setw( ) Allows you to specify a field width to be occupied by a value. int num=8;
cout<<setw(8)<<num;
setprecision( ) Sets a number of decmial places of a float or double number. double y=1.23456;
cout<<setiosflags(ios::fixed)<<setprecision(2)<<y<<endl;
the output will be    1.23

ARITHMETIC OPERATORS

Figure 1.4 lists some common C++ operators and their functions.

Figure 1.4 C++ Operators
Operator Name Operator Symbol Example Comment
Addition + num1 + num2
Subtraction - num1 - num2
Multiplicaton * num1*num2
Division / 15/3
15.0/2.0
Integer division; result is 7; fraction is truncated
Floating-point division; result is 7.500000
Modulus % 5%2 Performs the division and finds the remainder; the result is 1
Unary minus - -(num1) if value of num1 is 10, then -(num1) is -10
Assignment = num1=10; assigns 10 to variable num1
+= num += 10; Equivalent to num = num +10;
-= num -= 10; Equivalent to num = num -10;
*= num *= 10; Equivalent to num = num *10;
/= num /= 10; Equivalent to num = num /10;
%= num %= 10; Equivalent to num = num %10;
Increment ++ num++ or ++num Increment num to 1
Decrement -- num-- or --num Decrement num to 1

Precedence prefers to the order in which the compiler will execute the operations. Operators have different precedences associated with them. Operators also have associativity, which refers to the order in which the compiler will execute operations that have the same precedence. For example, the expression num1 + num2 - num3 includes two operators that have the same precedence--the addition and the subtraction operator. The two operator have left to right associativity, so the computer will begin with the leftmost operator and then move to the right. In this case, the subtraction operator will follow the addition operator. Figure 1.5 shows the precedence and associativity of the operators discussed in this lecture.

Figure 1.5 Order of Precedence
Operator Symbol(s) Order of Precedence Associativity
( ) First Left to Right
- Second Right to Left
* / % Third Left to Right
+ - Fourth Left to Right
=  += -= *= /=  %= Fifth Right to Left