You are on page 1of 29

Object oriented Programming

Lecture 13- Inheritance types

Instructor: Natalia Chaudhry Object oriented programming in C++ by Robert Lafore PUCIT, University of the Punjab 1
Recap
• Inheritance
• Introduction to function overriding

Instructor: Natalia Chaudhry Object oriented programming in C++ by Robert Lafore PUCIT, University of the Punjab 2
Agenda
• Function overriding
• Inheritance: static vs non-static variables in base class
• Types of inheritance
• Inheritance hierarchies

Instructor: Natalia Chaudhry Object oriented programming in C++ by Robert Lafore PUCIT, University of the Punjab 3
Recommended reads
• Chapter 9, Object oriented programming in C++ by Robert Lafore
• Chapter 11, Dietal & Dietal

Instructor: Natalia Chaudhry Object oriented programming in C++ by Robert Lafore PUCIT, University of the Punjab 4
Inheritance: static vs non-static variables in base class
• When we create a Derived object, it contains a Base part (which is constructed
first), and a Derived part (which is constructed second).
• If the shared value is static in the base class, then all instances of derived classes
will see exactly that one base class member.
• If the value is not static, then each instance of a class will have its own copy
whether or not the value is in a base class.
• You could make the member a static member of the base class, then all derived
classes could access it, however any object of a derived that changes the static
member would change it for every other object.

Instructor: Natalia Chaudhry Object oriented programming in C++ by Robert Lafore PUCIT, University of the Punjab 5
Non-static variables in base class
Problem: Changes made by derived class on non-static variable are not visible to
base class
class DerivedClass : public BaseClass {
public:
#include <iostream> DerivedClass()
using namespace std; {
class BaseClass { cout << "Constructor of child class" << endl;
public: base += 1;
int base; }
BaseClass() void disp()
{ {
cout << "Constructor of parent class" << endl; cout << "Display by Child Class" << endl;
base = 0; cout << base << endl;
} }
void disp() };
{ int main()
cout << "Display by Parent Class" << endl; {
cout << base << endl; BaseClass b;
} DerivedClass obj;
};

b.disp();
obj.BaseClass::disp();
return 0;
}

Instructor: Natalia Chaudhry Object oriented programming in C++ by Robert Lafore PUCIT, University of the Punjab 6
Analysis of output
• Even though attribute (base) has been incremented in
derived class but the change is not reflected in base class.
• That’s why when we have displayed the value of attribute
using base class’ object and its disp() function, it is still
shown as 0
• On the other hand, when we displayed the value of this
attribute using derived class’ object (using disp() of base
class though), it is displayed as 1
• It doesn’t matter that if we are using base class method
for displaying values then only the base class version/
value of attribute will be shown. Instead, the value of
attribute depends on the object that we are using to call
the function. In this scenario, it is obj (i.e. derived class
object) obj.BaseClass::disp();

Instructor: Natalia Chaudhry Object oriented programming in C++ by Robert Lafore PUCIT, University of the Punjab 7
Solution: use static variables
class DerivedClass : public BaseClass {
public:
#include <iostream> DerivedClass()
using namespace std; {
class BaseClass { cout << "Constructor of child class" << endl;
public: base += 1;
static int base; }
void disp()
BaseClass()
{
{
cout << "Display by Child Class" << endl;
cout << "Constructor of parent class" << endl;
cout << base << endl;
}
}
void disp()
};
{
int main()
cout << "Display by Parent Class" << endl;
{
cout << base << endl;
BaseClass b;
}
DerivedClass obj;
};
int BaseClass::base = 0;
b.disp();
obj.BaseClass::disp();
return 0;
}

Instructor: Natalia Chaudhry Object oriented programming in C++ by Robert Lafore PUCIT, University of the Punjab 8
Single vs multiple inheritance
• With single inheritance, a class is derived from one base class.
• With multiple inheritance, a derived class inherits simultaneously from two or
more (possibly unrelated) base classes.

Instructor: Natalia Chaudhry Object oriented programming in C++ by Robert Lafore PUCIT, University of the Punjab 9
Hierarchical inheritance
If more than one class is inherited from the base class, it's known as hierarchical
inheritance.
For example: Physics, Chemistry, Biology are derived from Science class.

Instructor: Natalia Chaudhry Object oriented programming in C++ by Robert Lafore PUCIT, University of the Punjab 10
// first sub class
// base class
class Car : public Vehicle
class Vehicle
{
{
public:
};
Vehicle()
{
// second sub class
cout << "This is a Vehicle" << endl;
class Bus : public Vehicle
}
{
};
};

// main function
int main()
{
// creating object of sub class will
// invoke the constructor of base class
Car obj1;
Bus obj2;
return 0;
}
Instructor: Natalia Chaudhry Object oriented programming in C++ by Robert Lafore PUCIT, University of the Punjab 11
class Cube :public Side
class Side {
{ public:
protected: int cub()
int l; {
public: return (6 * l * l);
void set_values(int x) }
{ };
l = x; int main()
} {
}; Square s;
class Square : public Side s.set_values(10);
{ cout << "The square value is::" << s.sq()
public: << endl;
int sq() Cube c;
{ c.set_values(20);
return (l * l); cout << "The cube value is::" << c.cub() <<
} endl;
}; return 0;
}

Instructor: Natalia Chaudhry Object oriented programming in C++ by Robert Lafore PUCIT, University of the Punjab 12
Single vs multiple inheritance
• With single inheritance, a class is derived from one base class.
• With multiple inheritance, a derived class inherits simultaneously from two or
more (possibly unrelated) base classes.

Instructor: Natalia Chaudhry Object oriented programming in C++ by Robert Lafore PUCIT, University of the Punjab 13
Multiple inheritance
A class can inherit from more than one classes. i.e one sub class is inherited from
more than one base classes

Instructor: Natalia Chaudhry Object oriented programming in C++ by Robert Lafore PUCIT, University of the Punjab 14
Multiple inheritance
// base class // sub class derived from two base classes
class Vehicle { class Car : public Vehicle, public FourWheeler
public: {
Vehicle() Car()
{ {
cout << "This is a Vehicle" << endl; cout << "This is a car" << endl;
} }
}; };
// second base class // main function
class FourWheeler { int main()
public: {
FourWheeler() Car obj;
{ return 0;
cout << "This is a 4 wheeler Vehicle" << endl; }
}
};

Instructor: Natalia Chaudhry Object oriented programming in C++ by Robert Lafore PUCIT, University of the Punjab 15
Ambiguities in multiple inheritance
• Function overriding (derived class’ function gets called)
class Vehicle { // sub class derived from two base classes
public: class Car : public Vehicle, public FourWheeler
Vehicle() {
{ public:
cout << "This is a Vehicle" << endl; Car()
} {
void disp() cout << "This is a car" << endl;
{ }
cout << "disp of Vehicle" << endl; void disp()
} {
}; cout << "disp of car" << endl;
// second base class }
class FourWheeler { };
public:
FourWheeler() // main function
{ int main()
cout << "This is a 4 wheeler Vehicle" << endl; {
} // creating object of sub class will
void disp() // invoke the constructor of base classes
{ Car obj;
cout << "disp of wheeler Vehicle" << endl; Obj.disp();
} return 0;
}; }

Instructor: Natalia Chaudhry Object oriented programming in C++ by Robert Lafore PUCIT, University of the Punjab 16
Disp() not in derived class but in only one of the base classes
// sub class derived from two base classes
class Vehicle { class Car : public Vehicle, public FourWheeler
public: {
Vehicle() public:
{ Car()
cout << "This is a Vehicle" << endl; {
} cout << "This is a car" << endl;
}; }
// second base class };
class FourWheeler {
public: // main function
FourWheeler() int main()
{ {
cout << "This is a 4 wheeler Vehicle" << endl; // creating object of sub class will
} // invoke the constructor of base classes
void disp() Car obj;
{ Obj.disp();
cout << "disp of wheeler Vehicle" << endl; return 0;
} }
};

Instructor: Natalia Chaudhry Object oriented programming in C++ by Robert Lafore PUCIT, University of the Punjab 17
ERROR: disp() method exists in both base classes!
// sub class derived from two base classes
class Vehicle { class Car : public Vehicle, public FourWheeler
public: {
Vehicle() public:
{ Car()
cout << "This is a Vehicle" << endl; {
} cout << "This is a car" << endl;
void disp() }
{ };
cout << "disp of Vehicle" << endl;
} // main function
}; int main()
// second base class {
class FourWheeler { // creating object of sub class will
public: // invoke the constructor of base classes
FourWheeler() Car obj;
{ Obj.disp();
cout << "This is a 4 wheeler Vehicle" << endl; return 0;
} }
void disp()
{
cout << "disp of wheeler Vehicle" << endl;
}
};

Instructor: Natalia Chaudhry Object oriented programming in C++ by Robert Lafore PUCIT, University of the Punjab 18
Rules of inheritance
• Depending on the value of visibility-mode, multiple inheritance can be performed in
various ways
• When a class inherits another class with the visibility-mode - public.
When a class is publicly inherited by another class, its public members become
the public members of the derived class, its protected members become
the protected members of the derived class, while the private members of any class
cannot be inherited.
• When a class inherits another class with the visibility-mode - private.
When a class is privately inherited by another class, its public members become
the private members of the derived class, its protected members become
the private members of the derived class, while the private members of any class cannot
be inherited.
• When a class inherits another class with the visibility-mode - protected.
When a class is inherited by another class using the protected visibility mode,
its public members become the protected members of the derived class,
its protected members become the protected members of the derived class, while
the private members of any class cannot be inherited.

Instructor: Natalia Chaudhry Object oriented programming in C++ by Robert Lafore PUCIT, University of the Punjab 19
class A //base class class C : private A //privately-derived class
{ {
public:
private:
void funct()
int privdataA; //(functions have the same access {
protected: //rules as the data shown here) int a;
int protdataA; a = privdataA; //error: not accessible
public: a = protdataA; //OK
int pubdataA; a = pubdataA; //OK
}; }
};
class B : public A //publicly-derived class
{ class D : protected A //protectedly-derived class
public: {
void funct() public:
void funct()
{ {
int a; int a;
a = privdataA; //error: not accessible a = privdataA; //error: not accessible
a = protdataA; //OK
a = protdataA; //OK a = pubdataA; //OK
a = pubdataA; //OK }
} };
};
Instructor: Natalia Chaudhry Object oriented programming in C++ by Robert Lafore PUCIT, University of the Punjab 20
int main()
{
int a;
B objB;
a = objB.privdataA; //error: not accessible
a = objB.protdataA; //error: not accessible
a = objB.pubdataA; //OK (A public to B)

C objC;
a = objC.privdataA; //error: not accessible
a = objC.protdataA; //error: not accessible
a = objC.pubdataA; //error: not accessible
(A private to C)

D objD;
a = objD.privdataA; //error: not accessible
a = objD.protdataA; //error: not accessible
a = objD.pubdataA; //error: not accessible
(A private to C)
return 0;
}

Instructor: Natalia Chaudhry Object oriented programming in C++ by Robert Lafore PUCIT, University of the Punjab 21
Multilevel inheritance
Derive a class from the derived clas

Instructor: Natalia Chaudhry Object oriented programming in C++ by Robert Lafore PUCIT, University of the Punjab 22
Multi level inheritance
• deriving a class from the derived class: multilevel inheritance
• When a class is derived from a class which is also derived from another class, i.e. a class having
more than one parent classes, such inheritance is called Multilevel Inheritance.
class A int main()
{ {
public: C obj;
void display() obj.display();
{ return 0;
cout<<"Base class content."; }
}
}; • The compiler first looks for the display() function in class C.
class B: public A
{ • Since the function doesn't exist there, it looks for the
... .. ... function in class B (as C is derived from B).
};
class C: public B • The function also doesn't exist in class B, so the compiler
{ looks for it in class A (as B is derived from A).
... ... ...
}; Natalia Chaudhry
Instructor: Object oriented programming in C++ by Robert Lafore PUCIT, University of the Punjab 23
class person void display()
{ {
char name[100], gender[10]; person::display();
int age; cout << "Name of Company: " << company << endl;
public: cout << "Salary: Rs." << salary << endl;
void getdata() }
{ };
cout << "Name: ";
cin >> name; class programmer : public employee
cout << "Age: "; {
cin >> age; int number;
cout << "Gender: "; public:
cin >> gender; void getdata()
}
{
void display()
{
employee::getdata();
cout << "Name: " << name << endl; cout << "Number of programming language known: ";
cout << "Age: " << age << endl; cin >> number;
cout << "Gender: " << gender << endl; }
} void display()
}; {
class employee : public person employee::display();
{ cout << "Number of programming language known: " << number;
char company[100]; }
float salary; };
public:
void getdata() int main()
{ {
person::getdata(); programmer p;
cout << "Name of Company: "; cout << "Enter data" << endl;
cin>>company; p.getdata();
cout << "Salary: Rs."; cout << endl << "Displaying data" << endl;
cin >> salary; p.display();
} return 0;
}

Instructor: Natalia Chaudhry Object oriented programming in C++ by Robert Lafore PUCIT, University of the Punjab 24
Hybrid inheritance
• Combination of any inheritance type
• Hybrid inheritance is performed when we have to mix different types of inheritance within a
single program, for example, mixing single inheritance with multiple inheritance or multiple
inheritance within a single program.

Instructor: Natalia Chaudhry Object oriented programming in C++ by Robert Lafore PUCIT, University of the Punjab 25
class student class test : public student
//base class derivation //intermediate base class
{ {
protected: protected:
int r_no; int part1, part2;

public: public:
void getRollno() void getMarks()
{ {
cout << "Enter the roll number of student : "; cout << "Enter the marks of student in SA 1 : ";
cin >> r_no; cin >> part1;
} cout << "Enter the marks of student in SA 2 : ";
cin >> part2;
void putRollno() }
{
cout << "\nRoll Number -: " << r_no << "\n"; void putMarks()
} {
}; cout << "Marks Obtained : " << "\n";
cout << " Part 1 -: " << part1;
cout << "\n Part 2 -: " << part2 << "\n";
}
};

Instructor: Natalia Chaudhry Object oriented programming in C++ by Robert Lafore PUCIT, University of the Punjab 26
class result : public test, public sports
class sports
{
{
int total;
protected:
int score;
public:
void display()
public:
{
void getSportsMarks()
total = part1 + part2 + score;
{
putRollno();
cout << "Enter the marks in Physical Eduction : ";
putMarks();
cin >> score;
putSportsMarks();
}
cout << "Total Score : " << total;
void putSportsMarks()
}
{
};
cout << "Additional Marks : " << score << "\n \n";
}
int main()
};
{
result s1;
s1.getRollno();
s1.getMarks();
s1.getSportsMarks();
s1.display();

return 0;
}

Instructor: Natalia Chaudhry Object oriented programming in C++ by Robert Lafore PUCIT, University of the Punjab 27
Inheritance hierarchy

Instructor: Natalia Chaudhry Object oriented programming in C++ by Robert Lafore PUCIT, University of the Punjab 28
That’s it

Instructor: Natalia Chaudhry Object oriented programming in C++ by Robert Lafore PUCIT, University of the Punjab 29

You might also like