Write a Program to demonstrate multilevel Inheritance. Create class `A` with integer `x`, class `B` derives from `A` and has integer `y`, class `C` derives from `B` and calculates product of `x` and `y`.

SOLUTION....


#include <iostream>
using namespace std;

// Base Class A
class A {
protected:
    int x;
public:
    void setX(int a) {
        x = a;
    }
};

// Derived Class B from A
class B : public A {
protected:
    int y;
public:
    void setY(int b) {
        y = b;
    }
};

// Derived Class C from B
class C : public B {
public:
    void displayProduct() {
        cout << "Value of x: " << x << endl;
        cout << "Value of y: " << y << endl;
        cout << "Product (x * y): " << (x * y) << endl;
    }
};

// Main function
int main() {
    C obj;

    int a, b;
    cout << "Enter value of x: ";
    cin >> a;
    cout << "Enter value of y: ";
    cin >> b;

    obj.setX(a);
    obj.setY(b);

    cout << "\n--- Multilevel Inheritance Example ---\n";
    obj.displayProduct();

    return 0;
}
				

OUTPUT

Leave a Reply

Your email address will not be published. Required fields are marked *