In this C++ tutorial, you will learn about Inheritance (an Object Oriented Programming concept), how to implement inheritance between classes, with examples.

C++ Inheritance

In C++, Inheritance is a process in which a class (derived) can acquire the properties and functions of another class (base).

For example, consider two class A and B. If B is declared as derived class from A, then B can access the properties and functions defined in A. But, A cannot access the properties and functions of B. A is base class and B is derived class. B inherits A.

Syntax

The syntax to declare that class B inherits class A is

class A {
    //properties and functions
};

class B: public A {
    //properties and functions
};

Example

In the following example, we define two classes: A and B. A is base class and B is derived class.

Instance of B can access the properties and methods of its base class A.

C++ Program

</>
Copy
#include <iostream>
using namespace std;

class A {
public:
    int x = 22;
    void displayMsg(string msg) {
        cout << "Message : " << msg << endl;
    }
};

class B: public A {
    
};

int main() {
    B b = B();
    cout << b.x << endl;
    b.displayMsg("Hello World");
}

Output

22
Message : Hello World
Program ended with exit code: 0

Accessibility of Parent Class Members

1. Public/Protected

Public and protected members of the derived class can access all the public and protected members of the base class.

Public members of derived class access public members of base class.

</>
Copy
class A {
public:
    void baseClassFun() {}
};

class B: public A {
public:
    void derivedClassFun() {
        baseClassFun();
    }
};

Protected members of derived class access public members of base class.

</>
Copy
class A {
public:
    void baseClassFun() {
    }
};

class B: public A {
protected:
    void derivedClassFun() {
        baseClassFun();
    }
};

Public members of the derived class access protected members of the base class.

</>
Copy
class A {
public:
    void baseClassFun() {}
};

class B: public A {
public:
    void derivedClassFun() {
        baseClassFun();
    }
};

Protected members of the derived class access protected members of the base class.

</>
Copy
class A {
protected:
    void baseClassFun() {}
};

class B: public A {
public:
    void derivedClassFun() {
        baseClassFun();
    }
};

2. Private – Any Access (Base – Derived)

Public/Protected/Private members of the derived class can never access private members of the base class. Private members of a class can be accessed only within the same class.

</>
Copy
class A {
private:
    void baseClassFun() {
    }
};

class B: public A {
public:
    void derivedClassFun() {
        baseClassFun();
    }
};

In summary to the accessibility of class members,

  • public members are accessible everywhere.
  • private members are accessible within that class only.
  • protected class members are accessible within the class and in the classes that inherit them.

C++ Multiple Inheritance

A derived class can inherit one or more base classes. If a class inherits more than one class, then that is called Multiple Inheritance.

In the following program, A and X are base classes. B is derived class and inherits both A and X. B inherits more than one class.

C++ Program

</>
Copy
class A {
public:
    void funA() {}
};

class X {
public:
    void funX() {}
};

class B: public A, public X {
public:
    void funB() {
        funA();
        funX();
    }
};

int main() {
    B b = B();
    b.funB();
    b.funA();
    b.funX();
}

Conclusion

In this C++ Tutorial, we learned what Inheritance is, how to use inheritance in C++, with the help of examples.