Home » C++ Programming » Classes & Objects » Question
  1. What is the output of this program?
    #include <iostream>
    using namespace std;
    class Polygon
    {
    protected:
    int W, H;
    public:
    void set_values(int width, int height)
    {
    W = width; H = height;
    }
    };
    class Coutput
    {
    public:
    void output(int i);
    };
    void Coutput::output(int i)
    {
    cout << i <<" ";
    }
    class Rectangle:public Polygon, public Coutput
    {
    public:
    int area()
    {
    return(W * H);
    }
    };
    class Triangle:public Polygon, public Coutput
    {
    public:
    int area()
    {
    return(W * H / 2);
    }
    };
    int main()
    {
    Rectangle Rect;
    Triangle trgl;
    Rect.set_values(5, 2);
    trgl.set_values(5, 2);
    Rect.output(Rect.area());
    trgl.output(trgl.area());
    return 0;
    }
    1. 5 2
    2. 2 5
    3. 10 5
    4. 5 10
    5. None of these
Correct Option: C

In this program, We are calculating the area of rectangle and
triangle by using multilevel inheritance.



Your comments will be displayed only after manual approval.