C++ Program to add two numbers using multilevel inheritance


In this program, You will learn how to add two numbers using multilevel inheritance in C++.


10 + 10 => 20

10 + 20 => 30


Example: How to add two numbers using multilevel inheritance in C++

Run it Now
#include<iostream>
using namespace std;

class First {
public:
   int a, b, s;

   void input() {
       cout << "Enter two numbers :";
       cin >> a>>b;
   }
};

class Second : public First {
public:

   void add() {
       s = a + b;
   }
};

class Third : public Second {
public:

   void display() {
     cout << "\nSum is :" << s;
   }
};

int main() {

   Third th;
   th.input();
   th.add();
   th.display();

   return 0;
}

Output:

Enter two numbers :12 13
Sum is :25