C++语言-多态,纯虚函数,模版函数

102 阅读1分钟

1、虚函数、纯虚函数

class Person {
public:
    //普通函数
    void onCreat() {
        cout << "Person onCreat" << endl;
    }
    //
    // 虚函数是不是不必须继承的
    virtual void onStart() {
        cout << "Person virtual onStart" << endl;
    }

    //纯虚函数是必须继承的
    virtual void onStop() = 0;

};

class Student : public Person {
public:void onStop(){
        cout << "Student onStop" << endl;
    }
};
Student student;
    student.onStart();
    student.onStop();

Person virtual onStart
Student onStop

需要注意的是,纯虚函数子类是必须继承的,虚函数不是不必须继承的。

2、多态

在C++中默认关闭多态,而在Java中默认打开多态。 使用虚函数实现多态


class Person {
public:
    virtual void eat() {
        cout << "Person eat" << endl;
    }
};

class Student : public Person {
public:
    void eat() {
        cout << "Student eat" << endl;
    }

};

class Teacher : public Person {
public:
    void eat() {
        cout << "Teacher eat" << endl;
    }
};

void eat(Person *person) {
    person->eat();

}


int main() {
    Student *student = new Student();
    eat(student);

    Teacher *teacher = new Teacher();
    eat(teacher);
}
Student eat
Teacher eat

在这发现一个问题就是使用多态的时候,需要用指针对象,不能使用引用。

3、全纯虚函数实现回调

class ILogin {
public:
    virtual void loginSuccess(string msg) = 0;
};

class LoginImpl : public ILogin {
    void loginSuccess(string msg) {
        cout << msg << endl;
    }
};

void login(string username, string pwd, ILogin *login) {
    if (username == "daxia" && pwd == "123") {
        login->loginSuccess("login success");
    }
}


int main() {
    LoginImpl *loginImpl = new LoginImpl();
    login("daxia", "123", loginImpl);
    if (loginImpl) {
        delete loginImpl;
        loginImpl = NULL;
    }
}

全纯虚函数实现回调(模仿Java接口回调),我们创建的对象也是指针对象,而不能使用引用。

4、模版函数

模版函数类似Java中的泛型

template<typename T>
void add(T t1, T t2) {
    cout << t1+t2 << endl;
}
int main() {
    add(1,2);
}

5、构造函数中两种成员初始化方式

  • 使用this关键字
class Pig {
public:
    string name;
    Pig() {
    }
    Pig(string name) {
        this->name = name;
    }
};

class People {
public:
    int age = 0;
    Pig pig;

    People(int age, Pig pig) {
        this->age = age;
        this->pig = pig;
        cout << this->age <<" "<< this->pig.name << endl;
    }
};

int main() {
    Pig pig("xiaoming");
    People people(12, pig);
}
  • 使用冒号
class Pig {
public:
    string name;
    Pig() {
    }
    Pig(string name) {
        this->name = name;
    }
};

class People {
public:
    int age = 0;
    Pig pig;
    People(int age, Pig pig):age(age),pig(pig) {
        cout << this->age <<" "<< this->pig.name << endl;
    }
};

int main() {
    Pig pig("xiaoming");
    People people(12, pig);
}

6、继承关系子父类构造函数、析构函数执行顺序

class Object {
public:
    string name;
    Object(string name) {
        this->name = name;
        cout << "Object Construct" << endl;
    }
    ~Object(){
        cout << "Object Xigou" << endl;
    }

};
class Activity:Object{
public:
    int age;
    Activity(int age,string name): Object(name){
        cout << "Activity Construct" << endl;
    }
    ~Activity(){
        cout << "Activity Xigou" << endl;
    }
};


int main() {
    Activity activity(12,"xiaohong");
}
Object Construct
Activity Construct
Activity Xigou
Object Xigou

执行顺序: 父类构造函数->子类构造函数->子类析构函数->父类析构函数