Object Oriented Programming 5 + 6-1
Object Oriented Programming 5 + 6-1
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
■ class Demo {
■ public:
■ Demo() { cout << "Constructor called\n"; }
■ ~Demo() { cout << "Destructor called\n"; }
■ };
Output:
■ 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
Derived Class };
■ 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 () {
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
class Parent {
public: void show() { cout << "Parent"; }
};
class Child1 : public Parent {};
class Child2 : public Parent {};
Hybrid Inheritance
■ class A {
■ public: void showA() {}
■ };
■ class B : virtual public A {};
■ class C : virtual public A {};
■ class D : public B, public C {};
Assignment: