Multiple choice

What is the output of C++ program given below?

#include<iostream> using namespace std; class Parent1 { public: int a=10; }; class Parent2 { public: int a=5; }; class Child:public Parent1,public Parent2 { public: int func() { return a; } }; int main() { Child ch; cout<<ch.func(); }

  1. 10

  2. 5

  3. 10 5

  4. Compile time error

  5. None of the above

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

 When we compile the given program it generates compile time error.

Because integer variable 'a' is defined in both Parent classes. Child class inherits both Parent class & child class function func() accesses integer variable 'a' which leads to ambigious situation.

Error generated at linereturn a; Error message : reference to 'a' is ambigious This option is true.