What is the output of C++ program given below?
#include<iostream>
using namespace std;
class Parent
{
public:
int a=10,b=20;
};
class Middle:protected Parent
{
public:
Middle()
{
a++;
b--;
}
};
class Child:private Middle
{
public:
int hello()
{
return a*b;
}
};
int main()
{
Child ch;
cout<<ch.hello();
}
-
200
-
209
-
189
-
Compile time error
-
None of the above
B
Correct answer
Explanation
class derived_class:access_specifier base_class
We have to understand above syntax inorder to answer this question.
when access_specifier is protected: all public & protected members of base_class becomes protected type for derived_class.
class Middle:protected Parent //int a,b are now protected
Where Middle() constructor increments value of 'a' * decrements 'b'
Hence a=11,b=19
when access_specifier is private: all public & protected of base_class becomes private members for derived_class.
So, int a,b are now private for class Child
Hence they are accessible to derived_class from the base_class. Which prints a*b=11*19=206.