# C ++ Programming

## C++ Expressions and Operators

##### Reading and Writing Data

- Cout is a predefined object and represents the
**standard output stream**and this output stream represents the screen. - E.g. cout<<“ I love india”;

cout will display this string as such on screen.

- << is called
**insertion**or**put to**operator. - It is also called bit-wise left -shift operator
- if string is variable then cout can be used to display the contents of string.

E.g. cout<< string;

- cin is used to read the data.
- cin>>a;
**>>**is called**extraction**operator.

##### Expressions

- It is a sequence of operators and operands that reduces to single value.

e.g. 2*5 is a expression consisting of operands and operator.

##### Types of Expressions

**Primary-**identifier, constant, Parenthetical Expression.**Postfix-**postfix increment and decrement.**Unary-**prefix increment and decrement, sizeof, unary plus and unary minus.**Binary-**multiply, divide, modulus, add, sub.**Assignment-**=+, -=, *=.

##### Primary Expression

- It has only one operand with no operator.
- Operand can be name, constant or parenthetical expression.

##### Binary Expression

- Operand-operator-operand
- Multiply , divide, modulus operators have highest priority among binary expression.

##### Assignment Expression

- The value of the total expression is the value of the expression on the right of the (=) operator.
- The result places the expression value in the operand on the left of the assignment operator.

e.g. b=5

**Simple Assignment-**e.g. b=x+1, i=i+1.**Compound Assignment-**e.g. x+=y: x=x+y

x*=y: x=x*y

##### Postfix Expression

- Consist of one operand which must be primary expression followed by one operator.

e.g. b++

- 2 types of operators:

i) postfix increment ++

ii) postfix decrement —

Expression |
Value of ‘a’ before |
Value of expression |
Value of ‘a’ after |

a++ | 10 | 10 | 11 |

a- – | 10 | 10 | 9 |

- In postfix increment variable increased by1 and in decrement decreased by 1.

##### Prefix Expression

- Consist of one operand which must be primary expression followed by one operator.

e.g. ++b

- 2 types of operators:

i) prefix increment

ii) prefix decrement

- In prefix increment variable increased by1 and in decrement decreased by 1.

Expression |
Value of ‘a’ before |
Value of ‘a’ after |
Value of ‘expression |

++a | 10 | 11 | 11 |

–a | 10 | 9 | 9 |

Int a = ++x;

you have told the compiler to increment x (making it 6) and then fetch that value and assign it to a. Thus, a is now 6 and x is now 6.

- If, after doing this, you write
- Int b = x++;

you have now told the compiler to fetch the value in x (6) and assign it to b, and then go back and increment x. Thus, b is now 6, but x is now 7.

##### Example

#include <iostream.h> void main() { int myAge = 39; // initialize two integers int yourAge = 39; cout << "I am: " << myAge << " years old.\n"; cout << "You are: " << yourAge << " years old\n"; myAge++; // postfix increment ++yourAge; // prefix increment cout << "One year passes...\n"; cout << "I am: " << myAge << " years old.\n"; cout << "You are: " << yourAge << " years old\n"; cout << "Another year passes\n"; cout << "I am: " << myAge++ << " years old.\n"; cout << "You are: " << ++yourAge << " years old\n"; cout << "Let's print it again.\n"; cout << "I am: " << myAge << " years old.\n"; cout << "You are: " << yourAge << " years old\n"; }

##### Output:

I am 39 years old You are 39 years old One year passes I am 40 years old You are 40 years old Another year passes I am 40 years old You are 41 years old Let’s print it again I am 41 years old You are 42 years old |

##### OPERATORS in C++

##### Arithmetic Operators

+ (Addition), – (subtraction), * (Multiplication), / (Division), % (Modulus)

##### Assignment Operators

=, +=, -=, *=, /=, !=,/=

##### Relational and Logical Operators

< (less than), > (greater than), <= (less than or equal to), >= (greater than or equal to)

**Equality Operator:- **== (equal to), != (not equal to)

**Logical operators:-** && (logical AND), || (logical OR), ! (NOT)

##### Bitwise Logical Operators

Eg:- bitwise compliment

Left shift

Right shift

Bitwise AND (&)

Bitwise OR (|)

Bitwise Exclusive OR (^)

##### Ternary Operator (?: )

- exp1 ? Exp2 : exp3
- Eg:- x=10;

Y=x>9?100:200

##### sizeof Operator

- This operator tells about the size in byte of whatever type specified.

e.g. sizeof (int).

- In case of pc size of int 2 bytes, in mainframes size is 4 bytes and in case of supercomputer size is 16 bytes.
- X=sizeof (int) saves the value in integer type.

##### Precedence and Associativity

- Precedence determines the order in which diff. operators in the complex expression are evaluated.
- Multiplication and division are performed before addition and subtraction as they have high priority.

e.g. 3+4*5 : (3+ (4*5)).

eg – 5*4/2+3/3+8-2 = ANS :- 17

##### Associativity

- Associativity determines the order in which operators with same precedence are evaluated in complex expression.
- Left Associativity: evaluated expression from left to right.
- Right Associativity: evaluates expression from right to left.

Operators category |
Operators |
Asssociativity |

Unary operators | -, ++, — , size of() | R TO L |

Arithmetic | *, /, % | L TO R |

ADD and SUB | +, – | L TO R |

RELATIONAL OPERATORS | <, <=,>, >= | L TO R |

EQUALITY OPERATOR | ==, != | L TO R |

LOGICAL AND | && | L TO R |

LOGICAL OR | || | R TO L |

CONDITIONAL OPERATOR | ?: | R TO L |

ASSIGNMENT OPERATORS | =, += etc. | R TO L |

COMMA | , | L TO R |

C++ is a general-purpose programming language. It has imperative, object-oriented and generic programming features, while also providing facilities for low-level memory manipulation.

C++ was developed by Bjarne Stroustrup at Bell Labs since 1979, as an extension of the C language as he wanted an efficient and flexible language similar to C, which also provided high-level features for program organization.

It was designed with a bias toward system programming and embedded, resource-constrained and large systems, with performance, efficiency and flexibility of use as its design highlights. C++ has also been found useful in many other contexts, with key strengths being software infrastructure and resource-constrained applications, including desktop applications, servers (e.g. e-commerce, web search or SQL servers), and performance-critical applications (e.g. telephone switches or space probes).C++ is a compiled language, with implementations of it available on many platforms and provided by various organizations, including the Free Software Foundation (FSF’s GCC), LLVM, Microsoft, Intel and IBM.

### Course Features

- Lectures 19
- Quizzes 0
- Duration 15 hours
- Skill level All levels
- Language English
- Students 20
- Certificate No
- Assessments Self