Write a C++ program to demonstrate multilevel inheritance. The program should print the value of a, b, c declared in differentclasses, value a, is declared as private in the base class, value b is declared as private in class B, with multilevel,class B becomes base class for class C and derived class for A and finally value c declared as private in Class C whereClass C is a derived class and B is a base class. Create an object of the final class C to print all the values
#include <iostream>
using namespace std;
class A{
int a = 5;
public:
A(){}
protected:
int getA(){
return a;
}
};
class B: public A{
int b = 10;
public:
B(): A(){}
protected:
int getB(){
return b;
}
};
class C: public B{
int c = 15;
public:
C(): B(){}
void display(){
cout<<"Value of a: "<<getA()<<endl;
cout<<"Value of b: "<<getB()<<endl;
cout<<"Value of c: "<<c;
}
};
int main(){
C c;
c.display();
return 0;
}
Comments
Leave a comment