Object Oriented Programming (OOP) (1)
Object Oriented Programming (OOP) (1)
By
Irfan Abdullah
CONTENTS
• Concept of Class and Object in OOP
• Private and Public access specifiers
• Concept of Encapsulation
• What are Constructor and Destructor?
• Types of Constructor
• Inheritance in Object Oriented Programming
• Inheritance Access Specifiers
• Types of Inheritance
• Polymorphism
• concept of Abstraction
• Overloading and Overriding in OOP
Classes in C++
• A class in C++ is the building block, that leads to Object-
Oriented programming. It is a user-defined data type, which
holds its own data members and member functions, which can
be accessed and used by creating an instance of that class. A
C++ class is like a blueprint for an object.
• For Example: Consider the Class of Cars. There may be many
cars with different names and brand but all of them will share
some common properties like all of them will have 4 wheels,
Speed Limit, Mileage range etc. So here, Car is the class and
wheels, speed limits, mileage are their properties.
Classes in C++
• A Class is a user defined data-type which has data members and
member functions.
• Data members are the data variables and member functions are
the functions used to manipulate these variables and together
these data members and member functions defines the
properties and behavior of the objects in a Class.
• In the above example of class Car, the data member will be
speed limit, mileage etc and member functions can be apply
brakes, increase speed etc.
Object in C++
• An Object is an instance of a Class. When a class is defined, no
memory is allocated but when it is instantiated (i.e. an object is
created) memory is allocated.
Defining Class and Declaring Objects
• A class is defined in C++ using
keyword class followed by the
name of class. The body of class
is defined inside the curly
brackets and terminated by a
semicolon at the end.
• A class definition starts with the
keyword class followed by the
class name; and the class body,
enclosed by a pair of curly
braces. A class definition must
be followed either by a
semicolon or a list of
declarations.
Defining Class and Declaring Objects
• When a class is defined, only the specification for the object is
defined; no memory or storage is allocated. To use the data and
access functions defined in the class, you need to create objects.
• Syntax:
ClassName ObjectName;
• Following statements declare two objects of class Box.
Box Box1; // Declare Box1 of type Box
Box Box2; // Declare Box2 of type Box
• Both of the objects Box1 and Box2 will have their own copy of
data members.
Defining Class and Declaring Objects
// box 2 specification
#include <iostream>
Box2.height = 10.0;
using namespace std;
Box2.length = 12.0;
class Box {
Box2.breadth = 13.0;
public:
double length; // Length of a box
// volume of box 1
double breadth; // Breadth of a box
volume = Box1.height * Box1.length * Box1.breadth;
double height; // Height of a box
cout << "Volume of Box1 : " << volume <<endl;
};
int main() {
// volume of box 2
Box Box1; // Declare Box1 of type Box
volume = Box2.height * Box2.length * Box2.breadth;
Box Box2; // Declare Box2 of type Box
cout << "Volume of Box2 : " << volume <<endl;
double volume = 0.0; // Store the volume of a box here
return 0;
}
// box 1 specification
Box1.height = 5.0; • It produces the following result −
Box1.length = 6.0; Volume of Box1 : 210
Box1.breadth = 7.0; Volume of Box2 : 1560
Access Specifiers in C++
• In C++, there are three access specifiers:
};
Multilevel Inheritance
• In this type of inheritance, a
derived class is created from
another derived class.
Multilevel Inheritance
#include <iostream> class Car: public fourWheeler{
using namespace std; public:
// base class Car()
class Vehicle {
{ cout<<"Car has 4 Wheels"<<endl;
public: }
Vehicle() };
{ // main function
cout << "This is a Vehicle" << endl; int main()
} {
}; //creating object of sub class will
class fourWheeler: public Vehicle //invoke the constructor of base classes
{ public: Car obj;
fourWheeler() return 0;
{ }
cout<<"Objects with 4 wheels are • The output is
vehicles"<<endl;
} This is a Vehicle
}; Objects with 4 wheels are vehicles
Car has 4 Wheels
Multiple Inheritance
• Multiple Inheritance is a • Syntax:
feature of C++ where a class class subclass_name :
can inherit from more than access_mode base_class1,
one classes. i.e one sub class access_mode base_class2, ....
is inherited from more than {
one base classes. //body of subclass
};
Multiple Inheritance
#include <iostream> class Car: public Vehicle, public FourWheeler {
using namespace std;
// first base class };
class Vehicle {
public: // main function
Vehicle() int main()
{ {
cout << "This is a Vehicle" << endl; // creating object of sub class will
} // invoke the constructor of base classes
}; Car obj;
// second base class return 0;
class FourWheeler { }
public: • The output of the program is:
FourWheeler() This is a Vehicle
{ This is a 4 wheeler Vehicle
cout << "This is a 4 wheeler Vehicle" << endl;
}
};
Hierarchical Inheritance
• In this type of inheritance,
more than one sub class is
inherited from a single base
class. i.e. more than one
derived class is created from a
single base class.
Hierarchical Inheritance
#include <iostream> class Bus: public Vehicle
using namespace std; {
// base class
class Vehicle };
{ // main function
public: int main()
Vehicle() {
{ // creating object of sub class will
cout << "This is a Vehicle" << endl; // invoke the constructor of base class
} Car obj1;
}; Bus obj2;
// first sub class return 0;
class Car: public Vehicle }
{ • The output of the program is
This is a Vehicle
}; This is a Vehicle
Hybrid (Virtual) Inheritance
• Hybrid Inheritance is
implemented by combining
more than one type of
inheritance. For example:
Combining Hierarchical
inheritance and Multiple
Inheritance.
Hybrid (Virtual) Inheritance
#include <iostream> // first sub class
using namespace std; class Car: public Vehicle
// base class {
class Vehicle
{ };
public: // second sub class
Vehicle() class Bus: public Vehicle, public Fare
{ {
cout << "This is a Vehicle" << endl;
} };
}; // main function
//base class int main()
class Fare {
{ // creating object of sub class will
public: // invoke the constructor of base class
Fare() Bus obj2;
{ return 0;
cout<<"Fare of Vehicle\n"; }
} • The output of the program is
}; This is a Vehicle
Fare of Vehicle
Multipath Inheritance
• A derived class with two base
classes and these two base
classes have one common
base class is called multipath
inheritance. An ambiguity can
arrise in this type of
inheritance.
Multipath Inheritance
int main()
#include<iostream> {
class ClassA ClassD obj;
{ //obj.a = 10; //Statement 1, Error
public: //obj.a = 100; //Statement 2, Error
int a; obj.ClassB::a = 10; //Statement 3
}; obj.ClassC::a = 100; //Statement 4
class ClassB : public ClassA obj.b = 20;
{ obj.c = 30;
public: obj.d = 40;
int b; cout<< "\n A from ClassB : "<< obj.ClassB::a;
}; cout<< "\n A from ClassC : "<< obj.ClassC::a;
class ClassC : public ClassA cout<< "\n B : "<< obj.b;
{ cout<< "\n C : "<< obj.c;
public: cout<< "\n D : "<< obj.d;
int c; return 0;
}; }
class ClassD : public ClassB, public ClassC
{ • The output of the program is
public: A from ClassB : 10
int d; A from ClassC : 100
}; B : 20
C : 30
D : 40
Encapsulation
• Encapsulation is an Object Oriented Programming concept that binds
together the data and functions that manipulate the data, and that keeps
both safe from outside interference and misuse. Data encapsulation led to
the important OOP concept of data hiding.
• C++ supports the properties of encapsulation and data hiding through the
creation of user-defined types, called classes. We already have studied that
a class can contain private, protected and public members. By default, all
items defined in a class are private.
Encapsulation
#include<iostream> int main()
using namespace std; {
class Encapsulation Encapsulation obj;
{
private: obj.set(5);
// data hidden from outside world
int x; cout<<obj.get();
return 0;
public: }
// function to set value of variable x • The output of the program is
void set(int a) 5
{
x =a; • In the above program the variable x is
} made private. This variable can be accessed
and manipulated only using the functions
// function to return value of variable x get() and set() which are present inside the
int get() class. Thus we can say that here, the
{ variable x and the functions get() and set()
return x; are binded together which is nothing but
} encapsulation.
};
Constructor
• A constructor is a member function of a class that has the same name
as the class name. It helps to initialize the object of a class. It can
either accept the arguments or not. It is used to allocate the memory
to an object of the class. It is called whenever an instance of the class
is created. It can be defined manually with arguments or without
arguments. There can be many constructors in class. It can be
overloaded but it can not be inherited or virtual. There is a concept of
copy constructor which is used to initialize a object from another
object.
• Syntax:
ClassName()
{
//Constructor's Body
}
Destructor
• Like constructor, deconstructor is also a member function of a class
that has the same name as the class name preceded by a tilde(~)
operator. It helps to deallocate the memory of an object. It is called
while object of the class is freed or deleted. In a class, there is always
a single destructor without any parameters so it can’t be oveloaded. It
is always called in the reverse order of the constructor. if a class is
inherited by another class and both the classes have a destructor then
the destructor of the child class is called first, followed by the
destructor of the parent or base class.
• Syntax:
~ClassName()
{
}
Implementation of Constructor and
Destructor
#include <iostream> int main()
using namespace std; {
class Z Z z1; // Constructor Called
{ int a = 1;
public: if(a==1)
// constructor {
Z() Z z2; // Constructor Called
{ } // Destructor Called for z2
cout<<"Constructor called"<<endl; return 0;
} } // Destructor called for z1
// destructor • The output of the program is
~Z() Constructor called
{ Constructor called
cout<<"Destructor called"<<endl; Destructor called
} Destructor called
};
Difference between Constructor and
Destructor
Constructor Destructor
• Constructor helps to initialize the object of a • Whereas destructor is used to destroy the
class. instances.
• It is declared as Classname( arguments if • Whereas it is declared as ~ ClassName( no
any ){Constructor’s Body }. arguments ){ };.
• Constructor can either accept an arguments or • While it can’t have any arguments.
not.
• It is called while object of the class is freed or
• A constructor is called when an instance or deleted.
object of a class is created.
• While it is used to deallocate the memory of an
• Constructor is used to allocate the memory to an object of a class.
instance or object.
• While it can’t be overloaded.
• Constructor can be overloaded.
• Here, it’s name is also same as the class name
• The constructor’s name is same as the class preceded by tiled (~) operator.
name.
• While in a class, there is always a single
• In a class, there can be multiple constructors. destructor.
• There is a concept of copy constructor which is • While here, there is no copy constructor
used to initialize a object from another object. concept.
Types of Constructors in C++
• Constructors are of three types:
• Default Constructor
• Parametrized Constructor
• Copy Constructor
Default Constructor
• Default constructor is the constructor class Cube
which doesn't take any argument. It {
has no parameter. public:
• Syntax: int side;
class_name(parameter1, parameter2, ...) Cube()
{ {
// constructor Definition side = 10;
} }
};
• In this case, as soon as the object is int main()
created the constructor is called which {
initializes its data members.
Cube c;
• If we have not defined a constructor in cout << c.side;
our class, then the C++ compiler will return 0;
automatically create a default }
constructor with an empty code and no
parameters. • The output is
10
Parameterized Constructor
• These are the constructors with parameter. Using this
constructor, you can provide different values to data members
of different objects, by passing the appropriate values as
argument.
• To create a parameterized constructor, simply add parameters
to it the way you would to any other function. When you define
the constructor’s body, use the parameters to initialize the
object.
• This is the preferred method to initialize member data.
Parameterized Constructor
#include <iostream> double calculateArea() {
using namespace std; return length * height;
}
// declare a class };
class Wall { int main() {
private: // create object and initialize data
double length; members
double height; Wall wall1(10.5, 8.6);
Wall wall2(8.5, 6.3);
public: cout << "Area of Wall 1: " <<
wall1.calculateArea() << endl;
// create parameterized constructor
cout << "Area of Wall 2: " <<
Wall(double len, double hgt) { wall2.calculateArea() << endl;
// initialize private variables
length = len; return 0;
height = hgt; }
}
• The output of the program is
• Area of Wall 1: 90.3
• Area of Wall 2: 53.55
Copy Constructor
• A copy constructor is a member function which initializes an
object using another object of the same class. A copy constructor
has the following general function prototype: