Answer to Question #192449 in C++ for Dunga

Question #192449

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


1
Expert's answer
2021-05-14T00:36:34-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment