Presentations PPT Unit-4 OOP
Presentations PPT Unit-4 OOP
Object Oriented
Programming
Unit-4
Object and
Classes
Prof. Jinal Bhagat
Outline Weightage: 15%
Basics of Object and Class in C++
Private and Public Members
Static data and Function Members
Constructors and their types
Destructors I like C++ so much
Operator Overloading
I like
Type Conversion
Rupesh sir
Physical objects…
Unit-4 Objects and Classes A D Patel Institute of Technology 4
What is an Object? (Cont…)
Logical objects…
Bank Account
Properties (Describe)
Company Methods (Functions)
Model Start
Color Drive
Mfg. Year Park
Price On_break
Fuel Type On_lock
Mileage On_turn
Gear Type
Power Steering
Anti-Lock braking system
Objects of Class Car
Example:
class car
{ I like C++ so much
// data members and member functions
}car1; I like Rupesh sir
In above example class name is car, and car1 is object of that
class.
Class
class car
{
private: I like C++ so much Car
int price; Attributes
float I like Rupesh sirPrice
mileage; Mileage
public: Methods
void start(); Start
void drive(); Drive
};
Class
class car
{ I like C++ so much Object
private:
int price; I like Rupeshintsirmain()
float {
mileage; car c1;
public: c1.start();
void start(); }
void drive();
};
Unit-4 Objects and Classes A D Patel Institute of Technology 14
Object in C++
An object is an instance of a class
An object is a variable of type class
Class Object
class car
{
I like C++ so much
int main()
private:
int price;
I like Rupesh{ sir
car c1;
float
c1.start();
mileage;
c1.drive();
public:
}
void start();
void drive();
};
Unit-4 Objects and Classes A D Patel Institute of Technology 15
Program: class, object
Write a C++ program to create class Test having data members
mark and spi.
Create member functions SetData() and DisplayData()to
demonstrate class and objects.
I like C++ so much
I like Rupesh sir
Private Public
Private Members
Private members of the class can be
By default all the members of class
Class accessed within the class and from
are private
member functions of the class.
class car A private member variable or
{ function cannot be accessed, or even
Private:
long int price; I like C++ so much
viewed from outside the class.
float mileage;
void setdata() I like Rupesh sir
This feature in OOP is known as Data
{
price = hiding / Encapsulation
700000; mileage
= 18.5;
}
};
Unit-4 Objects and Classes A D Patel Institute of Technology 24
Private Members
Private members of the class can be accessed within the class and
from member functions of the class.
They cannot be accessed outside the class or from other
programs, not even from inherited class.
I like C++ so much
If you try to access private data from outside of the class, compiler
throws error.
I like Rupesh sir
This feature in OOP is known as Data hiding / Encapsulation.
If any other access modifier is not specified then member default
acts as Private member.
yash.setData(23,1500);
yash.displaydata();
raj.setData(27,1800);
raj. displaydata();
return 0;
Unit-4 }
Objects and Classes A D Patel Institute of Technology 41
Program: Function with
class Employee{ argument
private :
int age; int salary;
public :
void setData(int , int);
void displaydata();
}; I like C++ so much
void Employee::setData(int x, int y){
age=x;
salary=y;
I like Rupesh sir
}
void Employee::displaydata(){
cout<<"age="<<age<<endl;
cout<<"salary="<<salary<<endl;
}
Unit-4 Objects and Classes A D Patel Institute of Technology 42
Passing Objects as Function
Arguments
Function with argument and returns value
#include <iostream> Value of
using namespace std; int main() Argument int fun1(int
{ f)
..... {
int add(int, int); b = .....
fun1(a); .....
int main(){
int a=5,b=6,ans; I like C++ so much
}
..... Function
Result }
return e;
ans = add(a,b);
I like Rupesh sir
cout<<"Addition is="<<ans;
return 0;
}
int add(int x,int y)
{
return x+y;
}
Unit-4 Objects and Classes A D Patel Institute of Technology 44
Object as Function arguments
time
int time
int
t1
a t2
b
Function
I like C++ so much
void add(intI x,
like Rupesh
int y) void sir
addtime(time x, time y)
{ {
statements… statements…
} }
int main() int main()
{ {
int a=5,b=6; time t1,t2,t3;
add(a,b); t3.addtime(t1,t2);
} }
Unit-4 Objects and Classes A D Patel Institute of Technology 45
Object as Function arguments
t1.getTime();
t1.printTime();
t2.getTime();
t2.printTime();
t3.addTime(t1,t2);
cout<<"\nafter adding two objects";
t3.printTime();
return 0;
}
t3.addTime(t1,t2);
Function Declaration
void addTime(Time x, Time y)
{
hour = x.hour + y.hour;
minute = x.minute + y.minute;
second = x.second + y.second;
}
Program: Passing object as argument
Define class Complex with members real and imaginary .
Also define function to setdata() to initialize the members,
print() to display values and addnumber() that adds two
complex objects.
Function result
I like C++ so much
int add(int I
x,like
int y)Rupesh sir
time addtime(time x, time y)
{ {
return return //object of class time
} }
int main() int main()
{ {
int a=5,b=6,result; time t1,t2,t3,result;
result = add(a,b); result = t3.addtime(t1,t2);
} }
Unit-4 Objects and Classes A D Patel Institute of Technology 53
Passing and returning object
t1.getTime();
t1.printTime();
t2.getTime();
t2.printTime();
ans=t3.addTime(t1,t2);
cout<<"\nafter adding two objects";
ans.printTime();
return 0;
}
Program: Returning object
C++ program to add two complex numbers by Pass and Return
Object from the Function.
printdata();
}
I like
objects are created.
C++ so much
Only space for member variable is allocated separately for each
I like
object because, Rupesh
the member sirwill hold different data
variables
values for different objects.
Member function 2
int demo::count; d1 d2 d3
int main()
{
demo d1,d2,d3; Static members are declared inside
d1.getcount(); the class and defined outside the
d2.getcount(); class.
d3.getcount();
return 0;
}
class demo
Regular Data members
{
int count;
public:
void getcount()
{
count = 0;
cout<<"count="<< +
d1 d2 d3
+count;
}
}; 10 01 10
int main()
{ count count count
demo d1,d2,d3;
d1.getcount();
d2.getcount();
d3.getcount();
return 0;
}
Static Data Members
Data members of the class which are shared by all objects are known
as static data members.
Only one copy of a static variable is maintained by the class and it is
common for all objects.
c.getdata(300);
a.getcount(); Output:
value of count: 1
return 0; value of count: 2
} value of count: 3
static
variable
variable
a redeclared
a declared
outside
inside
theclass
class
but,
usingstorage
scope resolution
is not allocated
operator.
Storage for the variable will be allocated
Unit-4 Objects and Classes A D Patel Institute of Technology 75
class A
{
Program : Static data member
int x;
public:
A()
{
cout << "A's constructor called " << endl;
}
};
class B
{
static A a;
public:
B()
{
cout << "B's constructor called " << endl;
}
};
A B::a; // definition of a
Output:
int main() A's constructor called
{
B's constructor called
B b1, b2, b3;
return 0; B's constructor called
} B's constructor called
Static Member Functions
Static Member Functions
Static member functions can access only static members of the
class.
Static member functions can be invoked using class name, not
object.
I like C++ so much
There cannot be static and non-static version of the same
function.
I like Rupesh sir
They cannot be virtual.
They cannot be declared as constant or volatile.
A static member function does not have this pointer.
a.getdata(100);
I like C++ so much
item::getcount();
c.getdata(300); Output:
item::getcount(); value of count: 1
return 0; value of count: 2
} value of count: 3
I like Rupesh
function sir
cin>>mileage;
} cin>>mileage;
}; }
};
int main() int main()
Called
{ {
automatically
car c1,c2;
c1; car c1,c2;
c1;
on creation
c1.setdata()
of object
;
c2.setdata()
} ; }
Unit-4 Objects and Classes A D Patel Institute of Technology 96
Properties of Constructor class car
Constructor should be declared in public {
private:
section because private constructor cannot float
be invoked outside the class so they are mileage;
public:
useless.
car()
I like C++ so much
Constructors do not have return types and
they cannot return values, not even void.
{
};
int main()
{
Rectangle r1;
return 0;
}
Unit-4 Objects and Classes A D Patel Institute of Technology 98
Types of Constructors
Types of Constructors
1) Default constructor
2) Parameterized constructor
3) Copy constructor
breadth=2;
} I like Rupesh sir
void Calculate(){
cout<<"\narea="<<length * breadth;
A1 A2
}
length breadth length breadth
};
5 2 5 2
Unit-4 Objects and Classes A D Patel Institute of Technology 102
2) Parameterized Constructor
Constructors that can take arguments are called parameterized
constructors.
Sometimes it is necessary to initialize the various data elements of
different objects with different values when they are created.
I like C++ so much
We can achieve this objective by passing arguments to the
constructor function when the objects are created.
I like Rupesh sir
demo(demo &x){ 5 6
m = x.m;
n = x.n; obj2
cout<<"Copy Constructor"; m n
}
5 6
};
Unit-4 Objects and Classes A D Patel Institute of Technology 108
Program: Types of Constructor
Create a class Rectangle having data members length and
width. Demonstrate default, parameterized and copy
constructor to initialize members.
constructor
length = x;
width = y;
}
rectangle(rectangle &_r){ // Copy constructor
length = _r.length;
width = _r.width;
}
Program: Types of Constructor (Cont…)
int main()
{
rectangle r1; // Invokes default
constructor
rectangle r2(10,20); // Invokes
parameterized
constructor
rectangle r3(r2); // Invokes copy
constructor
}
Destructor
Destructor class car
{
float mileage;
• Destructor is used to destroy the objects public:
that have been created by a constructor. car(){
cin>>mileage;
• The syntax for destructor is same as that }
for the constructor,
~car(){
– the class name is used for the name of cout<<"
destructor, destructor";
}
– with a tilde (~) sign as prefix to it.
};
Destructor
never takes any argument nor it returns any value nor it has return
type.
is invoked automatically by the complier upon exit from the
program.
should be declared in the public section.
Program: Destructor
class rectangle int main()
{ {
int length, width; rectangle x;
public: // default
rectangle(){ //Constructor constructor is
length=0;
width=0; I like C++ so much
called
}
cout<<”Constructor Called”;
} I like Rupesh sir
~rectangle() //Destructor
{
cout<<”Destructor Called”;
}
// other functions for reading, writing and
processing can be written here
};
Unit-4 Objects and Classes A D Patel Institute of Technology 114
Program: Destructor
class Marks{ int main( )
public: {
int maths; Marks
int science; m1;
//constructor Marks
Marks() { I like C++ so much
m2;
cout << "Inside Constructor"<<endl;
return
}
I like Rupesh sir
cout << "C++ Object created"<<endl;
0;
}
//Destructor
~Marks() {
cout << "Inside Destructor"<<endl;
cout << "C++ Object destructed"<<endl;
}
};
Unit-4 Objects and Classes A D Patel Institute of Technology 115
Operator Overloading
Operator Overloading
int a=5, b=10,c; Operator + performs
c = a + b; addition of
integer operands a, b
* As pointer, As multiplication
Some of C++ Operators
are already overloaded << As insertion, As bitwise shift left
}
// statements
I like Rupesh sir
Keyword substitute the operator
Example:
void operator + (arguments);
int operator - (arguments);
class-name operator / (arguments);
float operator * (arguments);
Unit-4 Objects and Classes A D Patel Institute of Technology 120
Overloading Binary operator + int main()
class complex{ {
int real,imag; complex
public: c1(4,6),c2(7,9);
complex(){ complex c3;
real=0; imag=0; c3 = c1 + c2;
} c1.disp();
complex(int x,int y){ c2.disp();
real=x; imag=y; c3.disp();
} return 0;
void disp(){ }
cout<<"\nreal value="<<real<<endl;
cout<<"imag value="<<imag<<endl;
}
complex operator + (complex);
};
complex complex::operator + (complex c){
complex tmp;
tmp.real = real + c.real; Similar to function call
tmp.imag = imag + c.imag; c3=c1.operator +
return tmp; (c2);
}
Binary Operator Arguments
result = obj1.operator symbol (obj2);//function
notation
result = obj1 symbol obj2; //operator
notation
complex operator + (complex x)
{
complex tmp;
tmp.real = real + x.real;
tmp.imag = imag + x.imag;
return tmp;
}
result = obj1.display();
void display()
{
cout<<"Real="<<real;
cout<<"Imaginary="<<imag;
}
Operator Overloading
Operator overloading is compile time polymorphism.
You can overload most of the built-in operators available in C++.
+ - * / % ^
& | ~ ! , =
<
<< I like C++ so much
>>
> <=
==
>=
!=
++
&&
--
||
+=
|= I like Rupesh sir
-=
*=
/=
<<=
%=
>>=
^=
[]
&=
()
-> ->* new new [] delete delete []
}
m = x;
I like C++ so much
I like Rupesh sir
void operator ++()
{
++m;
cout<<"Pre Increment="<<m;
}
void operator ++(int)
{
m++;
cout<<"Post Increment="<<m;
}
};
Unit-4 Objects and Classes A D Patel Institute of Technology 130
Invoking Operator Function
Binary operator
operand1 symbol operand2
Unary operator
operand symbol
symbol operand
I like C++ so much
I like Rupesh
Binary operator using friend function
operator symbol (operand1,operand2)
sir
Unary operator using friend function
operator symbol (operand)
int a;
float b = I like C++ sois converted
a = 10;
float much to integer automatically
a = b;
10.54;
integer float
I like Rupesh sir
by complier.
basic to basic type conversion.
(Basic) (Basic)
Syntax:
I like Rupesh sir
operator
destinationtype()
{
....
return
}
Unit-4 Objects and Classes A D Patel Institute of Technology 139
Program: Class to basic type conversion
class sample int main()
{ {
float a; sample S;
public: int y= S;//Class to Basic
sample() conversion
{ cout<<"The value of
}
a=10.23;
I like C++ so much
y="<<y;
return 0;
}
operator int() //Casting operator
{
I like Rupesh sir
function
Explicit type conversion
int x; y = int (S);
x=a;
return x;
Automatic type conversion
} y = S;
};
}
I like C++ so much
a[i] = i*2; cout<<“Length of
V="<<len;
};
operator int(); I like Rupesh sir
return 0;
}
vector:: operator int() {
int sum=0;
for(int i=0;i<5;i++)
sum = sum + a[i];
return sum;}
} {
}; commonb =
int main() temp.getvalue();
{ }
alpha operator alpha() //operator
obja(10); function
beta {
objb(obja); return alpha(commonb);
beta objb(20); }
class stock2 ;
class stock1{ Program: Type Conversion
int code , item ;
float price ;
public :
stock1 ( int a , int b , int c ) {
code = a ; item = b ; price = c ;
}
void disp () {
cout << " code " << code << " \n " ;
cout << " items " << item << " \n " ;
cout << " price per item Rs. " << price << " \n
" ;
}
int getcode (){ return code; }
int getitem (){ return item ; }
int getprice (){ return price ; }
operator float () {
return ( item*price ) ;
}
class stock2{
int code ;
Program: Type Conversion
float val ;
public :
stock2 () {
code = 0; val = 0 ;
}
stock2( int x , float y ){
code = x ; val = y ;
}
void disp () {
cout << " code " << code << " \n " ;
cout << " total value Rs. " << val << " \n
" ;
}
stock2( stock1 p ) {
code = p.getcode() ;
val = p.getitem() * p.getprice() ;
}
};
int main() Program: Type Conversion
{
stock1 i1 ( 101 , 10 ,125.0 ) ;
stock2 i2 ;
float tot_val = i1;
i2 = i1 ;
cout << " Stock Details : Stock 1 type " << " \
n " ;
i1.disp ();
cout << " Stock Value " << " - " ;
cout << tot_val << " \n " ;
cout << " Stock Details : Stock 2 type " << " \
n " ;
i2.disp () ;
return 0 ;
}
Thank You