C++多态

多态定义

多态分为静态多态和动态多态。静态多态在编译阶段就确定了函数的地址,而动态多态要到运行时才可以确定函数的地址。函数重载和运算符重载属于静态多态。


虚函数、纯虚函数、普通函数

  • 虚函数:通过关键字virtual修饰的函数,父类中提供了虚函数的实现,为子类提供默认的函数实现。也就是子类如果不实现虚函数,默认调用父类的虚函数实现,子类可以重写父类的虚函数。
  • 纯虚函数:通过关键字virtual修饰的函数,父类不实现纯虚函数,也就是只提供声明,不提供实现,子类必须要实现纯虚函数。如果class中包含了纯虚函数,那么该class被称为抽象类。抽象类不能new出来对象,只有实现了其纯虚函数的子类可以new出来对象。
  • 普通函数:普通函数没有多态的概念,是父类为子类提供的强制实现,子类不应该重写父类的普通函数
#include <iostream>
#include <string>
using namespace std;

class Father {
   public:
    /* 虚函数 */
    virtual void func1() {
        cout << "is Father func1()" << endl;
    }
    /* 纯虚函数 */
    virtual void func2() = 0;
    /* 普通函数 */
    void func3() {
        cout << "is Father func3()" << endl;
    }
};

class Son : public Father {
   public:
    virtual void func1() {
        cout << "is Son func1()" << endl;
    }

    virtual void func2() {
        cout << "is Son func2()" << endl;
    }

    void func3() {
        cout << "is Son func3()" << endl;
    }
};

int main(int argc, char const* argv[]) {
    Father* f = new Son();
    f->func1(); /* 输出 is Son func1(),如果子类没有重写父类的虚函数,就会输出 is Father func2() */
    f->func2(); /* 输出 is Son func2() */
    f->func3(); /* 输出 is Father func3() */
    delete f;

    return 0;
}

虚析构、纯虚析构

和虚函数、纯虚函数的的概念类似,如果是虚析构子类可以不重写析构函数,如果是纯虚析构则需要重写,含有纯虚析构的类叫抽象类。虚析构和纯虚析构是用来解决通过父类指针释放子类对象,如果子类中没有堆区的数据,可以不写虚析构或者纯虚析构。

#include <iostream>
#include <string>
using namespace std;

class Father {
   public:
    /* 纯虚函数 */
    virtual void func() = 0;
    /* 虚析构,如果父类不是虚析构或者纯虚析构,释放的时候子类的析构函数将无法被调用 */
    virtual ~Father() {
        cout << "~Father()" << endl;
    }
};

class Son : public Father {
   public:
    virtual void func() {
        cout << "value: " << *value << endl;
    }

    Son(int value) {
        this->value = new int(value);
    }

    ~Son() {
        cout << "~Son()" << endl;
        if (value != NULL) {
            cout << "delete value" << endl;
            delete value;
            value = NULL;
        }
    }

    int* value;
};

int main(int argc, char const* argv[]) {
    Father* f = new Son(10);
    f->func(); /* 输出:value: 10 */
    delete f;

    /* 父类不含虚析构的调用顺序:
       1. value: 10
       2. ~Father()
     */

    /* 父类含有虚析构的调用顺序:
       1. value: 10
       2. ~Son()
       3. delete value
       4. ~Father()
     */

    return 0;
}

示例

#include <iostream>
#include <string>
using namespace std;

/* 多态案例:
   1.生物识别有2种:Fingerprint, Face
   2.TEE有2种:TRUSTONIC, QSEE
   3.组装
 */

/* 生物识别抽象类 */
class BiometricsModule {
   public:
    virtual void biometrics() = 0;
};

/* TEE抽象类 */
class TEEModule {
   public:
    virtual void tee() = 0;
};

/* Fingerprint */
class Fingerprint : public BiometricsModule {
   public:
    virtual void biometrics() {
        cout << "Fingerprint biometrics" << endl;
    }
};

/* Face */
class Face : public BiometricsModule {
   public:
    virtual void biometrics() {
        cout << "Face biometrics" << endl;
    }
};

/* TRUSTONIC tee */
class Trustonic : public TEEModule {
   public:
    virtual void tee() {
        cout << "TRUSTONIC tee" << endl;
    }
};

/* QSEE tee */
class Qsee : public TEEModule {
   public:
    virtual void tee() {
        cout << "QSEE tee" << endl;
    }
};

/* 手机类 */
class MobilePhone {
   public:
    MobilePhone(BiometricsModule* bio, TEEModule* ta) {
        this->bio = bio;
        this->ta = ta;
    }

    ~MobilePhone() {
        if (this->bio != NULL) {
            delete this->bio;
            this->bio = NULL;
        }

        if (this->ta != NULL) {
            delete this->ta;
            this->ta = NULL;
        }
    }

    void work() {
        this->bio->biometrics();
        this->ta->tee();
    }

    BiometricsModule* bio;
    TEEModule* ta;
};

int main(int argc, const char** argv) {
    MobilePhone xiaomi(new Fingerprint, new Trustonic);
    cout << "xiamo" << endl;
    xiaomi.work();

    cout << "=========================" << endl;
    MobilePhone* vivo = new MobilePhone(new Face, new Qsee);
    cout << "vivo" << endl;
    vivo->work();

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++中的多态Polymorphism)是指在父类和子类之间的相互转换,以及在不同对象之间的相互转换。 C++中的多态性有两种:静态多态和动态多态。 1. 静态多态 静态多态是指在编译时就已经确定了函数的调用,也称为编译时多态C++中实现静态多态的方式主要有函数重载和运算符重载。 函数重载是指在同一作用域内定义多个同名函数,但它们的参数列表不同。编译器根据传递给函数的参数类型和数量来确定调用哪个函数。例如: ```c++ void print(int num) { std::cout << "This is an integer: " << num << std::endl; } void print(double num) { std::cout << "This is a double: " << num << std::endl; } int main() { int a = 10; double b = 3.14; print(a); // 调用第一个print函数 print(b); // 调用第二个print函数 } ``` 运算符重载是指对C++中的运算符进行重新定义,使其能够用于自定义的数据类型。例如: ```c++ class Complex { public: Complex(double real, double imag) : m_real(real), m_imag(imag) {} Complex operator+(const Complex& other) const { return Complex(m_real + other.m_real, m_imag + other.m_imag); } private: double m_real; double m_imag; }; int main() { Complex a(1.0, 2.0); Complex b(3.0, 4.0); Complex c = a + b; // 调用Complex类中重载的+运算符 } ``` 2. 动态多态 动态多态是指在运行时根据对象的实际类型来确定调用哪个函数,也称为运行时多态C++中实现动态多态的方式主要有虚函数和纯虚函数。 虚函数是在父类中定义的可以被子类重写的函数,使用virtual关键字声明。当一个对象的指针或引用指向一个子类对象时,调用虚函数时会根据实际的对象类型来确定调用哪个函数。例如: ```c++ class Shape { public: virtual void draw() { std::cout << "Drawing a shape." << std::endl; } }; class Circle : public Shape { public: void draw() override { std::cout << "Drawing a circle." << std::endl; } }; int main() { Shape* shape_ptr = new Circle(); shape_ptr->draw(); // 调用Circle类中重写的draw函数 } ``` 纯虚函数是在父类中定义的没有实现的虚函数,使用纯虚函数声明(如virtual void func() = 0;)。父类中包含纯虚函数的类称为抽象类,抽象类不能被实例化,只能作为基类来派生子类。子类必须实现父类的纯虚函数才能实例化。例如: ```c++ class Shape { public: virtual void draw() = 0; }; class Circle : public Shape { public: void draw() override { std::cout << "Drawing a circle." << std::endl; } }; int main() { Shape* shape_ptr = new Circle(); shape_ptr->draw(); // 调用Circle类中重写的draw函数 } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值