OBJECT
ORIENTED
PROGRAMMING
Lecture 5 + 6
Instructor: Tayyba Khalid
Content:
■ Copy Constructors
Types of inheritance
■ Destructors
Single inheritance
■ Const vs non-const functions Multilevel inheritance
■ Static data members & methods Multiple inheritance
■ Inheritance: Hierarchical inheritance
■ Protected Access modifiers Hybrid inheritance
■ Modes of inheritance
Copy Constructors
A copy constructor creates a new object as a copy of an existing
object. It's invoked when:
• An object is initialized from another object of the same class.
• An object is passed by value to a function.
• An object is returned by value from a function.
destructor
■ A destructor is a special member function that is executed
automatically when an object of that class is destroyed.
■ No data type use same like constructor
■ Same name of class
■ They cannot return value and cannot accept parameter.
■ We use destructor to automatically clean up resources when an object
destroyed.
example
■ #include <iostream>
■ using namespace std;
■ class Demo {
■ public:
■ Demo() { cout << "Constructor called\n"; }
■ ~Demo() { cout << "Destructor called\n"; }
■ };
Output:
■ int main() { Constructor called
Destructor called
■ Demo d;
■ return 0;
■ }
Static data members & methods
■ Static data members are class members that are declared
using static keywords.
■ Syntax
■ className {
static data_type data_member_name;
.....
}
■ To access the static data member of any class we have to define it first and
static data members are defined outside the class definition.
■ datatype class_name::var_name = value...;
Example:
#include <iostream> // we cannot initialize the static data member inside the
using namespace std; // class due to class rules and the fact that we cannot
// assign it a value using constructor
// class definition int A::x = 2;
class A { // Driver code
int main()
public: {
// static data member here // accessing the static data member using scope
// resultion operator
static int x;
A() { cout << "A's constructor called cout << "Accessing static data member: " << A::x
<< endl;
" << endl; }
}; return 0;
}
Protected Access Modifier
Protected members are accessible:
class Base {
• Inside the class. protected:
• Inside derived (child) classes. int protectedVar;
};
• Not accessible from outside the class class Derived : public Base {
hierarchy. public:
void show() {
protectedVar = 10; // OK
}
};
Inheritance
■ The capability of a class to derive properties and characteristics from another
class is called Inheritance. Inheritance is one of the most important features
of Object-Oriented Programming in C++.
■ class DerivedClass : mode_of_inheritance BaseClass { // Body of the
Derived Class };
■ where mode of inheritance controls the access level of the inherited
members of the base class in the derived class
Access Base Class Members
■ class Base {
■ public:
■ int n;
■ void printN() {
int main() {
■ cout << n << endl; // Creating objects of derived
■ } Derived d;
// Accessing Derived class member
■ };
d.func();
// Accessing Base class member
■ // Inheriting Base class publicly d.printN();
■ class Derived : public Base {
return 0;
}
■ public:
■ void func () {
■ // Accessing Base class members
■ n = 22;
■ }
Modes of Inheritance
Access specifier In own class From derived Outside of class
class
Public Yes Yes Yes
Protected Yes Yes No
private yes no no
How can we access private data member
in derived class?
■ The private members in the base class cannot be directly accessed in
the derived class, while protected and public members can be directly
accessed.
■ To access or update the private members of the base class in derived
class, we have to use the corresponding getter and setter functions of
the base class or declare the derived class as friend class
Example:
■ class Base {
■ public:
■ int x;
■ protected:
■ int y;
■ private:
■ int z;
■ };
■ class Derived : public Base {
■ // x becomes public
■ // y becomes protected
■ // z is not inherited
■ };
Types:
The inheritance can be classified on the basis of the relationship between the
derived class and the base class. In C++, we have 5 types of inheritances:
• Single inheritance
• Multilevel inheritance
• Multiple inheritance
• Hierarchical inheritance
• Hybrid inheritance
Single Inheritance
In single inheritance, a class is allowed to inherit from only one class. i.e. one base class is inherited by
one derived class only.
public:
Vehicle() {
cout << "This is a Vehicle"<< endl;
} };
class Car : public Vehicle {
public:
Car() {
cout << "This Vehicle is Car"<< endl;
}};
int main() {
Car obj;
return 0;
}
Multilevel Inheritance
■ In multilevel inheritance, a derived class is created from another derived class and
that derived class can be derived from a base class or any other derived class.
There can be any number of levels.
■ A class inherits from a derived class (i.e., inheitance chain)
class A {
public: void Afunc() { cout << "A"; }
};
class B : public A {
public: void Bfunc() { cout << "B"; }
};
class C : public B {
public: void Cfunc() { cout << "C"; }
};
Multiple Inheritance
■ Multiple Inheritance is a feature of C++ where a class can inherit from more
than one class. i.e one subclass is inherited from more than one base class.
■ A class inherits from more than one base class.
class A {
public: void showA() { cout << "A"; }
};
class B {
public: void showB() { cout << "B"; }
};
class C : public A, public B {};
Hierarchical Inheritance
■ In hierarchical inheritance , more than one subclass is inherited from a single
base class. i.e. more than one derived class is created from a single base
class.
■ One base class, multiple derived classes.
class Parent {
public: void show() { cout << "Parent"; }
};
class Child1 : public Parent {};
class Child2 : public Parent {};
Hybrid Inheritance
Hybrid Inheritance is implemented by combining more than one type of
inheritance. For example: Combining Hierarchical inheritance and
Multiple Inheritance will create hybrid inheritance in C++.
■ There is no particular syntax of hybrid inheritance. We can just
combine two of the above inheritance types.
•Combination of two or more types (e.g., multiple + hierarchical).
•Can lead to the diamond problem, solved using virtual inheritance.
Example:
■ class A {
■ public: void showA() {}
■ };
■ class B : virtual public A {};
■ class C : virtual public A {};
■ class D : public B, public C {};
Assignment:
■ Write 2 ,examples , each type of inheritance.