Help on Inheritence needed!

Started by
3 comments, last by whaleyboy 19 years, 7 months ago
Okay, I'm trying to get my head around the whole inheritence thing and had a question... Say I have a base class (base_class) that has a private variable (x). Now, say I derive a new class from base_class called my_class (class my_class: public base_class) that adds a new private variable (y), and a public function that sets both x and y to a value. The compliler tells me I don't have access to x: "cannot access private member declared in class 'base_class'. I thought that as the child class was declared with the 'public' function it would be able to use it's own member functions to access the private varibles inherited from the base class...? Can anyone clear this up? Cheers!
Advertisement
It also depends on the status of the variable in the base class. Public variables remain public in derived classes, protected variables become private and private variables are not accessable.

Alan
"There will come a time when you believe everything is finished. That will be the beginning." -Louis L'Amour
Quote:Original post by AlanKemp
protected variables become private
Alan


I don't think so.
[size=2]
Quote:Original post by wild_pointer
Quote:Original post by AlanKemp
protected variables become private
Alan


I don't think so.


I really dont know what I was thinking when I wrote that...

Protected is what the OP was after. Protected is like private, except that derived classes can also access them.

Alan
"There will come a time when you believe everything is finished. That will be the beginning." -Louis L'Amour
Thanks guys, that's sorted it for me!

This topic is closed to new replies.

Advertisement