Download as pdf or txt
Download as pdf or txt
You are on page 1of 81

Subject:- Object Oriented Programming (210245)

By:-Rathod S.B.
C++ Programming
 It includes
 Structure

 Data types
 Variable

 Operators

 loop types

 Pointers
C++ Structure
Program Structure

 Save program with hello.cpp


Compiling and running
 The compilation can be done with
g++ filename.cpp //
 Running executable in unix/linux
./a.out
 If you want to create your own executable then
g++ -o “executable name” filename.cpp
Semicolons & Blocks in C++
 The semicolon is a statement terminator
 Each statement need to be ended with semicolon.
Block
 It is a set of logically connected statements that are
surrounded by opening and closing braces.
Identifier
 A name used to identify variable, function, class or
user defined members.
 The special characters like @,$,% can’t be used as
identifier.

 Note:-c++ is a case sensitive language


 Example:-
 abs

 _abc

 abc_pqr

 Abc10

 A_12
Reserved Keywords
Whitespace in C++
 A line can have many whitespaces in between
variable or lines.
Comments
 These are the statements that a programmer can
include in a program.
 Characters used in comments ignored by compiler
 Comment Types
 Single Line Statement
 Multiline Statement
 Single Line Comment
 Multi Line Comment
Data Types
 Primitive Built-in Type
 typedef declaration
 Enumerated Type
Data Types
 Primitive Built-in Type
Primitive Built-in Type range
char 1 byte -127 to 127 or 0 to 255
unsigned char 1 byte O to 255
signed char 1 byte -127 to 127
int 4 byte -2147483648 to 2147483647
signed int 4 byte -2147483648 to 2147483647
unsigned int 4 byte 0 to 4294967295
short int 2 byte -32768 to 32767
float 4bytes +/- 3.4e +/- 38 (~7 digits)
double 8bytes +/- 1.7e +/- 308 (~15 digits)
long double 8bytes +/- 1.7e +/- 308 (~15 digits)
w_char 2 or 4 bytes 1 wide
Example
 typedef
 Used to create new name for existing type
 Enumeration Type
 It
is a set of integer constant with legal values.
enum enum-type name{ enumeratino list} varaible_list
Example:-
enum color{red ,blue ,green} c;
 Note:-default red=0,blue=1 and green =3
 You can change sequence like red=2,then blue =3
and green=4
Variable
 A name given to memory location termed as
variable
Variable
 Variable declaration
Variable
 Variable initialization
Where to declare variable
 Local Variable
 Global Variable
 Function Parameter
Local Variable
 Variables that are declared within code block
termed as local variable.
 These variables can be accessed by statements
within code block.
 The most common code block is function
 Scope is within code block
Example Using Local Variables:
#include <iostream.h>
int main ()
{ // Local variable declaration:
int a, b;
int c;
// actual initialization
a = 10;
b = 20;
c = a + b;
cout << c;
return 0;
}
Global Variable
 Variables that are known by throughout program
and may be accessible any code block.
 The value is hold throughout the program.
 These are declared outside the code block.
Example Using Global and Local
Variables
#include <iostream>
// Global variable declaration:
int g;
int main ()
{ // Local variable declaration:
int a, b;
// actual initialization
a = 10;
b = 20;
g = a + b;
cout << g;
return 0;
}
Formal Parameters
 If a function is using argument then parameters
need to be passed as an variable
 Scope is within a function body
Example Using formal Variables
#include <iostream>
// Global variable declaration:
void add(int a, int b) //formal parameters
{
int g = a + b; // g is Local variable declaration:
cout << g;
}
int main()
{ int p=10,int q=20;
add(10,20);
return 0;
}
Reference Variable
 Reference as a second label attached to that
memory location.
 Access to the contents on a memory location is done
either by original variable name or the reference.
 Syntax:
Data-type & reference name = variable name
Output:
Example Value of
Value of
i:5
i reference : 5
Value of d : 11.7
Value of d reference : 11.7
Operators
 Assignment
 Arithmetic
 Relational
 Logical
 Bitwise
Arithmetic Operators
Operator Symbol Action Example

Addition + Adds operands x+y


Subtraction - Subs second from first x-y
Negation - Negates operand -x
Multiplication * Multiplies operands x*y
Division / Divides first by second x/y
(integer quotient)
Modulus % Remainder of divide op x%y
Assignment Operator
x=3
=is an operator
-The value of this expression is 3
Compound Assignment Operator

Operator Equivalent to:


x *= y x=x*y
y -= z + 1 y = y - (z + 1)
a /= b a=a/b
x += y / 8 x = x + (y / 8)
y %= 3 y=y%3
Relational Operators
Operator Symbol Example
Equals == x == y NOT x = y
Greater than > x > y
Less than < x< y
Greater/equals >= x >= y
Less than/equals <= x <= y
Not equal != x != y
Logical Operators
&& AND
|| OR
! NOT
Bitwise Operator
 Operator  Action
&  AND

|  OR

^  Ex-OR

~  Ones complement
 >>  Right Shift

 <<  Left shift


10000000 11000001
|00000011 & 00000001

10000011(131) 00000011(3)
01111111 ~01111100
^ 01111000 10000011
00000111
x=7 00000111 x=7
x=x<<1 00001110 x=14
x=x<<3 01110000 x=112
x=x<<2 11000000 x=192
x=x>>1 01100000 x=96
X=x>>2 00011000 x=24
Operator Precedence
Control Structure
 Selection statements
 Loops
Selection statement
 if ---else
 if else ladder
Selection statement
 if ---else if (condition)
{
statement1;
statement2
}
else
{
statement;
statement
}
if else
#include<iostream> cout<<“your choice matched”<<“\n”;
using namespace std; }else
int main() { cout<<“sorry try again”<<“\n”;
{ int no; }
cout<<“enter your choice”<<“\n”; return 0;
cin>>no }
int guess=rand();
if(guess==no)
{
The ? operator
 It is special operator used in if else to replace
certain statements of else.

Expr1?Expr2:Exp3
x=10;
y=x>9?100:200;
if else if ladder
If(expression)
{statements;
}else
if(expression)
{statements;
}else
if(expression)
{ statements;
} else { statements;}
if else ladder
cout<<“your choice matched”<<“\n”;
#include<iostream>
}else
using namespace std;
if(guess >no)
int main()
{ cout<<“guess is larger”<<“\n”;
{ int no;
}else
cout<<“enter your choice”<<“\n”;
{ cout<“sorry”<<“\n”;
cin>>no
}
int guess=rand();
return 0;
if(guess==no)
}
{
int x,y;
int main ()
{ cout<<"\nInput an integer value for x: “;
cin>> x;
cout<<"\nInput an integer value for y: ";
cin>>y;
if (x==y)
cout<<x is equal to y\n";
else if (x > y)
cout<<x is greater than y\n";
else
cout<<"x is smaller than y\n";
return 0;
Thank You
 Switch Statement:-
 It allows a variable to be tested for equality against a
list of values.
 Each value is called a case
switch(expression){
case constant-expression :
statement(s);
break; //optional
case constant-expression :
statement(s);
break; //optional
default :
statement(s);
}
int main ()
{ char grade = 'D';
switch(grade) {
case 'A' :
cout << "Excellent!" << endl;
break;
case 'C' :
cout << "Well done" << endl;
break;
default :
cout << "Invalid grade" << endl;
} return 0;
Loops
 do while
 while
 for
While Loop
 A while loop statement repeatedly executes a
target statement as long as a given condition is true.
#include <iostream> if(i%2==0)
using namespace std; {
int main() cout<<“no. . . ”<<i<<“is even”<<endl;
{ }else
int no,i=1; { cout<<“I “<<I <<“is odd”<<endl;
cout<<“Enter no “<<endl; }
cin>>no; }
while(i<no) return 0;
{ }
Do-while
For loop
n1 n2 n3
0 1 1
 Fibonacci series 1 1 2
1 2 3
0,1,1,2,3,5,8,…… 2 3 5
3 5 8
5 8 13
8 13 21
13 21 34
21 34 55
24 55 79
55 79 134
int main(){
int n1=0,n2=1,n3,i,count=10;
cout<<n1<<" “<<n2<<endl;
//printing 0 and 1
for(i=2;i<count;++i)
{ n3=n1+n2;
cout<<" "+<<n3;
n1=n2;
n2=n3;
}
 Break statement:-
 Itused to terminate loop
 When loop encounters break, the loop will immediate
terminated and program control resumes next statement
after loop block.

break;
int main ()
{ int a = 10;
do {
cout << "value of a: " << a << endl;
a = a + 1;
if( a > 15){
break;
}
}while( a < 20 );
return 0;
}
Program to check prime no
#include<iostream> break;
int main() }
{ If(flag==0)
int n,I,flag=0, cout<<“no is prime”;
cin<<“Enter number”; else
for(i=2;i<n;i++) cout<<“not prime”;
{ return 0;
if(n%i==0){ }
flag=1;
 Continue Statement:-
 Itworks somewhat like break but instead of termination
of flow control it forces next iteration to be excuted.

continue;
int main ()
{ int a = 10;
while( a < 20 ){
if( a == 15) {
a = a + 1;
continue;
}
cout << "value of a: " << a << endl;
a = a + 1;
} return 0;
}

You might also like