Examveda

What will be the output of the following C++ code?
#include <iostream>
using namespace std;
class p 
{
    protected:
    int width, height;
    public:
    void set_values (int a, int b)
    {
        width = a; height = b; 
    }
    virtual int area (void) = 0;
};
class r: public p
{
    public:
    int area (void)
    { 
       return (width * height);
    }
};
class t: public p 
{
    public:
    int area (void)
    {
        return (width * height / 2); 
    }
};
int main () 
{
    r rect;
    t trgl;
    p * ppoly1 = &rect;
    p * ppoly2 = &trgl;
    ppoly1->set_values (4, 5);
    ppoly2->set_values (4, 5);
    cout << ppoly1 -> area() ;
    cout << ppoly2 -> area();
    return 0;
}

A. 1020

B. 20

C. 10

D. 2010

Answer: Option D


Join The Discussion

Related Questions on C plus plus miscellaneous

What is the difference between '++i' and 'i++' in C++?

A. None of the above

B. They both have the same effect

C. '++i' increments the value of 'i' before returning it, while 'i++' increments the value of 'i' after returning it

D. '++i' increments the value of 'i' after returning it, while 'i++' increments the value of 'i' before returning it