0

I have just started learning about inheritance and I know that private members of base class are not inherited by the derived class.

But when I run the 'get_data1' function of the derived class, it returns the value of data1 from the base class even though I have defined a new 'data1' in the derived class. So shouldn't it return 3 instead of 5 as it is a member function of the derived class?

class base
{
    int data1;
    public:
        base()
        {
            data1=5;
        }
        int get_data1()
        {
            return data1;
        }
};

class derived : public base
{
    int data1;
    public:
        derived()
        {
            data1=3;
        }
        void printData()
        {
            cout<<get_data1()<<endl;
        }
};
int main()
{
    derived der1;
    der1.printData();

    return 0;
}
  • 1
    *"I know that private members of base class are not inherited by the derived class..."* **is wrong**. They are inherited. – Jason Sep 28 '22 at 16:24
  • 2
    `private` members are inherited just like any other. They're just not accessible from the child class. – Silvio Mayolo Sep 28 '22 at 16:25
  • Just create extra constructor in `base`: `explicit Base(int initialValueOfData)` and use it in `derived`. – Marek R Sep 28 '22 at 16:26
  • You're calling `get_data1()` which is a base class member function and it prints the base class member. Why do you expect it to print the derived class data member's value? – Jason Sep 28 '22 at 16:27
  • But won't the get_data1 function also be inherited? After inheritance it should be a member function of derived class too. And since I am calling it inside derived class, shouldn't I expect it to return value of data1 in the derived class? – World Travellzzz Sep 28 '22 at 16:50

2 Answers2

1

Just create extra constructor in base:

class base {
    int data1;

public:
    base()
        : base(5)
    {
    }
    explicit base(int initial)
        : data1 { initial }
    {
    }
    int get_data1() const
    {
        return data1;
    }
};

class derived : public base {
public:
    derived()
        : base(3)
    {
    }
    void printData()
    {
        std::cout << get_data1() << std::endl;
    }
};

https://godbolt.org/z/aqxhTjTYa

Marek R
  • 32,568
  • 6
  • 55
  • 140
1

I know that private members of base class are not inherited by the derived class.

No, private members are inherited just like other members. The difference is that they're not accessible from the derived class unlike public or protected members.


shouldn't it return 3 instead of 5 as it is a member function of the derived class?

No, you're confusing calling printData with calling getdata1. Even though printData is a derived class member function, it calls the base class' base::get_data1 member function which prints the base class' data member and hence the output.

See the added comments in the below program.

class base
{
    int data1;
    public:
        base()
        {
            data1=5;
        }
        int get_data1()
        {
            return data1;  //this prints base class' data1's value
        }
};

class derived : public base
{
    int data1;
    public:
        derived()
        {
            data1=3;
        }
        void printData()
        {
            cout<<get_data1()<<endl; //this calls base class' base::get_data1()
        }
};

int main()
{
    derived der1;
    der1.printData(); //this calls derived class' derived::printData()

    return 0;
}

Jason
  • 36,170
  • 5
  • 26
  • 60
  • But won't the get_data1 function also be inherited? After inheritance it should be a member function of derived class too. And since I am calling it inside derived class, shouldn't I expect it to return value of data1 in the derived class? – World Travellzzz Sep 28 '22 at 16:49
  • @WorldTravellzzz Even though `get_data1` will be inherited by the derived class, it is still part of the base class subobject of the derived class. For example, when you called it by writing `get_data()` you're actually doing `this->get_data1()`. Now, since the derived class doesn't have a member function called `get_data1()`, it's base class is searched where a member function named `get_data1` is found and used. And since the base class' member function is used we get the value of base class' data member printed on the screen. This is what is actually happening. – Jason Sep 28 '22 at 16:55
  • So can I, in any way, use get_data1 function to get data1 value of derived class? – World Travellzzz Sep 28 '22 at 17:03
  • @WorldTravellzzz Yes there is. For example you can add a member function with the same name `get_data1` inside the derived class which will return the derived class' data member. See [live demo](https://onlinegdb.com/d4jbJyZ7D) of this. I've added some comments in the above [linked demo](https://onlinegdb.com/d4jbJyZ7D) so that it is easier to see what changes i've made. When you execute this [demo](https://onlinegdb.com/d4jbJyZ7D) the output will be `3`. – Jason Sep 28 '22 at 17:10
  • But that's what confuses me...why do I need to define the function again in the derived class if it's already inherited. – World Travellzzz Sep 28 '22 at 17:24
  • @WorldTravellzzz As i said earlier because even though the member function is inherited by the derived class, that member function is still part of the base class subobject of the derived class. That is, the inherited member is not actually directly a part of the derived class. You can confirm this because if suppose what you're saying was true then this will mean that in this [demo](https://onlinegdb.com/d4jbJyZ7D) we're defining `get_data1` twice which is not possible and we would have gotten an error. – Jason Sep 28 '22 at 17:29
  • But instead we're not getting any error because the inherited member is not actually a part of the derived class. It is a part of the base class subobject of the derived class. So we're not actually defining the same member function twice. – Jason Sep 28 '22 at 17:29