Object Oriented Programming
using C++
Topic: C++ Basics
Mr. B. Srinu
Contents
Operators
Expressions
Type Conversions
Control Statements
Operators
Type of Operation to be carried out
Types
Unary , Binary , Ternary
Operators in C++ are:
Arithmetic Operators
Relational Operators
Bit-wise Operators
Conditional operator (?)
Assignment Operator
& , * operators
Arithmetic Operators
int i=10, j=20;
k=i++ + ++j => ?
k=--i + j-- => ?
K= i++ + --i + ++j + --i => ? ( i,j,k)
Relational Operators
int i=20 , j=6
bool x=i>j => true
bool x=i<j => false
bool x=(i==j) => false
bool x=(i!=j) => true
Logical Operators
bool x=true , y=false
z= x && y => false
z= x || y => true
z= !x => false
Bitwise Operators
int a=2 , b=4
c=a&b =>0
c=a|b =>6
c=a^b =>6
c=~a =>-3
c=a<<1 =>4
c=a>>1 => 1
Conditional Operators
Ternary Operator
Alternate to if-then-else form
Exp1 ? Exp2 : Exp3;
int x = 10;
int y = x>9 ? 100 : 200;
y is assigned the value 100.
Other Operators
Assignment Operator:
variable_name = expression;
sizeof operator:
double f;
sizeof f => 8
sizeof(int)) => 4
Other Operators
The Dot (.) and Arrow ( ->)
struct employee
{
char name[80];
int age;
float wage;
} e1;
e1.age=20;
struct employee *p = &e1;
p->age=22;
Other Operators
The & and * Pointer Operators:
int a=10; Adress a =>100
int *p = &a; Address p => 200
int **q= &p; Address q=> 300
&a => 100 , *p => 10
&p => ? *q => ? q => ?
**q => ? *(&q) => ?
Expressions –
Operator Precedence
Type Conversions - Implicit
bool -> char -> short int -> int ->
unsigned int -> long -> unsigned -> long
long -> float -> double ->
long double
Type Conversions - Cast
Force an expression to be of a
specific type by using a cast
(type) expression
double x = 1.2;
int sum = (int)x + 1;
Flow Control Statements
Selection Statements
if , if-else , if-else-if
switch
Iterative Statements or Loops
while
do-while
for
Unconditional Jump Statements
break
continue
return
goto
Selection Statements
Simple if:
if (expression) statement;
if-else:
if (expression)
statement;
else
statement;
if-else-if:
if (expression) statement;
else
if (expression) statement;
else if (expression) statement;
Selection Statements
switch (expression) {
case constant1:statement sequence
break;
case constant2:statement sequence
break;
case constant3:statement sequence
break;
.
.
.
default:statement sequence
}
Selection Statements
int a=50;
switch(a)
{
default: a=45;
case 49: a++;
case 50: a--;
case 51: a =a+1;
}
cout<<a;
Output -> ?
Iterative Statements
for(initialization; condition;
increment)
statement;
while(condition) statement;
do {
statement;
} while(condition);
Iterative Statements
int i = 1, j = 1;
for(--i && j++ ; i<10; i+=2)
{
cout<<"loop ";
}
o/p -> ?
for( ; ; ;)
{
cout<<”A”;
}
O/ p -> ?
Summary
Operators
Expressions
Type Conversions