Home » C++ Programming » Classes & Objects » Question
  1. What is the output of this program?
    #include <iostream>
    using namespace std;
    class FirstClass
    {
    protected:
    int width, height;
    public:
    void set_values (int x, int y)
    {
    width = x; height = y;
    }
    virtual int area (void) = 0;
    };
    class SecondClass: public FirstClass
    {
    public:
    int area (void)
    {
    return (width * height);
    }
    };
    class ThirdClass: public FirstClass
    {
    public:
    int area (void)
    {
    return (width * height / 2);
    }
    };
    int main ()
    {
    SecondClass Rectangle;
    ThirdClass trgl;
    FirstClass * ppoly1 = &Rectangle;
    FirstClass * ppoly2 = &trgl;
    ppoly1->set_values (3, 6);
    ppoly2->set_values (3, 6);
    cout << ppoly1 -> area() <<" ";
    cout << ppoly2 -> area();
    return 0;
    }
    1. 3 6
    2. 6 3
    3. 9 18
    4. 18 9
    5. None of these
Correct Option: D

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



Your comments will be displayed only after manual approval.