Unit 5 - Objects and Classes
Unit 5 - Objects and Classes
Object Oriented
Programming
Unit-5
Object and
Classes
Outline
▪ 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
▪
▪
I like
Operator Overloading
Type Conversion
Rupesh sir
Physical objects…
Unit-5 Objects and Classes 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
Car
{
private:
I like C++ so much
Attributes
int price;
I like Rupesh sirPrice
float mileage;
public: Mileage
void start(); Methods
void drive(); Start
}; Drive
Class
class car I like C++ so much Object
{
private: I like Rupeshintsirmain()
int price;
{
float mileage;
public: car c1;
void start(); c1.start();
void drive(); }
};
Unit-5 Objects and Classes 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: I like Rupesh{ sir
int price; car c1;
float mileage; c1.start();
public: c1.drive();
void start(); }
void drive();
};
Unit-5 Objects and Classes 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
▪ ByPrivate
defaultmembers of the class
all the members of can
classbe
Class accessed within the class and from
are private
member functions of the class.
▪ A private member variable or
class car
function cannot be accessed, or even
{ Private:
long int price;
I like C++ so muchviewed from outside the class.
float mileage;
void setdata()
I like Rupesh sir
This feature in OOP is known as Data
{ hiding / Encapsulation
price = 700000;
mileage = 18.5;
}
};
raj.setData(27,80000);
raj. displaydata();
return 0;
}
Unit-5 Objects and Classes 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-5 Objects and Classes 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); .....
..... return e;
int main(){
int a=5,b=6,ans;
I like C++ so much
} Function
Result
}
ans = add(a,b);
I like Rupesh sir
cout<<"Addition is="<<ans;
return 0;
}
int add(int x,int y)
{
return x+y;
}
Unit-5 Objects and Classes 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-5 Objects and Classes 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-5 Objects and Classes 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 Rupesh sir
Reduce the time and memory by storing the data in a single
variable.
float price;
public: I like Rupesh sir
void books::putdata()
{
void getdata();
void putdata() cout<<"Title:"<<title<< "\n";
}; cout<<"Price:"<<price<< "\n”;
}
};
}
I like Rupesh
function sir
};
}
};
int main()
{
Rectangle r1;
return 0;
}
Unit-5 Objects and Classes 93
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-5 Objects and Classes 97
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
int main()
{
rectangle r1; // Invokes default constructor
rectangle r2(10,20); // Invokes parameterized
constructor
rectangle r3(r2); // Invokes copy constructor
}
Destructor
class car
Destructor {
float mileage;
• Destructor is used to destroy the objects public:
car()
that have been created by a constructor. {
• The syntax for destructor is same as that cin>>mileage;
}
for the constructor,
– the class name is used for the name of ~car()
destructor, {
cout<<"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”;
}
};
}
I like Rupesh sir
cout << "C++ Object created"<<endl;
//Destructor
~Marks() {
cout << "Inside Destructor"<<endl;
cout << "C++ Object destructed"<<endl;
}
};
Unit-5 Objects and Classes 110
Operator Overloading
Operator Overloading
int a=5, b=10,c; Operator + performs
c = a + b; addition of
integer operands a, b
}
// statements
I like Rupesh sir
Keyword substitute the operator
Example:
void operator + (arguments);
int operator - (arguments);
class-name operator / (arguments);
float operator * (arguments);
Unit-5 Objects and Classes 115
Overloading Binary operator + int main()
class complex{ {
int real,imag; complex c1(4,6),c2(7,9);
public: complex c3;
complex(){ c3 = c1 + c2;
real=0; imag=0; c1.disp();
} c2.disp();
complex(int x,int y){ c3.disp();
real=x; imag=y; 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;
return tmp;
c3=c1.operator +(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
void operator ++()
{
++m;
I like Rupesh sir
cout<<"Pre Increment="<<m;
}
void operator ++(int)
{
m++;
cout<<"Post Increment="<<m;
}
};
Unit-5 Objects and Classes 125
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)
Member function 2
Object A3
int main(){
Account A1,A2,A3; Account No 103
A1.setdata(101,“Current“,3400); Account Type Current
A2.setdata(102,“Saving“,150);
A3.setdata(103,“Current“,7900); Balance 7900
return 0;
}
Static Data members / variables
Static Data members
A static data member is useful,
when all objects of the same class must share a common
information.
int demo::count;
d1 d2 d3
int main()
{
demo d1,d2,d3;
d1.getcount(); Static members are declared inside
d2.getcount(); the class and defined outside the
d3.getcount(); class.
return 0;
}
class demo
Regular Data members
{
int count;
public:
void getcount()
{
count = 0; d1 d3
cout<<"count="<< ++count;
d2
}
};
int main() 10 01 10
{
demo d1,d2,d3; count count count
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.
▪ Static members are declared inside the class and defined outside the
class. I like C++ so much
▪
▪
I like Rupesh sir
It is initialized to zero when the first object of its class is created.
you cannot initialize a static member variable inside the class
declaration.
▪ It is visible only within the class but its lifetime is the entire program.
▪ Static members are generally used to maintain values common to the
entire class.
}
count++;
I like Rupesh sir
void getcount(){
cout<<"\nvalue of count: "<<count;
}
};
int item :: count; // static variable definition
b.getdata(200);
I like Rupesh sir 1230
a.getcount();
c.getdata(300);
a.getcount(); Output:
value of count: 1
return 0; value of count: 2
} value of count: 3
}
count++;
I like Rupesh sir
static void getcount(){
cout<<”value of count: “<<count;
}
};
int item :: count; // static variable definition
int a;
I like C++
float b = 10.54; so much
a = 10;
▪ float is converted to integer automatically
a = b; by complier.
integer float
I like Rupesh sir
▪ basic to basic type conversion.
(Basic) (Basic)
Time integer
(Class) (Basic)
Syntax:
I like Rupesh sir
operator destinationtype()
{
....
return
}
}
a=10.23;
I like C++ so much
}
return 0;
{
I like Rupesh sir
function
Explicit type conversion
int x; y = int (S);
x=a;
return x;
Automatic type conversion
} y = S;
};