C ++ class inheritance

Inheritance class


In C ++, inheritance, there are three classes: public, protected, private.

Literal interpretation:

public: private properties are not inherited, various other privileges remain unchanged, and the same parent.

protected: private properties are not inherited, change other property protected, accessible within the class, the class is not accessible outside.

private: private properties are not inherited, change other property to private, accessible within the class, not accessible outside the class, subclass inherits is not allowed.

 

Graphic:

Code explanation:

// object-oriented inheritance way .cpp: This file contains the "main" function. Program execution will begin and end here.
//
 
#include <the iostream>
 the using  namespace STD;

class Father {
public:
    int a;
protected:
    int b;
private:
    int c;

};
class Son1 :public Father {

public:
    void show() {
        cout << A << endl;             // public members can be inherited, attributes the same public 
        cout << b << endl;             // protected members can be inherited, the same property protected
         // cout << endl << Money;       // private members can not be inherited, can not access 
    }

};

class Son2 :protected Father {
public:
    void show() {
        cout << A << endl;           // public properties can be inherited, within the protected class property to access 
        cout << b << endl;           // protection attributes can be inherited, the property remains unchanged, still protected
         // cout < <Money << endl;     // private member can not be inherited 
    }

};
class Son3 :private Father {
public:
    void show() {
        COUT << A << endl;           // public attributes can be inherited, attributes to private, the class can access 
        COUT B << << endl;           // protection attributes can be inherited, attributes to private, can access the class
         // c << endl << cout;          // private member can not be inherited 
    }

};

void test01() {

    Son1 son1;
    son1.a = 0 ;    // public properties remain public
     @ son1.b protection attributes are inaccessible outside the class
     // outer Private property class inaccessible son1.c
                                
    son1.show();
}
void test02() {
    Son2 son2;
    // son2.a     // public change protection, not accessible outside the class
     // son2.b     // protection does not change, is not accessible outside the class
     // son2.c     // private are not inherited, has been inaccessible 
    son2.show (); // public function can print protected members 
}

void test03() {
    Son3 son3;
    // all non-private members are all converted to private, inaccessible outside
    
    son3.show();
}

int main ()
{
    
    
}

 

 

Guess you like

Origin www.cnblogs.com/PPWEI/p/12605690.html