C++—Inheritance and polymorphism

Article directory

1 inheritance

Class inheritance can derive a class based on the original class. The original class is called the base class (parent class). A derived class is called a derived class (subclass). The class inheritance mechanism enables C++ to achieve a great deal of code reuse. The basic form of defining a derived class is

class 派生类名:访问权限 基类名

There are three types of access rights, namely public, private, and protected. Usually, the access rights of derived classes are defined as public. The derived class can automatically obtain all the data members and member functions in the base class, and except for the private type, other types can be directly accessed in the derived class. Derived classes can also define their own data members and member functions.

When the object of the derived class is generated, it also needs to be initialized with the constructor. During initialization, the constructor of the base class is called first to initialize the data members of the base class in the derived class. Then call the derived class's own constructor to initialize the data members of the derived class. Destructors are also called separately when objects are destroyed. Destructors are called in the reverse order of constructors.

Access rights are different for different base class members

member type access permission
public Accessible both outside the class and in derived classes
private Can only be accessed within the class, not outside the class and in derived classes
protected Both within the class and derived classes can be accessed, but not outside the class

If no access specifier is specified when defining a member, it defaults to private. Learn about inheritance with an example

#include<iostream>

using namespace std;

// 基类
class temp
{
    
    
    public:
        string name;
        int age;
};

class temp1 : public temp
{
    
    
    public:
        string number;
};

int main()
{
    
    
    temp1 a;   // 定义一个类tenp1的对象
    
    // 给对象a的成员赋值
    a.name = "ertu";
    a.age = 23;
    a.number = "20230714";
    
    // 输出对象a成员
    cout << "姓名:" << a.name << endl;
    cout << "年龄:" << a.age << endl;
    cout << "编号:" << a.number << endl;
    
    return 0;
}

The output is

姓名:ertu
年龄:23
编号:20230714

A function with the same name as the member function of the base class can be defined in the derived class, and the return type, number of parameters and parameter types can all be the same . For a function already defined in the base class, if a function with the same name is defined in the derived class, this is called refactoring or overriding. For functions that are only declared in the base class, if a function of the same name is defined in the derived class, this becomes the implementation .

#include<iostream>

using namespace std;

// 基类
class temp
{
    
    
    public:
        void function1 ()
        {
    
    
            cout << "调用基类函数1" << endl;
        }
        
        void function2 ();   // 仅有一个声明
};

class temp1 : public temp
{
    
    
    public:
        
        // 重置基类成员函数1
        void function1 ()
        {
    
    
            cout << "调用派生类成员函数1" << endl;
        }
        
        // 实现基类成员函数2
        void function2 ()
        {
    
    
            cout << "调用派生类成员函数2" << endl;
        }
};

int main()
{
    
    
    temp a;   // 定义一个temp类的对象
    temp1 b;   // 定义一个类tenp1的对象
    
    // 调用成员函数
    a.function1();
    // a.function2();   // 基类中没有function的实现,不能调用
    b.function1();
    b.function2();
    
    return 0;
}

The output is

调用基类函数1
调用派生类成员函数1
调用派生类成员函数2

2 Polymorphism

/------------------------------------------------- 2023.07.14 Unfinished------------------------------------------------ ---------/

Guess you like

Origin blog.csdn.net/qq_45217381/article/details/131725737