shreyasva
shreyasva

Reputation: 13506

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

Upvotes: 25

Views: 59236

Answers (8)

Vegeta
Vegeta

Reputation: 332

If you want inherited private members to be modifiable in a child class, then that is possible if you make the child class a friend of the base class.

Upvotes: 0

Kotauskas
Kotauskas

Reputation: 1374

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 and someobject.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).

Upvotes: 5

mr.trovaz
mr.trovaz

Reputation: 21

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;
}

Upvotes: 2

JRL
JRL

Reputation: 78033

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

Upvotes: 10

Danvil
Danvil

Reputation: 23031

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++.

Upvotes: 0

zmbush
zmbush

Reputation: 2810

Getters and setters do Not give you complete control over private data members. The control still lies with the base class.

Upvotes: 0

Avi
Avi

Reputation: 20152

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.

Upvotes: 42

Billy ONeal
Billy ONeal

Reputation: 106609

Because the getters and setters are public -- they're callable by anyone, not just derived classes.

Upvotes: 3

Related Questions