What is the output of C++ program given below?
#include<iostream>
using namespace std;
class Test
{
protected:
int a=10,b=5;
public:
virtual int func()=0;
};
class Imp:public Test
{
public:
int func()
{
int x=++a*b++;
return x;
}
};
int main()
{
Imp i;
cout<<i.func();
}
-
60
-
66
-
50
-
55
-
Compile time error
D
Correct answer
Explanation
This program describes about abstract class.
virtual int func()=0; is a pure virtual function hence Test class becomes abstract class.
Implementation of func() is present in Imp class where we are inheriting the abstract class Test into Imp class.
int x=++a*b++;
Initially a=10,b=5;
++a is pre-increment which makes value of 'a' as 11 & b++ is post-increment , until completion the statement execution 'b' value is 5. Only after completing the execution of the statement the value of 'b' is incremented.
Hence 11*5=55:
This option is correct.