Skip to content
Related Articles

Related Articles

Improve Article

C++ | Inheritance | Question 13

  • Difficulty Level : Medium
  • Last Updated : 28 Jun, 2021
Geek Week




#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;
}

(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;
}


Quiz of this Question

Take a step-up from those “Hello World” programs. Learn to implement data structures like Heap, Stacks, Linked List and many more! Check out our Data Structures in C course to start learning today.



My Personal Notes arrow_drop_up
Recommended Articles
Page :