Inheritance
Inheritance
Inheritance: Derived and base classes, Class hierarchies, public, private, and protected derivations, constructors in derived
classes, destructors in derived classes, constructors invocation and data members initialization in derived classes, classes
within classes, virtual base class.
Inheritance in C++ is a mechanism in which one object acquires all the properties and behaviors of a parent object. It is an
important part of OOPs (Object Oriented programming system).
The idea behind inheritance in Java is that you can create new classes that are built upon existing classes. When you
inherit from an existing class, you can reuse methods and fields of the parent class. Moreover, you can add new methods
and fields in your current class also.
Inheritance represents the IS-A relationship which is also known as a parent-child relationship.
Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called a derived class, extended class, or
child class.
Super Class/Parent Class: Superclass is the class from where a subclass inherits the features. It is also called a base class or
a parent class.
Reusability: As the name specifies, reusability is a mechanism which facilitates you to reuse the fields and methods of the
existing class when you create a new class. You can use the same fields and methods already defined in the previous class.
class Super {
.....
.....
}
class Sub extends Super {
.....
.....
}
The extends keyword indicates that you are making a new class that derives from an existing class. The meaning
of "extends" is to increase the functionality.
Extensibility: The inheritance helps to extend the functionalities of a class. If you have a base class with some
functionalities, you can extend them by using the inheritance in the derived class.
Implantation of Method Overriding: Inheritance is required to achieve one of the concepts of Polymorphism which is
Method overriding.
Achieving Abstraction: Another concept of OOPs that is abstraction also needs inheritance.
Public Mode: If we derive a subclass from a public base class. Then the public member of the base class will become public
in the derived class and protected members of the base class will become protected in the derived class.
Protected Mode: If we derive a subclass from a Protected base class. Then both public members and protected members
of the base class will become protected in the derived class.
Private Mode: If we derive a subclass from a Private base class. Then both public members and protected members of the
base class will become Private in the derived class.
Types of Inheritance:-
Single inheritance
Multilevel inheritance
Multiple inheritance
Hierarchical inheritance
Hybrid inheritance
1. Single Inheritance: In single inheritance, a class is allowed to inherit from only one class.
i.e. one subclass is inherited by one base class only.
Example
#include<iostream>
using namespace std;
class Vehicle { // base class
public:
Vehicle()
{
cout << "This is a Vehicle\n";
}
};
class Car : public Vehicle { // sub class derived from a single base classes
};
int main()
{
// Creating object of sub class will
// invoke the constructor of base classes
Car obj;
return 0;
}
Output
This is a Vehicle
2. 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.
Example:
// multiple inheritance
#include <iostream>
using namespace std;
// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base classes.
Car obj;
return 0;
}
Output
This is a Vehicle
This is a 4 wheeler Vehicle
3. Multilevel Inheritance: In this type of inheritance, a derived class is created from another derived class.
// base class
class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};
// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base classes.
Car obj;
return 0;
}
Output
This is a Vehicle
Objects with 4 wheels are vehicles
Car has 4 Wheels
4. Hierarchical Inheritance: In this type of 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.
// base class
class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};
// main function
int main()
{
// Creating object of sub class will
// invoke the constructor of base class.
Car obj1;
Bus obj2;
return 0;
}
Output
This is a Vehicle
This is a Vehicle
5. Hybrid (Virtual) Inheritance: Hybrid Inheritance is implemented by combining more than one type of inheritance. For
example: Combining Hierarchical inheritance and Multiple Inheritance.
Below image shows the combination of hierarchical and multiple inheritances:
#include <iostream>
using namespace std;
// base class
class Vehicle {
public:
Vehicle() { cout << "This is a Vehicle\n"; }
};
// base class
class Fare {
public:
Fare() { cout << "Fare of Vehicle\n"; }
};
// first sub class
class Car : public Vehicle {
};
// second sub class
class Bus : public Vehicle, public Fare {
};
// main function
int main()
{
Bus obj2;
return 0;
}
Output
This is a Vehicle
Fare of Vehicle
In single inheritance, the derived While in multiple inheritance, the derived class
class uses the features of the single uses the joint features of the inherited base
2. base class. classes.
Single inheritance requires a small While multiple inheritance requires more run
run time as compared to multiple time as compared to single inheritance due to
3. inheritance due to less overhead. more overhead.
The below table summarizes the above three modes and shows the access specified of the members of the base class in the
subclass when derived in public, protected and private modes:
Constructors in Derived Class in C++
In C++, constructors in derived classes are special member functions used to initialize objects of derived classes. When you
create an object of a derived class, its constructor is called to initialize both the base class part and the derived class part
of the object. Constructors in derived classes can call constructors of their base classes explicitly to ensure proper
initialization of the inherited members.
Example:
#include <iostream>
// Base class
class Base {
public:
Base() {
std::cout << "Base constructor\n";
}
};
// Derived class
class Derived : public Base {
public:
Derived() {
std::cout << "Derived constructor\n";
}
};
int main() {
Derived d; // Creating an object of the derived class
return 0;
}
Let's break it down:
In this example, when you create an object of the Derived class (Derived d;), its constructor is called. The constructor of
the Derived class implicitly calls the constructor of the Base class (Base ()), ensuring that both the base class and the
derived class are properly initialized.
Destructors in Derived Class in C++
In C++, destructors in derived classes play a crucial role in ensuring proper cleanup of resources allocated by both the base
class and the derived class. A destructor in C++ is a special member function of a class that is automatically called when an
object of that class goes out of scope or is explicitly deleted. In the context of derived classes, a derived class can have its
own destructor in addition to the destructor of its base class.
Example:
#include <iostream>
class Base {
public:
Base() {
std::cout << "Base constructor called" << std::endl;
}
~Base() {
std::cout << "Base destructor called" << std::endl;
}
};
~Derived() {
std::cout << "Derived destructor called" << std::endl;
}
};
int main() {
Derived derivedObj;
return 0;
}
Why the base class’s constructor is called on creating an object of derived class?
To understand this you will have to recall your knowledge on inheritance. What happens when a class is inherited from
other? The data members and member functions of base class comes automatically in derived class based on the access
specified but the definition of these members exists in base class only. So when we create an object of derived class, all of
the members of derived class must be initialized but the inherited members in derived class can only be initialized by the
base class’s constructor as the definition of these members exists in base class only. This is why the constructor of base
class is called first to initialize all the inherited members.
#include <iostream>
using namespace std;
// base class
class Parent
{
public:
// sub class
class Child : public Parent
{
public:
// main function
int main() {
return 0;
}
Output:
Inside base class
Inside sub class
#include <iostream>
using namespace std;
// first base class
class Parent1
{
public:
// first base class's Constructor
Parent1()
{
cout << "Inside first base class" << endl;
}
};
// second base class
class Parent2
{
public:
Output:
Inside first base class
Inside second base class
Inside child class
Order of constructor and Destructor call for a given order of Inheritance
Classes within classes: A nested class is a class which is declared in another enclosing class. A nested class is a member
and as such has the same access rights as any other member. The members of an enclosing class have no special access to
members of a nested class; the usual access rules shall be obeyed.
Example
#include<iostream>
class A {
public:
class B {
private:
int num;
public:
void getdata(int n) {
num = n;
void putdata() {
};
};
int main() {
A :: B obj;
obj.getdata(9);
obj.putdata();
return 0;
Output
Nested classes in C++
The number is 9
class A {
public:
void show()
{
cout << "Hello form A \n";
}
};
class B : public A {
};
class C : public A {
};
Compile Errors:
prog.cpp: In function 'int main()':
prog.cpp:29:9: error: request for member 'show' is ambiguous
object.show();
^
prog.cpp:8:8: note: candidates are: void A::show()
void show()
^
prog.cpp:8:8: note: void A::show()
class A {
public:
int a;
A() // constructor
{
a = 10;
}
};
return 0;
}
Output
a = 10
Broad questions type: CPP Programming Inheritance based Questions Answers - EXAMRADAR