Inheriting private members in C++
suppose a class has private data members but the setters and getters are in public scope. If you inherit from this class, you开发者_如何学编程 can still call those setters and getters -- enabling access to the private data members in the base class. How is this possible since it is mentioned that a derived class cannot inherit private data members
A derived class doesn't inherit access to private data members. However, it does inherit a full parent object, which contains any private members which that class declares.
It depends on the inheritance type. If you inherit privately, then the derived class does NOT have access to the Base's private members.
Access public protected private
-----------------------------------------------------------
members of the same class yes yes yes
members of derived classes yes yes no
not members yes no no
Because the getters and setters are public
-- they're callable by anyone, not just derived classes.
They are included, but not inherited. What this means is:
- Any inheriting type (
: public SomeClass
,: protected SomeClass
or even: SomeClass
, equivalent to: private SomeClass
) will not make them accessible from child class methods, or outside (this->a
andsomeobject.a
respectively); - They will still be there - take space in memory allocated for child class instance;
- Inherited methods will be able to access that private field.
So, basically protected
is not visible outside while visible inside and from derived classes (if : private Parent
wasn't used), while private
is not visible from neither derived classes nor outside of the parent class; it's only visible for parent class' methods, even if they are inherited (but not overrided).
you can access to them by set access to setters and getters public and acces to them like that
*.h
class Mamifere
{
private:
int a;
public:
Mamifere();
virtual ~Mamifere();
int getA();
// ~Mamifere(); //le delete dans le exp02() affiche seulement mamifere mort :( destructeur de la class mere
void manger() ;
virtual void avancer() const;
};
class Deufin:public Mamifere{
public:
Deufin();
void manger() const;
void avancer() const;
~Deufin();
};
*.cpp
Mamifere::Mamifere(){
printf("nouveau mamifere est nee\n");
this->a=6;
}
Mamifere::~Mamifere(){
printf("mamifere Mort :(\n");
}
void Mamifere::manger() {
printf("hhhh je mange maifere %d\n",Mamifere::getA());
}
void Mamifere::avancer() const{
printf("allez-y Mamifere\n");
}
Deufin::Deufin(){
printf("nouveau Deufin est nee\n");
}
int Mamifere::getA(){
return this->a;
}
void Deufin::manger() const{
printf("hhhh je mange poisson\n");
}
void Deufin::avancer() const{
printf("allez-y Deufin\n");
}
Deufin::~Deufin(){
printf("Deufin Mort :(\n");
}
main.cpp
void exp031(){
Mamifere f;//nouveau mamifere est nee // nouveau Deufin est nee
Deufin d;
f.avancer();//allez-y Deufin (resolution dynamique des lien la presence de mot cle virtual)
f.manger();//hhhh je mange maifere (resolution static des lien pas de mot cle virtual)
printf("a=%d\n",d.getA());//Deufin Mort :( mamifere Mort :( (resolution static des lien la presence de mot cle virtual) distructeur de class fille appel auromatiquement le destructeur de la class mere
}
int main(){
exp031();
getchar();
return 0;
}
Getters and setters do Not give you complete control over private data members. The control still lies with the base class.
Using the pattern
class MyClass {
private: int a;
public: void setA(int x) { a = x; }
public: int getA() const { return a; }
};
seems object-orientated and has the sent of encapsulation.
However as you noticed, you can still directly access the private field and there is nothing gained over just making a
public and accessing it directly.
Using getters and setters like this does not really make sense in C++.
精彩评论