C ++ inheritance - initialization of the base class in a derived class

Unlike the other members of the constructors of the base class can not be inherited a derived class, so to member variables initialized in the base class, need to call the constructor for the base class in a derived class (i.e., explicit call), if the delivery class does not call the the default call the base class constructor without parameters (i.e. implicit invocation).

Invoked explicitly by reference the following code:


//基类
class animal{
    protected:     
        int height;
    public:
        animal(){
            height=0;
        }
        animal(int height){
            this->height=height;
        }
};
//派生类
class fish:public animal{
    public:
        fish(){
            //
        }
        fish(int height):animal(height){
        //
        }
};
//fish m_fish(3); then animal->height = 3;

• If the base class is inherited in a public way public, all public members of the base class will become public members of the derived class. Protected base class members become protected members of the derived class
• If the base class is inherited in a private private, all public members of the base class will become private members of the derived class. Protected members of the base class become private members of the derived class.
• If the base class is inherited protected to a protected way, then all the members of a protected public and protected members of the derived class will become the base class.

Guess you like

Origin www.linuxidc.com/Linux/2019-07/159572.htm