简体   繁体   中英

the protected and private member variables in C++ inheritance

I am a newbie to C++, I have a question regarding to the c++ protected and private members in inheritance.

If a class is public inherits a based class, does the protected and private member variable will be part of derived class?

For example:

class Base
{
   protected: 
       int a;
       int b;
   private:
       int c;
       int d;
   public;
       int q;
};

class Derived: public Base
{

};

does class Derived also have all the member of a, b, c, d, q ? and can we define a int a as public, protected, and private in Derived class?

No class can access private variables. Not even subclasses.

Only subclasses can access protected variables.

All classes can access public variables.

All the member of the base class are part of the derived class. However, the derived class can only access members that are public or protected.

Declaring a member of the same name as a member of a Base class "shadows" the member of the Base class. That is the Derived class has its own independent variable that happens to have the same name as the base class version.

This is a personal choice, but I find using variables to communicate between base classes and derived classes leads to messier code so I tend to either make member variables private or use the PIMPL pattern.

The private members of a class can be inherited but cannot be accessed directly by its derived classes. They can be accessed using public or protected methods of the base class.

The inheritance mode specifies how the protected and public data members are accessible by the derived classes.

If the derived class inherits the base class in private mode,

  1. protected members of base class are private members of derived class.
  2. public data members of base class are private members of derived class.

If the derived class inherits the base class in protected mode,

  1. protected members of base class are protected members of derived class.
  2. public data members of base class are protected members of derived class.

If the derived class inherits the base class in public mode,

  1. protected members of base class are protected members of derived class.
  2. public data members of base class are public members of derived class.

Refer this link for more clarification: http://www.tutorialspoint.com/cplusplus/cpp_inheritance.htm

class A  
{ 
public: 
    int x; 
protected: 
    int y; 
private: 
    int z; 
}; 
  
class B : public A 
{ 
    // x is public 
    // y is protected 
    // z is not accessible from B 
}; 
  
class C : protected A 
{ 
    // x is protected 
    // y is protected 
    // z is not accessible from C 
}; 
  
class D : private A    // 'private' is default for classes 
{ 
    // x is private 
    // y is private 
    // z is not accessible from D 
}; 

The below table summarizes the above three modes and shows the access specifier of the members of base class in the sub class when derived in public, protected and private modes:

https://imgur.com/a/yaQjFOJ

https://www.geeksforgeeks.org/inheritance-in-c/

No derived class can access attributes or methods of its private base class. It can only access that of either public or protected. Even with setter and getter defined in the base class, you still get errors accessing members of the base class.

class A{
protected:
    int a {9723999};
};


class B:public A{
public:
    B(){
        cout<<a<<endl;
    }

};

int main()
{
    B b;
    return 0;
}

child class can not access private member of parent class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM