Reputation: 1203
I'm confused on how private variables are inherited and I'm getting errors telling my variable's are private when I try to use them in inherited classes.
A bare-bones example.
Let's say:
//dog.h
class dog
{
private:
bool fluffy;
public:
...
};
And let's say:
//dog.cpp
#include "dog.h"
...
Now:
//shepard.h
#include "dog.h"
class shepard: public dog
{
private:
...
public:
void groom();
};
And:
//shepard.cpp
#include "shepard.h"
void shepard::groom()
{
fluffy = false;
}
If I try to use the groom function I get an error something like:
error: 'bool dog::fluffy' is private
What am I doing wrong? Optional: What's the best way to design this solution in the future?
Upvotes: 1
Views: 206
Reputation: 206616
The most important rule for inheritance is:
Private members of a class are never accessible from anywhere except the members of the same class.
Further in Private Inheritance:
All Public members of the Base Class become Private Members of the Derived class &
All Protected members of the Base Class become Private Members of the Derived Class
Good Read:
What are access specifiers? Should I inherit with private, protected or public?
Upvotes: 2