Inheritance Mod 3 (Part 1)
Inheritance Mod 3 (Part 1)
Inheritance Mod 3 (Part 1)
role of virtual base class, constructor and destructor execution, base initialization using derived class
constructors.
Binding, Polymorphism and Virtual Function: Binding, Static binding, Dynamic binding, Static
polymorphism: Function Overloading, Dynamic polymorphism: virtual functions, method overriding with
virtual functions, pure virtual functions, abstract classes.
Inheritance:
// Base class
class Shape {
public:
void setWidth(int w) {
width = w;
}
void setHeight(int h) {
height = h;
}
protected:
int width;
int height;
};
// Derived class
class Rectangle: public Shape {
public:
int getArea() {
return (width * height);
}
};
int main(void) {
Rectangle Rect;
Rect.setWidth(5);
Rect.setHeight(7);
return 0;
}
When the above code is compiled and executed, it produces the following result −
Total area: 35
A derived class inherits all base class methods with the following exceptions −
Type of Inheritance
When deriving a class from a base class, the base class may be inherited through public,
protected or private inheritance. The type of inheritance is specified by the access-specifier as
explained above.
We hardly use protected or private inheritance, but public inheritance is commonly used. While
using different type of inheritance, following rules are applied −
Public Inheritance − When deriving a class from a public base class, public members of
the base class become public members of the derived class and protected members of
the base class become protected members of the derived class. A base
class's private members are never accessible directly from a derived class, but can be
accessed through calls to the public and protected members of the base class.
Protected Inheritance − When deriving from a protected base
class, public and protected members of the base class become protected members of the
derived class.
Private Inheritance − When deriving from a private base
class, public and protected members of the base class become private members of the
derived class.
Multiple Inheritance
A C++ class can inherit members from more than one class and here is the extended syntax −
class derived-class: access baseA, access baseB....
Where access is one of public, protected, or private and would be given for every base class and
they will be separated by comma as shown above. Let us try the following example −
Live Demo
#include <iostream>
protected:
int width;
int height;
};
int main(void) {
Rectangle Rect;
int area;
Rect.setWidth(5);
Rect.setHeight(7);
area = Rect.getArea();
return 0;
}
When the above code is compiled and executed, it produces the following result −
Total area: 35
Total paint cost: $2450
Type of Inheritance
Single Inheritance
Multiple Inheritance
Multilevel Inheritance
Hierarchical Inheritance
Hybrid Inheritance
Types Of Inheritance
Given below is a pictorial representation of the various types of inheritance.
We will see each type of inheritance with examples in the below sections.
#1) Single Inheritance
In single inheritance, a class derives from one base class only. This means that there is only one
subclass that is derived from one superclass.
#include <iostream>
#include <string>
using namespace std;
class Animal
{
string name="";
public:
int tail=1;
int legs=4;
};
class Dog : public Animal
{
public:
void voiceAction()
{
cout<<"Barks!!!";
}
};
int main()
{
Dog dog;
cout<<"Dog has "<<dog.legs<<" legs"<<endl;
cout<<"Dog has "<<dog.tail<<" tail"<<endl;
cout<<"Dog ";
dog.voiceAction();
}
We have a class Animal as a base class from which we have derived a subclass dog. Class dog inherits
all the members of Animal class and can be extended to include its own properties, as seen from the
output.
Multiple inheritance is a type of inheritance in which a class derives from more than one classes. As
shown in the above diagram, class C is a subclass that has class A and class B as its parent.
In a real-life scenario, a child inherits from its father and mother. This can be considered as an example of
multiple inheritance.
Roll No: 25
Total marks: 120
Average marks: 40
In the above example, we have three classes i.e. student_marks, cocurricular_marks, and Result. The
class student_marks reads the subject mark for the student. The class cocurricular_marks reads the
student’s marks in co-curricular activities.
The Result class calculates the total_marks for the student along with the average marks.
In this model, Result class is derived from student_marks and cocurricular_marks as we calculate Result
from the subject as well as co-curricular activities marks.
Diamond problem
Diamond Problem is pictorially represented below:
Here, we have a child class inheriting two classes Father and Mother. These two classes, in turn, inherit
the class Person.
As shown in the figure, class Child inherits the traits of class Person twice i.e. once from Father and the
second time from Mother. This gives rise to ambiguity as the compiler fails to understand which way to
go.
Since this scenario arises when we have a diamond-shaped inheritance, this problem is famously called
“The Diamond Problem”.
The Diamond problem implemented in C++ results in ambiguity error at compilation. We can resolve this
problem by making the root base class as virtual. We will learn more about the “virtual” keyword in our
upcoming tutorial on polymorphism.
#include <iostream>
#include <string>
using namespace std;
class Animal
{
string name="";
public:
int tail=1;
int legs=4;
};
class Dog : public Animal
{
public:
void voiceAction()
{
cout<<"Barks!!!";
}
};
class Puppy:public Dog{
public:
void weeping()
{
cout<<"Weeps!!";
}
};
int main()
{
Puppy puppy;
cout<<"Puppy has "<<puppy.legs<<" legs"<<endl;
cout<<"Puppy has "<<puppy.tail<<" tail"<<endl;
cout<<"Puppy ";
puppy.voiceAction();
cout<<" Puppy ";
puppy.weeping();
}
Output:
Puppy has 4 legs
Puppy has 1 tail
Puppy Barks!!! Puppy Weeps!!
Here we modified the example for Single inheritance such that there is a new class Puppy which inherits
from the class Dog that in turn inherits from class Animal. We see that the class Puppy acquires and uses
the properties and methods of both the classes above it.
Hybrid inheritance is usually a combination of more than one type of inheritance. In the above
representation, we have multiple inheritance (B, C, and D) and multilevel inheritance (A, B and D) to get a
hybrid inheritance.
int main(){
result res;//object//
res.getstudent();
res.getmarks();
res.getsports();
res.display();
return 0;
}
Output:
Enter student Id and student name 25 Ved
Enter 3 subject marks:89 88 87
Enter sports marks:40
Total marks =264
Average marks =88
Average + Sports marks =128
Here we have four classes i.e. Student, Marks, Sports, and Result. Marks are derived from the student
class. The class Result derives from Marks and Sports as we calculate the result from the subject marks
as well as sports marks.
The output is generated by creating an object of class Result that has acquired the properties of all the
three classes.
Note that in hybrid inheritance as well, the implementation may result in “Diamond Problem” which can be
resolved using “virtual” keyword as mentioned previously.
In hierarchical inheritance, more than one class inherits from a single base class as shown in the
representation above. This gives it a structure of a hierarchy.