C++, class inheritance

1. Basic concepts of inheritance

        Inheritance enables C++ to derive new classes from existing classes, and the derived class inherits the characteristics of the original class, including methods. The inheritor is called the parent class or base class, and the inheritor is called the subclass or derived class.

Purpose of inheritance:

  1. Enable code reusability
  2. Establish a link between parent and child classes
  3. When implementing polymorphism, it is necessary to implement the subclass's rewriting of the parent class's functions through inheritance.

Inherited format:

class 类名:继承方式 类名
{
    子类的拓展;
}

 Inheritance method:

        public inheritance, protected inheritance, private inheritance

Example:

#include <iostream>
using namespace std;

//封装 人  类  父类/基类
class Person
{
private:
    string name;
    int age;
public:
    //无参构造函数
    Person()
    {
        cout << "父类的无参构造函数" << endl;
    }

    //有参构造
    Person(string name, int age):name(name),age(age)
    {
        cout << "父类的有参构造函数" << endl;
    }
};

//封装 学生 类   共有继承人 类
class Stu:public Person   //子类 、派生类
{
private:
    int id;
    int math;
public:
    //无参构造函数
    Stu()
    {
        cout << "子类的无参构造函数" << endl;
    }

    //有参构造函数
    Stu(string name, int age, int id, int math):Person(name,age),id(id),math(math)
    {
        cout << "子类的有参构造函数" << endl;
    }
    
};

int main()
{
    Stu s("zhangsan",12,1001,78);
    return 0;
}

2. Special member functions in inheritance

Constructor:

        The parent class must be initialized before the child class. In other words, the constructor of the parent class must be called first, and then the constructor of the child class must be called.

Destructor:

        The order of calling destructors: first call the destructor of the subclass, then call the destructor of the parent class.

Constructed first and destructed later. Destruction first after construction.

Copy constructor:

        The copy constructor of the parent class will be inherited into the child class. The copy constructor of the parent class is used in the copy constructor of the child class to complete the copying of the members inherited by the child class from the parent class.

        If deep copying is involved, deep copying work needs to be completed in the subclass and parent class respectively.

Copy assignment function:

        The copy assignment function of the parent class will be inherited into the child class. The copy assignment function of the parent class is used in the copy assignment function of the child class to complete the assignment of members inherited by the child class from the parent class.

        If deep copying is involved, deep copying work needs to be completed in the subclass and parent class respectively.

Example:

#include <iostream>
using namespace std;

//封装 人  类  父类/基类
class Person
{
private:
    string name;
protected:
    int age;
public:
    int h;
public:
    //无参构造函数
    Person()
    {
        cout << "父类的无参构造函数" << endl;
    }

    //有参构造
    Person(string name, int age, int h):name(name),age(age),h(h)
    {
        cout << "父类的有参构造函数" << endl;
    }
    //拷贝构造函数
    Person(const Person & other):name(other.name),age(other.age),h(other.h)
    {
        cout << "父类的拷贝构造函数"  << endl;
    }


    //拷贝赋值函数
    Person & operator=(const Person &p)
    {
        name = p.name;
        age = p.age;
        h = p.h;
        cout << "父类的拷贝赋值函数" << endl;
        return  *this;
    }


    void show()
    {
        cout << "父类的show" << endl;
    }
};


//封装 学生 类   公有继承人 类
class Stu:public Person   //子类 、派生类
{
private:
    int id;
    int math;


public:
    //无参构造函数
    Stu()
    {
        cout << "子类的无参构造函数" << endl;
    }


    //有参构造函数
    Stu(string name, int age, int h, int id, int math):Person(name,age,h),id(id),math(math)
    {
        cout << "子类的有参构造函数" << endl;
    }


    //拷贝构造函数
    Stu(const Stu & s):id(s.id),math(s.math),Person(s)
    {
        cout << "子类的拷贝构造函数" << endl;
    }


    //拷贝赋值函数
    Stu & operator=(const Stu & s)
    {
        Person::operator=(s);

        id = s.id;

        math = s.math;

        cout << "子类的拷贝赋值函数" << endl;
        return *this;

    }
    void show()
    {
        cout << "子类的show" << endl;
        cout << h << endl; //通过共有继承,类外、子类可以访问父类共有成员
        cout << age << endl; //通过共有继承,子类可以访问父类保护成员,类外不可以访问
        //cout << name << endl;//通过共有继承,子类不可访问父类私有成员,类外不可以访问
    }


};


int main()
{
    Stu s("zhangsan",12,190,1001,78);

    Stu s2=s;

    Stu s3;
    s3 = s2;

//    s.show();
//    s.Person::show();

    return 0;
}

3. Summary

        The parent class must be initialized before the child class. In other words, the constructor of the parent class must be called first, and then the constructor of the child class.

        When the function of the parent class and the function of the subclass have the same name and type, no error will be reported because the scope is different. If the subclass instantiates an object and the object calls the function, the function of the subclass is called. If I want to call a function in the parent class. You need to add the class name and scope qualifier.

Guess you like

Origin blog.csdn.net/weixin_53478812/article/details/132634592
Recommended