C++ program for multilevel inheritance

In this program, we will create a base class student, intermediate class marks, and child class total. So the child class is inheriting data of marks class and marks class inheriting from student class.

Code for c++ program for multilevel inheritance

//c++ program for multilevel inheritance

#include <iostream>
using namespace std;
class student //grandfather class(base)
{

protected:
  int roll_num;

public:
  void get_data(int a)
  {
    roll_num = a;
  }

  void put_data()
  {
    cout << "roll_num =  " << roll_num << endl;
  }
};

class marks : public student // father class(intermediate base)
{

protected:
  int sub1;
  int sub2;

public:
  void get_data(int a, int b)
  {
    sub1 = a;
    sub2 = b;
  }

  void put_data()
  {
    cout << "sub1 =  " << sub1 << endl;
    cout << "sub2 =  " << sub2 << endl;
  }
};

class total : public marks // child class
{
  int sum;

public:
  void get_data(int r, int a, int b)
  {

    student::get_data(r); // inherited from base class
    marks::get_data(a, b); // inheritated from intermediate base class
    sum = a + b;
  }

  void put_data()

  {
    student::put_data();
    marks::put_data();
    cout << "total = " << sum << endl;
  }
};

int main()
{
  total s1;
  s1.get_data(12, 45, 60);
  s1.put_data();
  return 0;
}

Output

roll_num = 12
sub1 = 45
sub2 = 60
total = 105

Read: C++ Bank Application Program Using Classes And Object


If You Like This Page Then Make Sure To Follow Us on Facebook, G News and Subscribe Our YouTube Channel. We will provide you updates daily.
Shares
facebook sharing button Share
twitter sharing button Tweet
whatsapp sharing button Share
telegram sharing button Share
pinterest sharing button Pin

Leave a Comment