On the C ++ inheritance

C ++ inheritance

1; Definition:

Class inheritance, is already characteristic of a new class derived from an existing class there. When some of the original class to produce new classes, new classes will include the original features of the class, but can also add all the new features of its own. The original class is called the parent class and the base classes, the new class is called the child generated and derived classes.

The syntax is defined as:

class derived class: class name inheritance group 1, the base class name inheritance 2, ······

eg:

class Base{

public;

int a=2;

};

class Derived:public Base{

public;

int b=3;

};

The purpose and effect of using inheritance: make the code look more concise.

2: Inheritance of the three classification

1: public inheritance

2: private inheritance

3: protected inheritance

1; public inheritance; when class inheritance as at this time, members of the public and protected members of the base class to access the same property in a derived class, while the private members of the base class is not directly accessible.
2; private inheritance: class inheritance is when this time, members of the public and protected members of the base class appear as a private members in a derived class, while the private members of the base class is not directly accessible in a derived class.
3: protected inheritance; members of the public and protected members of the base class are to protect the identity of the members appear in a derived class, while the private members of the base class is not directly accessible.

3; type compatibility rules:

Wherever any need to base class object, the object can be used to replace public derived class, after the replacement, the derived class object can use as a base class objects, but only inherited from the base class members.
class B{.....}

class D:public B{.....}  

B b1,*pb1;

D d1;

Alternative conditions:

1; derived class objects can be converted implicitly to a base class object

b1=d1;

2; derived class objects can refer to initialize the base class

B &rb=d1;

3; derived class objects can be converted to the implicit base class pointer

pb1=&d1;

4: structure and derived class destructor

1: Constructor

Precautions:

1; when an object of a derived class configuration, it is necessary to add members and member object base class to initialize the object

2: If the base class initialization time, need to call the constructor for the base class with a parameter table, it is necessary to declare a constructor

eg:

#include <iostream>
using namespace std;

class Base{
    public:
        Base(){
                cout << "Base的构造函数" << endl;
        }
};
class Derived:public Base{
    public:
    Derived(){
        cout << "Derived的构造函数" << endl;
    }
};

int main()
{
    Derived s;
}

1570974351864

Principles: first construct the parent class, then structure member variables, and finally to construct their own
2: destructor

Precautions:

In the derivation process, the destructor of the base class can not be inherited, if desired destructor, then we would declare a new destructor in the derived class.

eg:

#include <iostream>
using namespace std;

class Base{
    public:
    ~Base(){
        cout << "Base的析构函数" << endl;
    }
};
class Derived:public Base{
    public:
     ~Derived(){
        cout << "Derived的析构函数" << endl;
    }
};

int main()
{
    Derived s;
}

1570974302915

Principles: first destructor themselves, group members variables in the analysis, the final destruction parent.
In summary it can be seen, the sequence constructor and destructor are different.

5: Identity and Access derived class member of the succession

Precautions;

1: In the inheritance hierarchy base and derived class has its own scope.
2: subclasses and members of the same name in the parent class, subclass the parent class members will shield direct access to members of the same name, this is called "hidden", also called "redefine" (in the sub-class member functions, you can use :: base class base class members display access)
3: Note that if you hide a member function, just the same as function names constitute hidden. :

4: In practical engineering, inheritance system is best not to define the members of the same name.

eg:

class Person
{
protected:
    string _name ="小明"; //姓名
    int _num = 513030; //身份证号
};
class Student :public Person
{
public:
    void Print(){
        cout << "姓名: " << _name << endl;
        cout << "身份证: " << Person::_num << endl;
        cout << "学号: " << _num << endl;
    }
protected:
    int _num = 001; //学号
};

void Test()
{
    Student s1;
    s1.Print();
}

Multiple Inheritance succession; 6

In general, a sub-class has only one parent, but multiple inheritance refers to a sub-class has more than one parent class.

eg: multiple inheritance of the same name hidden

#include <iostream>
using namespace std;

class Base0{
    public:
        int var0;
        void fun0()
        {
             cout <<"Member of Base0"<< endl;
         } 
    };
class Base1:public Base0
{
        public:
        int var1;
};
class Base2:public Base0
{
        public:
        int var2;
};
class Derived:public Base1,public Base2{
    public:
    int var;
    void fun()
        {
             cout <<"Member of Derived"<< endl;
         } 
};

int main()
{
    Derived s;
    s.Base1::var0=2;
    s.Base1::fun0();
    s.Base2::var0=3;
    s.Base2::fun0();
    return 0;
}

1570976249214

Guess you like

Origin www.cnblogs.com/byp-520/p/11668753.html