SPT U3 PPT
SPT U3 PPT
Semester : 4rd
C++
Introduction
C++ C
Procedure Oriented Programming (POP)
• Emphasis is on algorithm
• Large programs are divided into a no of procedures or
functions.
Global Data
Main Program
F1 F2 F3 F1 F2 F3
Local Local Local
F4 data data data
Data Data
Function Function
Data
Function
length 15
Data
Data
elements breadth 10 values
width 5
open()
Functions
close()
A Box object
Class
• Class is a collection of some properties, behavior
• BOX class:
Properties:- length , breadth, width. ( data elements)
Behavior:- open, close. ( functions )
length Data
breadth elements
width
open()
Functions
close()
A Box class
• Class is a logical structure or prototype where as Object
has physical existence.
• Object is an instance of a Class.
• Class is a collection of similar types of objects where the
data values may not be the same.
Definition of OOP
OOP is an approach which provides a way to make
modularized program by allocating separate memory
locations for both data and functions which acts as
prototype for creating some more object as per demand.
Insertion and Extraction operators
cout << “NITR”;
cout is an object of class ostream.
<< is called the insertion or put-to operator.
The value present right to << operator would be put into the
object cout which is connected to VDU to display it.
int a;
cin >> a;
cin is an object of class istream.
>> is called the extraction or get-from operator.
Extract the value stored in cin object got from keyboard and
assign it to the integer variable a;
A Simple Program
#include<iostream>
using namespace std; // namespace defines a scope
for
int main(){ // global identifiers.
int a,b,sum;
cout<<“Enter the value of a and b \n”;
cin>>a;
cin>>b; // cin>>a>>b;
sum=a+b;
cout<<“The addition result is: “<<sum;
return 0;
}
Executing the program in linux
environment
• Write the program in VI or VIM editor.
• Save the file with .cpp extension.
• Compile the file in the $ prompt with the command g++. e.g.
g++ add.cpp
• After the successful compilation, to see the output by ./a.out
Reference Variable
a a
int a=5; 5 int a=5; 5
110 110
b
a, b
int b=a; 5 int &b=a;
220
5
b 110
15 a, b
b=b+10; 220 b=b+10; 15
cout<<a; // 5 cout<<a// 15 110
// 15
cout<<b; cout<<b;// 15
p q
10 20
100 200 a b
10 20
p q
110 220
15 25
100 200
Pass by Address or Pointer
void add(int *p, int *q){ int main(){
cout<<(*p+*q); // 30 int a=10,b=20;
*p=*p+5; add(&a,&b);
*q=*q+5; cout<<a; // 15
} cout<<b; // 25
return 0;
p q }
110 220
100 200
a b
10 20
110 220
a b
15 25
110 220
Pass by Reference
void add(int &p, int &q){ int main(){
cout<<(p+q); // 30 int a=10,b=20;
p=p+5; add(a,b);
q=q+5; cout<<a; // 15
} cout<<b; // 25
return 0;
a b }
10 20
110 220
p b q
a
10 20
110 220
a p b q
15 25
110 220
Returning values from Function
Return by Value
int max(int p, int q){ int main(){
if(p > q) int a=50,b=20,c;
return p; c=max(a,b);
else cout<<c; // 50
return q; }
}
Return by Address or Pointer
int * max(int *p, int *q){ int main(){
if(*p > *q) int a=50,b=20;
return p; int *c;
else c=max(&a,&b);
return q; cout<< *c; // 50
} }
Return by Reference
int & max(int p, int q){ int main(){
if(p > q) int a=50,b=20,c;
return p; c=max(a,b);
else cout<< c; // 50
return q; }
}
Default Argument in Function
int main(){
void add(int a,int b){
cout<<a+b; add(5,7); // 12
} add(2); // error-insufficient no of arg
}
int main(){
void add(int a,int b=9){ add(5,7); // 12
cout<<a+b; add(2); // 11
} }
Default argument:
• Always assigned from right to left of the arg list
• Default value is overridden if new value is passed
Inline Function
• If the overhead time to call a function is greater than the
execution time of the function code then the function should
be made as inline and the function calls are replaced by the
function code.
int main(){
void disp(int);
// no inline keyword in the prototype
disp(5); //replaced by cout<<5;
disp(10); //replaced by cout<<10;
disp(15); //replaced by cout<<15;
return 0;
}
cout<<"double:"<<a<<endl; 29
cout<<"float:"<<b;
}
Why Does Function overloading does
not depend on the return type ???
int f( )
By seeing the function call it is not easy to
{ make out which version of the f ( ) should get
return 9; called.
}
void f( )
{
}
int main( )
{
f( ); 30s
}
OOP Features
1. Abstraction: It is a process of highlighting the important
features of a class or an object without going into much
detail.
2. Encapsulation: It is a process of putting data and functions
into a single unit.
3. Polymorphism: means “same name multiple forms”
e.g. area of a triangle.
area of a rectangle.
4. Inheritance: The process of acquiring the properties of one
class by another class is called inheritance.
Parent Child