C++学习笔记——(六)多态、纯虚函数、模板函数

注:编码工具是CLion+Cygwin64

目录

类属性为对象时的初始化方式

有继承关系时,子类和父类的构造和析构函数执行顺序

多态

几种常见说法

动态多态(重写)

静态多态(重载)

纯虚函数

类中函数部分为纯虚函数

类中函数全部为纯虚函数

全纯虚函数类模拟登录回调

模板函数


类属性为对象时的初始化方式

#include 

using namespace std;

class Pet{
public:
    string name;
    Pet(string name):name(name){}
};

class Man{
public:
    string name;
    Pet pet;
    // 初始化方式一
    Man(string manName, string petName):name(manName), pet(petName){}
    // 初始化方式二
    Man(string name, Pet pet): name(name), pet(pet){}
//    Man(string name, Pet pet){
//        this->name = name;
//        // 错误的初始化方式,编译报错
//        this->pet = pet;
//    }
};

int main(){
    Man man("man", "pet");
    return 0;
}

有继承关系时,子类和父类的构造和析构函数执行顺序

#include 

using namespace std;

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

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


int main(){
    Sub sub;
    return 0;
}

输出:

Base构造函数
Sub构造函数
Sub析构函数
Base析构函数

多态

几种常见说法

        父类的引用指向子类的对象;

        同一个函数的不同实现;

        重载和重写。

动态多态(重写)

        程序在运行期间才知道调用哪个类的函数,这种情形就是动态多态。

        C++默认不支持动态多态。

#include 

using namespace std;

class Base{
public:
    void show(){
        cout << "Base show" << endl;
    }
};

class Sub: public Base{
public:
    void show(){
        cout << "Sub show" << endl;
    }
};

class Sub2: public Base{
public:
    void show(){
        cout << "Sub2 show" << endl;
    }
};

void callShow(Base * base){
    base->show();
}

int main(){
    Sub * sub = new Sub;
    Sub2 * sub2 = new Sub2;
    callShow(sub);
    callShow(sub2);
    delete sub;
    delete sub2;
    cout << endl;
    Base * subBase = new Sub;
    Base* sub2Base = new Sub2;
    callShow(subBase);
    callShow(sub2Base);
    delete subBase;
    delete sub2Base;
    return 0;
}

输出:

Base show
Base show

Base show
Base show

要让C++支持动态多态,需要将父类的函数声明为虚函数。

将上面代码的Base类中的show函数添加virtual修饰即可。

class Base{
public:
    virtual void show(){
        cout << "Base show" << endl;
    }
};

输出:

Sub show
Sub2 show

Sub show
Sub2 show

静态多态(重载)

        程序在编译期间就可以决定调用哪个函数,这种情形就是静态多态。

#include 

using namespace std;
void add(int a, int b){
    cout << "two int sum: " << a << " + " << b << " = " << a + b << endl;
}
void add(int a, int b, int c){
    cout << "three int sum: " << a << " + " << b << " + " << c << " = " << a + b + c << endl;
}
void add(float a, float b){
    cout << "two float sum: " << a << " + " << b << " = " << a + b << endl;
}
void add(double a, double b){
    cout << "two double sum: " << a << " + " << b << " = " << a + b << endl;
}
int main(){
    add(1, 2);
    add(3, 4, 5);
    add(6.0f, 7.0f);
    add(8.0, 9.0);
    return 0;
}

输出:

two int sum: 1 + 2 = 3
three int sum: 3 + 4 + 5 = 12
two float sum: 6 + 7 = 13
two double sum: 8 + 9 = 17

纯虚函数

类中函数部分为纯虚函数

        类似Java的抽象类。如果一个类继承了此种类,则必须重写该类的纯虚函数,否则无法创建对象。

#include 

using namespace std;

class Base{
public:
    void show(){
        test();
    }

    virtual void test() = 0;
};

class Sub: public Base{};
class Sub2: public Base{
public:
    void test(){
        cout << "测试纯虚函数" << endl;
    }
};
int main(){
    // Sub sub;//编译时报错 Variable type 'Sub' is an abstract class
    Sub2 sub2;
    sub2.show();
    return 0;
}

输出:

测试纯虚函数

类中函数全部为纯虚函数

        类似Java的接口。如果一个类继承了此种类,则必须重写该类的纯虚函数,否则无法创建对象。

#include 

using namespace std;

class Base {
public:
    virtual void test() = 0;
};

class Sub : public Base {
};

class Sub2 : public Base {
public:
    void test() {
        cout << "测试全纯虚函数" << endl;
    }
};

int main() {
    //Sub sub;//编译时报错 Variable type 'Sub' is an abstract class
    Sub2 sub2;
    sub2.test();
    return 0;
}

输出:

测试全纯虚函数

全纯虚函数类模拟登录回调

#include 

using namespace std;

class LoginResponse{
public:
    string token;
    LoginResponse(string token): token(token){}
};

class LoginCallback{
public:
    virtual void loginSuccess(int code, string msg, LoginResponse loginResponse) = 0;
    virtual void loginFailure(int code, string msg) = 0;
};

void login(string name, string pwd, LoginCallback & callback){
    if("root" == name && "root" == pwd){
        callback.loginSuccess(200, "登录成功", LoginResponse("329dfas847dfsa6"));
    }else{
        callback.loginFailure(400, "登录失败,请检查用户名或密码");
    }
}

class CallbackImpl : public LoginCallback{
public:
    void loginSuccess(int code, string msg, LoginResponse loginResponse){
        cout << "code = " << code << ", msg = " << msg << ", token = " << loginResponse.token << endl;
    }

    void loginFailure(int code, string msg){
        cout << "code = " << code << ", msg = " << msg << endl;
    }
};

int main() {
    string name;
    cout << "请输入用户名..." << endl;
    cin >> name;
    string pwd;
    cout << "请输入密码..." << endl;
    cin >> pwd;
    CallbackImpl callback;
    login(name, pwd, callback);
    return 0;
}

输出:

请输入用户名...
root
请输入密码...
root
code = 200, msg = 登录成功, token = 329dfas847dfsa6

模板函数

        类似Java的泛型。

#include 

using namespace std;

template 
void add(T a, T b){
    cout << a << " + " << b << " = " << a + b << endl;
}

int main(){
    add(1, 2);
    add(3.14f, 4.13f);
    add(5.38, 6.97);
    add('7', '8');
    add("9", "10");
    // add(11.0, 12);// 编译报错,两种数据类型
    return 0;
}

输出:

1 + 2 = 3
3.14 + 4.13 = 7.27
5.38 + 6.97 = 12.35
7 + 8 = 111
9 + 10 = 910

你可能感兴趣的:(C/C++学习笔记,c++,开发语言,C++学习笔记)