Something about Class inheritance

I'm thinking about some details in Class inheritance. I know that the derived class can only inherit the public or protected members. But if that member inherited in derived class is using some members private in base class, can the program work properly?
It inherits the private members just fine. You simply can't access them directly; you need to use the base class's methods to work with them.
Thanks~
Oh, BTW, what if I declare a member in the derived class, with a name same to the member declared privately in the base class? In that case, will the complier be confused when accessing that private member through the inherited base class method?
You don't really want to do that.

Say you had a value called 'x' in both your superclass and subclass. Whenever you inherit public methods from the superclass that reference that variable, they'll be referring to the instance in the superclass. If you override any of these methods, or create new ones in the subclass, they'll refer to the subclass instance. So it may compile, but it's going to sting you down the line.
Just so you are clear, iHutch is saying that there will be two distinct variables, that just happen to have the same name. Assuming both are public, to access the base you'd have to do something like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Base
{
public:
	int x;
};

class Derived : public Base
{
public:
	int x;
};

int main() {
	Derived d;
	d.x = 5;  // sets the derived member variable
	d.Base::x = 3;  // Sets the Base member variable
        return 0;

Thanks ~
:)
Topic archived. No new replies allowed.