C++ | Inheritance | Question 13

filter_none

edit
close

play_arrow

link
brightness_4
code

#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


(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.

filter_none

edit
close

play_arrow

link
brightness_4
code

#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



Quiz of this Question



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.