Class 10 | Inheritance | C++ Notes

Inheritance

The capability of a class to derive properties and characteristics from another class is called Inheritance.

Inheritance is one of the most important features of Object-Oriented Programming.

Inheritance is a process in which new classes are created from existing classes.

The new class created is called the Derived Class or Child Class, while the existing class is called the Base Class or Parent Class.

When a derived class inherits a base class, it inherits the properties of the base class without changing the properties of the base class.

The derived class can also add new features of its own. These new features do not affect the base class.

Sub Class

The class that inherits properties from another class is called a Subclass or Derived Class.

Super Class

The class whose properties are inherited by a subclass is called a Base Class or Superclass.

Advantages of Inheritance

1. Code Reusability

One of the primary benefits of inheritance is code reuse.

When a class inherits from another class, it automatically gains access to the accessible data members and member functions of the base class.

This eliminates the need to rewrite code, resulting in more efficient development and easier maintenance.

2. Polymorphism

Inheritance enables polymorphic behavior.

It allows objects of derived classes to be treated as objects of their base class.

This helps in writing flexible and extensible programs.

3. Extensibility

Derived classes can add new functionality or modify existing behavior without modifying the base class.

This promotes modular program design.

4. Hierarchy Organization

Inheritance allows the creation of class hierarchies.

Classes can be organized according to their relationships, which helps represent real-world relationships and complex systems.

5. Abstraction

Inheritance supports abstraction by allowing developers to define base classes that provide common interfaces or behavior.

Derived classes can then implement specific functionality while inheriting common characteristics.

6. Encapsulation

Inheritance can be used together with encapsulation to hide implementation details and expose only a well-defined interface.

7. Facilitates Method Overriding

Inheritance allows derived classes to override the behavior of base class methods.

This helps customize functionality in different subclasses.

8. Easier Maintenance

Inheritance promotes code reuse, modularity, and consistency.

Changes made to the base class can be available to its derived classes.

Base Class and Derived Class

Base Class

A Base Class, also called a Parent Class or Superclass, serves as the foundation for other classes.

It defines common attributes and behaviors that can be shared among derived classes.

Syntax:

class BaseClass {

// Data members

// Member functions

};

Derived Class

A Derived Class, also called a Child Class or Subclass, inherits properties and behaviors from its base class.

It can add new features or override existing ones.

Syntax:

class DerivedClass : accessSpecifier BaseClass {

// Additional data members

// Additional member functions

};

Modes of Inheritance

There are three modes of inheritance:

  1. Public Mode
  2. Protected Mode
  3. Private Mode

1. Public Mode

If a subclass is derived using public inheritance:

  • Public members of the base class remain public in the derived class.
  • Protected members of the base class remain protected in the derived class.

2. Protected Mode

If a subclass is derived using protected inheritance:

  • Public members of the base class become protected in the derived class.
  • Protected members of the base class remain protected.

3. Private Mode

If a subclass is derived using private inheritance:

  • Public members of the base class become private in the derived class.
  • Protected members of the base class also become private.

Types of Inheritance

There are five major types of inheritance:

  1. Single Inheritance
  2. Multiple Inheritance
  3. Multilevel Inheritance
  4. Hierarchical Inheritance
  5. Hybrid Inheritance

1. Single Inheritance

Single inheritance refers to a situation where a derived class inherits from only one base class.

It is one of the most common forms of inheritance.

The derived class inherits the non-private members of the base class.

It can directly access public and protected members according to the inheritance mode.

Private members of the base class cannot be directly accessed by the derived class.

Syntax:

class BaseClass {

// Base class members

};

class DerivedClass : accessSpecifier BaseClass {

// Derived class members

};

Example: Single Inheritance

#include <iostream>

using namespace std;

class NepalEnotes {

public:

void display() {

cout << “This is NepalEnotes\n”;

}

};

class NexuCorner : public NepalEnotes {

public:

void Dhoni() {

cout << “Dhoni function in NexuCorner\n”;

}

};

int main() {

NexuCorner obj;

obj.display();

obj.Dhoni();

return 0;

}

2. Multiple Inheritance

Multiple Inheritance is a feature of C++ in which a class can inherit from two or more base classes.

The constructors of inherited classes are called in the same order in which the base classes are inherited.

Syntax:

class A {

…

};

class B {

…

};

class C : public A, public B {

…

};

Example 1: Multiple Inheritance

#include <iostream>

using namespace std;

class A {

public:

A() {

cout << “A’s constructor called” << endl;

}

};

class B {

public:

B() {

cout << “B’s constructor called” << endl;

}

};

class C : public B, public A {

public:

C() {

cout << “C’s constructor called” << endl;

}

};

int main() {

C Obj;

return 0;

}

Example 2: Multiple Inheritance

#include <iostream>

using namespace std;

// Base class 1

class NepalEnotes {

public:

NepalEnotes() {

cout << “NepalEnotes constructor called” << endl;

}

void displayNepalEnotes() {

cout << “Displaying NepalEnotes” << endl;

}

};

// Base class 2

class Antoter {

public:

Antoter() {

cout << “Antoter constructor called” << endl;

}

void displayAntoter() {

cout << “Displaying Antoter” << endl;

}

};

// Derived class

class NexuCorner : public NepalEnotes, public Antoter {

public:

NexuCorner() {

cout << “NexuCorner constructor called” << endl;

}

void displayNexuCorner() {

cout << “Displaying NexuCorner” << endl;

}

};

int main() {

NexuCorner obj;

obj.displayNepalEnotes();

obj.displayAntoter();

obj.displayNexuCorner();

return 0;

}

Ambiguity Error in Multiple Inheritance

In multiple inheritance, a class may inherit from two or more base classes containing functions with the same name.

If the derived class object tries to access that function, the compiler may not know which base class function should be called.

This situation is called an ambiguity error.

Example: Ambiguity Error

#include <iostream>

using namespace std;

class A {

public:

void func() {

cout << “I am in class A” << endl;

}

};

class B {

public:

void func() {

cout << “I am in class B” << endl;

}

};

class C : public A, public B {

};

int main() {

C obj;

obj.func();

return 0;

}

In the above example, both class A and class B contain a function called func().

Therefore, the compiler cannot determine which function should be called.

Solution of Ambiguity Error in Multiple Inheritance

We can solve ambiguity by using the scope resolution operator (::) to specify the base class whose function we want to call.

Example

#include <iostream>

using namespace std;

class NepalEnotes {

public:

void display() {

cout << “Displaying from NepalEnotes” << endl;

}

};

class Antoter {

public:

void display() {

cout << “Displaying from Antoter” << endl;

}

};

class NexuCorner : public NepalEnotes, public Antoter {

public:

void display() {

cout << “Displaying from NexuCorner” << endl;

}

};

int main() {

NexuCorner obj;

obj.NepalEnotes::display();

obj.Antoter::display();

return 0;

}

3. Multilevel Inheritance

Multilevel Inheritance is the process of deriving a class from another derived class.

When one class inherits another class and is further inherited by another class, it is called multilevel inheritance.

For example:

Class A → Class B → Class C

Syntax:

class A {

// Base class

};

class B : access_specifier A {

// Derived from A

};

class C : access_specifier B {

// Derived from B

};

Example 1: Multilevel Inheritance

#include <iostream>

using namespace std;

class A {

public:

int a;

void get_A_data() {

cout << “Enter value of a: “;

cin >> a;

}

};

class B : public A {

public:

int b;

void get_B_data() {

cout << “Enter value of b: “;

cin >> b;

}

};

class C : public B {

private:

int c;

public:

void get_C_data() {

cout << “Enter value of c: “;

cin >> c;

}

void sum() {

int ans = a + b + c;

cout << “sum: ” << ans;

}

};

int main() {

C obj;

obj.get_A_data();

obj.get_B_data();

obj.get_C_data();

obj.sum();

return 0;

}

Example 2: Multilevel Inheritance

#include <iostream>

using namespace std;

// Base class

class NepalEnotes {

public:

NepalEnotes() {

cout << “NepalEnotes constructor called” << endl;

}

void displayNepalEnotes() {

cout << “Displaying NepalEnotes” << endl;

}

};

// Derived class 1

class Nexus : public NepalEnotes {

public:

Nexus() {

cout << “Nexus constructor called” << endl;

}

void displayNexus() {

cout << “Displaying Nexus” << endl;

}

};

// Derived class 2

class NexuCorner : public Nexus {

public:

NexuCorner() {

cout << “NexuCorner constructor called” << endl;

}

void displayNexuCorner() {

cout << “Displaying NexuCorner” << endl;

}

};

int main() {

NexuCorner obj;

obj.displayNepalEnotes();

obj.displayNexus();

obj.displayNexuCorner();

return 0;

}

4. Hierarchical Inheritance

In Hierarchical Inheritance, more than one subclass inherits the properties of a single base class.

There is one base class and multiple derived classes.

Hierarchical inheritance forms a tree-like structure.

For example:

Class A → Class B
Class A → Class C

Syntax:

class A {

…

};

class B : access_specifier A {

…

};

class C : access_specifier A {

…

};

Example 1: Hierarchical Inheritance

#include <iostream>

using namespace std;

class A {

public:

void show_A() {

cout << “class A” << endl;

}

};

class B : public A {

public:

void show_B() {

cout << “class B” << endl;

}

};

class C : public A {

public:

void show_C() {

cout << “class C” << endl;

}

};

int main() {

B b;

cout << “calling from B: ” << endl;

b.show_B();

b.show_A();

C c;

cout << “calling from C: ” << endl;

c.show_C();

c.show_A();

return 0;

}

Example 2: Hierarchical Inheritance

#include <iostream>

using namespace std;

// Base class

class NepalEnotes {

public:

NepalEnotes() {

cout << “NepalEnotes constructor called” << endl;

}

void displayNepalEnotes() {

cout << “Displaying NepalEnotes” << endl;

}

};

// Derived class 1

class Nexus : public NepalEnotes {

public:

Nexus() {

cout << “Nexus constructor called” << endl;

}

void displayNexus() {

cout << “Displaying Nexus” << endl;

}

};

// Derived class 2

class NexuCorner : public NepalEnotes {

public:

NexuCorner() {

cout << “NexuCorner constructor called” << endl;

}

void displayNexuCorner() {

cout << “Displaying NexuCorner” << endl;

}

};

int main() {

Nexus obj1;

NexuCorner obj2;

obj1.displayNepalEnotes();

obj1.displayNexus();

obj2.displayNepalEnotes();

obj2.displayNexuCorner();

return 0;

}

5. Hybrid Inheritance

Hybrid Inheritance in C++ is a combination of two or more types of inheritance.

It can combine:

  • Single Inheritance
  • Multiple Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance

Hybrid inheritance provides flexibility in designing class hierarchies and improves code reuse and organization.

However, it can also introduce complexities such as:

  • Ambiguity
  • Diamond Problem

These issues may need to be handled using techniques such as virtual inheritance or explicit scope resolution.

Ways of Achieving Hybrid Inheritance

1. Single and Multiple Inheritance

A class can inherit from one base class using single inheritance and also inherit from multiple base classes using multiple inheritance.

2. Multilevel and Multiple Inheritance

One class can inherit from a base class, and another class can inherit from that derived class to form multilevel inheritance.

At the same time, a class may inherit from multiple classes.

3. Hierarchical and Multiple Inheritance

Multiple derived classes can inherit from a common base class, forming hierarchical inheritance.

These derived classes may also participate in multiple inheritance.

Example: Hybrid Inheritance Using Single and Multiple Inheritance

#include <iostream>

using namespace std;

class NepalEnotes {

public:

NepalEnotes() {

cout << “NepalEnotes constructor called” << endl;

}

void displayNepalEnotes() {

cout << “Displaying NepalEnotes” << endl;

}

};

class Nexus : public NepalEnotes {

public:

Nexus() {

cout << “Nexus constructor called” << endl;

}

void displayNexus() {

cout << “Displaying Nexus” << endl;

}

};

class NexuCorner : public NepalEnotes, public Nexus {

public:

NexuCorner() {

cout << “NexuCorner constructor called” << endl;

}

void displayNexuCorner() {

cout << “Displaying NexuCorner” << endl;

}

};

int main() {

NexuCorner obj;

obj.displayNepalEnotes();

obj.displayNexus();

obj.displayNexuCorner();

return 0;

}

Example: Hybrid Inheritance Using Multiple and Multilevel Inheritance

#include <iostream>

using namespace std;

class NepalEnotes {

public:

NepalEnotes() {

cout << “NepalEnotes constructor called” << endl;

}

void displayNepalEnotes() {

cout << “Displaying NepalEnotes” << endl;

}

};

class Nexus : public NepalEnotes {

public:

Nexus() {

cout << “Nexus constructor called” << endl;

}

void displayNexus() {

cout << “Displaying Nexus” << endl;

}

};

class NexuCorner : public NepalEnotes, public Nexus {

public:

NexuCorner() {

cout << “NexuCorner constructor called” << endl;

}

void displayNexuCorner() {

cout << “Displaying NexuCorner” << endl;

}

};

int main() {

NexuCorner obj;

obj.displayNepalEnotes();

obj.displayNexus();

obj.displayNexuCorner();

return 0;

}

Example: Hybrid Inheritance Using Multiple and Hierarchical Inheritance

#include <iostream>

using namespace std;

class NepalEnotes {

public:

NepalEnotes() {

cout << “NepalEnotes constructor called” << endl;

}

void displayNepalEnotes() {

cout << “Displaying NepalEnotes” << endl;

}

};

class Nexus : public NepalEnotes {

public:

Nexus() {

cout << “Nexus constructor called” << endl;

}

void displayNexus() {

cout << “Displaying Nexus” << endl;

}

};

class NexuCorner : public NepalEnotes, public Nexus {

public:

NexuCorner() {

cout << “NexuCorner constructor called” << endl;

}

void displayNexuCorner() {

cout << “Displaying NexuCorner” << endl;

}

};

int main() {

NexuCorner obj;

obj.displayNepalEnotes();

obj.displayNexus();

obj.displayNexuCorner();

return 0;

}

Discussion

Share a helpful question, idea, or explanation with other students.

Leave a Comment

Write a clear question, answer, or helpful explanation.
Your email will not be published.

Download Our Offline App

Study class-wise notes even when internet is not available. Get the app from Play Store.

Nepal eNotes offline app preview
Get it on Google Play