Quick question about c++ and inheritance

class 1
private:
map<string, int> ...
bool anyChanges = false;

public:
bool anyChange() { return anyChanges}

class 2 : class 1

What i notice was, that for me to be able to use:
class2 test;
test.anyChange();

I hade to manually put in “bool anyChanges = false;” in class 2 private area.
I thought the whole thing with OO was that i didnt have to do that, instead, i do that if ive to/want to change something… Instead i get a error that’s that it cant get to ‘class1 - private bool anyChanges = false;’.

So my question is, its design like this? That the private objects etc dosent get inherent?
I can see why, then whats the point of having to classes?
But then again, maybe i just want to change the member function.
I strongly belive that im just missing something and that why im here. Maybe its just a bugg or ive missed something in it.
PS i never had this problem in c#, cant even remeber anything like this :S so im pretty sure ive just missed something?!
Best regards!

Try my example, https://github.com/bsodmike/level1-zemos-cpp14-inheritance-example

I didn’t need to do that hmm…

Component's changes: 0
Cog's (original) changes: 0
Cog's changes: 1

Notice anyChanges remains private from the inheriting class.

A niggle with your question is that you seem not to have have posted full C++ which can make knowing for sure what your problem is trickier. It is usually best in these sorts of questions to come up with a Short, Self Contained, Correct (Compilable), Example (see http://sscce.org/) that demonstrates just the problem you are asking about.

Your problem, as far as I can tell, is that you have derived class2 from class1 without specifying the access-type of the inheritance, and in C++ for classes the derived access to the base defaults to private, which means that not only will users of class2 and class2 instances not be able to access the base public members, they (and the protected base members) cannot be accessed from within class2 member functions either. This so called private inheritance does have its uses but is not very common. More commonly you should derive from a base class using public inheritance, thus:

class class2 : public class1
{

};

One point worth noting: this is C++ not C# ! Obvious yes? Just remember that and remember that they are different beasts in many ways despite some similarities, some which look similar on the outside but can be quite different in use (on the inside :slight_smile: )

Hope this helps

1 Like

Please include error messages and a compilable example next time. We have to make an awful lot of assumptions when reading your code.

Class 2 is inheriting privatetly from class 1 so inherited members are also private and not accessible from the outside. You’ll want something like

class 2: public 1 {
    ...
}
2 Likes

Hello thanks for the answer!
Short, i start out learing c++. Been doing that for a long time on and of. Then last year i did 3 courses on c#. basic - graphically - OO. And now im back to c++… and its just difficult! hehe

So the thing to remeber here is " : public class1 " :slight_smile:

Thanks all!

1 Like