Unit 3
Unit 3
3.1 Inheritance
One of the most important concepts in object-oriented programming is
that of inheritance. Inheritance allows us to define a class in terms of
another class, which makes it easier to create and maintain an
application. This also provides an opportunity to reuse the code
functionality and fast implementation time.
When creating a class, instead of writing completely new data members
and member functions, the programmer can designate that the new class
should inherit the members of an existing class. This existing class is
called the base class, and the new class is referred to as the derived
class.
The idea of inheritance implements the is a relationship. For example,
mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as well
and so on.
Base and Derived Classes
A class can be derived from more than one classes, which means it can
inherit data and functions from multiple base classes. To define a
derived class, we use a class derivation list to specify the base class(es).
A class derivation list names one or more base classes and has the form
class derived-class: access-specifier base-class
Where access-specifier is one of public, protected, or private, and
base-class is the name of a previously defined class. If the access-
specifier is not used, then it is private by default.
Consider a base class Shape and its derived class Rectangle as follows
#include<iostream>
usingnamespace std;
// Base class
classShape
{
public:
voidsetWidth(int w){
width = w;
}
voidsetHeight(int h){
height = h;
}
protected:
int width;
int height;
};
classRectangle:publicShape // Derived class
{
public:
intgetArea(){
return(width * height);
}
};
int main(void)
{
RectangleRect;
Rect.setWidth(5);
Rect.setHeight(7);
// Print the area of the object.
cout<<"Total area: "<<Rect.getArea()<<endl;
return0;
}
When the above code is compiled and executed, it produces the
following result –
Total area: 35
Access Control and Inheritance
A derived class can access all the non-private members of its base class.
Thus base-class members that should not be accessible to the member
functions of derived classes should be declared private in the base class.
We can summarize the different access types according to - who can
access them in the following way −
Access public protected Private
Same class yes yes Yes
Derived classes yes yes No
Outside classes yes no No
A derived class inherits all base class methods with the following
exceptions −
Constructors, destructors and copy constructors of the base class.
Overloaded operators of the base class.
The friend functions of the base class.
3.2 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.
C++ supports five types of inheritance:
Single inheritance
Multiple inheritance
Hierarchical inheritance
Multilevel inheritance
Hybrid inheritance
3.2.1 Single Inheritance
Single inheritance is defined as the inheritance in which a derived class
is inherited from the only one base class.
Where 'A' is the base class, and 'B' is the derived class.
Single Level Inheritance Example: Inheriting Fields
When one class inherits another class, it is known as single level
inheritance. Let's see the example of single level inheritance which
inherits the fields only.
#include <iostream>
using namespace std;
class Account {
public:
float salary = 60000;
};
class Programmer: public Account {
public:
float bonus = 5000;
};
int main(void) {
Programmer p1;
cout<<"Salary: "<<p1.salary<<endl;
cout<<"Bonus: "<<p1.bonus<<endl;
return 0;
}
Output :
Salary: 60000
Bonus: 5000
In the above example, Employee is the base class and Programmer is the
derived class.
Single Level Inheritance Example: Inheriting Methods
Let's see another example of inheritance in C++ which inherits methods
only.
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout<<"Eating..."<<endl;
}
};
class Dog: public Animal
{
public:
void bark(){
cout<<"Barking...";
}
};
int main(void) {
Dog d1;
d1.eat();
d1.bark();
return 0;
}
Output:
Eating...
Barking...
Example
#include<iostream>
using namespace std;
class A
{
int a = 4;
int b = 5;
public:
int mul()
{
int c = a*b;
return c;
}
};
class B : private A
{
public:
void display()
{
int result = mul();
std::cout <<"Multiplication of a and b is : "<<result<< std::endl;
}
};
int main()
{
B b;
b.display();
return 0;
}
Ouput:
Multiplication of a and b is : 20
3.2.1.1 To make a Private Member Inheritable
The private member is not inheritable. If we modify the visibility mode
by making it public, but this takes away the advantage of data hiding.
C++ introduces a third visibility modifier, i.e., protected. The member
which is declared as protected will be accessible to all the member
functions within the class as well as the class immediately derived from
it.
Visibility modes can be classified into three categories:
Output:
error: reference to 'display' is ambiguous
display();
The above issue can be resolved by using the class resolution operator
with the function. In the above example, the derived class code can be
rewritten as:
class C : public A, public B
{
void view()
{
A :: display(); // Calling the display() function of class A.
B :: display(); // Calling the display() function of class B.
}
};
3.2.4 Hybrid Inheritance
Hybrid inheritance is a combination of more than one type of
inheritance.
Example:
#include <iostream>
using namespace std;
class A
{
protected:
int a;
public:
void get_a()
{
std::cout << "Enter the value of 'a' : " << std::endl;
cin>>a;
}
};
class B : public A
{
protected:
int b;
public:
void get_b()
{
std::cout << "Enter the value of 'b' : " << std::endl;
cin>>b;
}
};
class C
{
protected:
int c;
public:
void get_c()
{
std::cout << "Enter the value of c is : " << std::endl;
cin>>c;
}
};
class D : public B, public C
{
protected:
int d;
public:
void mul()
{
get_a();
get_b();
get_c();
std::cout << "Multiplication of a,b,c is : " <<a*b*c<< std::endl;
}
};
int main()
{
D d;
d.mul();
return 0;
}
Output:
Enter the value of 'a' :
10
Enter the value of 'b' :
20
Enter the value of c is :
30
Multiplication of a,b,c is : 6000
3.2.5 Hierarchical Inheritance
Hierarchical inheritance is defined as the process of deriving more than
one class from a base class.