• c++多态的使用


    为什么要使用多态

    项目需求:

    因为各种不确定原因,包括人为原因,ODU设备会自动的切换到其它类型的设备,而切换后的设备,和原设备有很多不同的地方。如何完美的实现这个切换呢?

    解决方案:

    使用多态。

    聚会案例:

    Demo.cpp

    #include 
    using namespace std;
    
    class Father {
       
    public:
    	void play() {
       
    		cout << "到KTV唱歌..." << endl;
    	}
    };
    
    class Son :public Father {
       
    public:
    	void play() {
       
    		cout << "一起打王者吧!" << endl;
    	}
    };
    
    void party(Father **men, int n) {
       
    	for (int i = 0; i<n; i++) {
       
    		men[i]->play();
    	}
    }
    int main(void) {
       
    	Father father;
    	Son son1, son2;
    	Father* men[] = {
        &father, &son1, &son2 };
    
    	party(men, sizeof(men) / sizeof(men[0]));
    
    	system("pause");
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    解决方案:
    通过虚函数,实现多态。

    在这里插入图片描述在这里插入图片描述

    项目多态-虚函数

    多态的本质:

    形式上,使用统一的父类指针做一般性处理,

    但是实际执行时,这个指针可能指向子类对象,

    形式上,原本调用父类的方法,但是实际上会调用子类的同名方法。

    【注意】

    程序执行时,父类指针指向父类对象,或子类对象时,在形式上是无法分辨的!

    只有通过多态机制,才能执行真正对应的方法。

    基础-虚函数的使用

    虚函数的定义:

    在函数的返回类型之前使用virtual

    只在成员函数的声明中添加virtual, 在成员函数的实现中不要加virtual

    虚函数的继承:

    l 如果某个成员函数被声明为虚函数,那么它的子类【派生类】,以及子类的子类中,所继承的这个成员函数,也自动是虚函数。

    l *如果在子类中重写这个虚函数,可以不用再写virtual, 但是仍建议写virtual, 更可读!*

    虚函数表

    单个类的虚函数表

    #include 
    using namespace std;
    
    class Father {
       
    public:
    	virtual void func1() {
        cout << "Father::func1" << endl; }
    	virtual void func2() {
        cout << "Father::func2" << endl; }
    	virtual void func3() {
        cout << "Father::func3" << endl; }
    	void func4() {
        cout << "非虚函数:Father::func4" << endl; }
    public:  //为了便于测试,特别该用public
    	int x = 100;
    	int y = 200;
    	static int z;
    };
    
    typedef void (*func_t)(void);
    int Father::z = 1;
    int main(void) {
       
    	Father father;
    
    	// 含有虚函数的对象的内存中,最先存储的就是“虚函数表”
    	cout << "对象地址:" << (int*)&father << endl;
    
    	int* vptr = (int*)*(int*)&father;
    	cout << "虚函数表指针vptr:" << vptr << endl;
    
    	cout << "调用第1个虚函数: ";
    	((func_t) * (vptr + 0))();
    
    	cout << "调用第2个虚函数:";
    	((func_t) * (vptr + 1))();
    
    	cout << "调用第3个虚函数: ";
    	((func_t) * (vptr + 2))();
    
    	
    	cout << "第1个数据成员的地址: " << endl;
    	cout <<  &father.x << endl;
    	cout << std::hex << (int)&father + 4 << endl;
    	cout << "第1个数据成员的值:" << endl;
    	cout << std::dec <<  father.x << endl;
    	cout << *(int*)((int)&father + 4) << endl;
    
    	cout << "第2个数据成员的地址: " << endl;
    	cout << &father.y << endl;
    	cout << std::hex << (int)&father + 8 << endl;
    	cout << "第2个数据成员的值:" << endl;
    	cout << std::dec << father.y << endl;
    	cout << *(int*)((int)&father + 8) << endl;
    
    	cout << "sizeof(father)==" << sizeof(father) << endl;
    
    	Father father2;
    	cout << "father的虚函数
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
  • 相关阅读:
    SpringBoot 学习(一)自动装配
    微信小程序自定义组件、组件应用、插槽、slot
    水文中的Top-Down和bottom-up
    Cyber RT 使用
    03if基础
    HCIP 第十六天
    Linux使用find命令查找文件
    Scala数组常用函数(1)
    【webrtc】接收/发送的rtp包、编解码的VCM包、CopyOnWriteBuffer
    软件工程结构化设计
  • 原文地址:https://blog.csdn.net/m0_72703340/article/details/133788752