1

Consider the sample code below:

#include <iostream>

using namespace std;

class A
{
    private:
        static int a;
        int b;

    protected:

    public:

        A() : b(0) {}

        void modify()
        {
            a++;
            b++;
        }

        void display()
        {
            cout << a <<"\n";
            cout << b <<"\n";
        }

};

int A::a=0;

class B : public A {

    private:
        int b;

    public:
        B(): b(5)
        {
        }

};

int main()
{
    A ob1;
    B ob2;
    ob1.display();
    ob2.display();

    return 0;

}

In the code above, the class A has a private data member band class B also has a private data member b. The function display() is used to display the data members. When i invoke display() using ob1.display(), display() accesses the private data member b of class A. I understand that. But when i invoke display using ob2.display, which b does display() access? Is it the b of class A or b of class B? Kindly explain the why it accesses class A's b or class B's b

nitin_cherian
  • 6,405
  • 21
  • 76
  • 127

3 Answers3

3

It will access A::b. The display method implementation in class A has no clue about the existence of B::b at all, let alone using it. For all intents and purposes, B::b is separate from A::b. Only in the scope of B itself, the name conflict makes b refer to B::b, hiding A::b. Member variables cannot be virtual.

Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
  • Thanks : So what is the use of `class B` inheriting `class A's` `display()` function. `class B` has inherited the function `display()` from `class A` and so i expected `display()` to access `class B's b` – nitin_cherian Jan 05 '12 at 02:55
  • 1
    @LinuxPenseur There are a lot of uses. This is simply not one of them. To achieve what you want, you could either not introduce a new variable and initialize the base class `b` via a base constructor call, or introduce a `virtual` method in base class, which returns the value of `b` in base class and override it to return the value of `b` in the derived class. – Mehrdad Afshari Jan 05 '12 at 02:57
  • 1
    Or simply have a method in derived class which accesses derived class members and propogates the call further by calling base class method which accesses the base class members. – Alok Save Jan 05 '12 at 03:15
2

ob2.display() will access the derived class member.
The member function call is always evaluated on this, this->display() the this in case points to object of your Base class and hence any reference to b inside the display() function is evaluated as this->b which is b of the Base class.

This is because display() of the Base class has no knowledge of whether any other class derives from it. Base class is always independent of the Derived class. To solve a problem the uual pattern followed is to provide a display() method in the Derived class which then in turn calls the dsiplay() method of the Base class.

void B::display()
{
    //cout << a <<"\n";
    cout << b <<"\n";
    A::display();    
}
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • Please note that display function is in class A, in case you have missed it. Could you rephrase your answer based on it. I say this because ob2.display does not seem to access the derived class member. – nitin_cherian Jan 05 '12 at 03:02
  • Now i have the doubt:). why is `this` pointer in the case of `ob2.display()` pointing to object of Base class? ob2 is the object of the derived class. – nitin_cherian Jan 05 '12 at 03:09
  • @LinuxPenseur: Becasue `display()` is member of the Base class.The call to `display()` is resolved in the scope of Base class `A`.The base class knows nothing about any other classes that may derive from it,if you think of it, it makes sense that there is no such reverse dependency.The `b` in derived class will only be referred when in the scope of the derived class. – Alok Save Jan 05 '12 at 03:18
0

It is class A. A::display() cannot access B private members.

Mahesh
  • 34,573
  • 20
  • 89
  • 115
  • Answered this part of question - "But when i invoke display using ob2.display, which b does display() access?" – Mahesh Jan 05 '12 at 03:25