C++ | Inheritance | Question 13
#include<iostream> using namespace std; class Base1 { public: char c; }; class Base2 { public: int c; }; class Derived: public Base1, public Base2 { public: void show() { cout << c; } }; int main(void) { Derived d; d.show(); return 0; } |
chevron_right
filter_none
(A) Compiler Error in “cout << c;"
(B) Garbage Value
(C) Compiler Error in “class Derived: public Base1, public Base2”
Answer: (A)
Explanation: The variable ‘c’ is present in both super classes of Derived. So the access to ‘c’ is ambiguous. The ambiguity can be removed by using scope resolution operator.
#include<iostream> using namespace std; class Base1 { public: char c; }; class Base2 { public: int c; }; class Derived: public Base1, public Base2 { public: void show() { cout << Base2::c; } }; int main(void) { Derived d; d.show(); return 0; } |
chevron_right
filter_none
Recommended Posts:
- C++ | Inheritance | Question 2
- C++ | Inheritance | Question 10
- C++ | Inheritance | Question 3
- C++ | Inheritance | Question 1
- C++ | Inheritance | Question 8
- C++ | Inheritance | Question 9
- C++ | Inheritance | Question 4
- C++ | Inheritance | Question 14
- C++ | Inheritance | Question 11
- C++ | Inheritance | Question 5
- C++ | Inheritance | Question 6
- C++ | Inheritance | Question 12
- C++ | Inheritance | Question 7
- Java | Inheritance | Question 7
- Java | Inheritance | Question 9



