Home » C++ Programming » Inheritance » Question
  1. What is the output of this program?
    #include <iostream>
    using namespace std;
    class PolygonShape
    {
    protected:
    int W, H;
    public:
    void set_values (int num1, int num2)
    {
    W = num1; H = num2;}
    };
    class outputA
    {
    public:
    void output (int k);
    };
    void outputA::output (int k)
    {
    cout << k << endl;
    }
    class RectangleShape: public PolygonShape, public outputA
    {
    public:
    int Area ()
    {
    return (W * H);
    }
    };
    class triangleShape: public PolygonShape, public outputA
    {
    public:
    int Area()
    {
    return (W * H / 2);
    }
    };
    int main ()
    {
    RectangleShape RectObject;
    triangleShape TrgObject;
    RectObject.set_values (3, 4);
    TrgObject.set_values (3, 4);
    RectObject.output (RectObject.Area());
    TrgObject.output (TrgObject.Area());
    return 0;
    }
    1. 12
      6
    2. 3
      4
    3. Compilation Error
    4. Runtime Error
    5. None of these
Correct Option: A

We are using the multiple inheritance to find the area of rectangle and triangle.



Your comments will be displayed only after manual approval.