欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 高考 > C++ 类和对象学习笔记(四)多态

C++ 类和对象学习笔记(四)多态

2025/4/29 17:09:03 来源:https://blog.csdn.net/qq_33582129/article/details/144877064  浏览:    关键词:C++ 类和对象学习笔记(四)多态

7多太

7.1 多太的基本概念

多太的基本概念
多太分为两类:
静态多太:函数重载和运算符重载属于静态多太,复用了同名函数
动态多太:派生类和虚函数实现运行时多态静态多太和动态多太的区别
静态多太:编译时确定函数地址,编译器根据函数名和参数类型确定函数地址
动态多太:运行时确定函数地址,虚函数表中存放函数地址
#include <iostream>
#include <string>using namespace std;// 动物类
class Animal {
public:// 虚函数virtual void speak() {cout << "动物在说话" << endl;  }
};// 猫类
class Cat : public Animal {
public:// 重写 函数返回值类型 函数名 参数列表 完全相同void speak() {cout << "猫在说话" << endl;}
};// 狗类
class Dog : public Animal {
public:void speak() {cout << "狗在说话" << endl;}
};//执行说话的函数
// 地址早绑定,在编译阶段确定函数地址
// 如果想执行猫在说话,那么这个函数地址就不能提前绑定,需要在运行时绑定,即虚函数// 动态多太的满足条件
// 1. 有继承关系
// 2. 子类重写父类虚函数// 动态多太使用
// 父类指针指或引用指向子类对象
void doSpeak(Animal &animal) {animal.speak();
}void test01() {Cat cat;doSpeak(cat);Dog dog;doSpeak(dog);
}
int main(int argc, char const *argv[]) {test01();return 0;
}

7.2 多太案例 - 计算器类

#include <iostream>
#include <string>using namespace std;//   多太案例 - 计算器类// 普通写法
class Calculator {
public:int getResult(string oper) {if (oper == "+"){return m_Num1 + m_Num2;}else if (oper == "-"){return m_Num1 - m_Num2;}else if (oper == "*"){return m_Num1 * m_Num2;}// 如果想加新的功能,需要修改源码// 在真实的开发中,提倡开闭原则return 0;}int m_Num1;int m_Num2;
};void test01() {// 创建计算器的对象Calculator c;c.m_Num1 = 10;c.m_Num2 = 20;cout << c.m_Num1 << " + " << c.m_Num2 << " = " << c.getResult("+") << endl;cout << c.m_Num1 << " - " << c.m_Num2 << " = " << c.getResult("-") << endl;cout << c.m_Num1 << " * " << c.m_Num2 << " = " << c.getResult("*") << endl;
}// 多态写法
// 多太好处
// 1、组织结构更清晰
// 2、可读性更高
// 3、对于前期和后期的扩展性更强
class AbstractCalculator {
public:virtual int getResult() {return 0;}int m_Num1;int m_Num2;
};// 加法计算器
class AddCalculator : public AbstractCalculator {
public:int getResult() {return m_Num1 + m_Num2;}
};// 减法计算器
class SubCalculator : public AbstractCalculator {
public:int getResult() {return m_Num1 - m_Num2;}
};// 乘法计算器
class MulCalculator : public AbstractCalculator {
public:int getResult() {return m_Num1 * m_Num2;}
};void test02() {// 创建加法计算器的对象AbstractCalculator* abc = new AddCalculator;abc->m_Num1 = 10;abc->m_Num2 = 20;cout << abc->m_Num1 << " + " << abc->m_Num2 << " = " << abc->getResult() << endl;// 释放内存delete abc;// 创建减法计算器的对象abc = new SubCalculator;abc->m_Num1 = 10;abc->m_Num2 = 20;cout << abc->m_Num1 << " - " << abc->m_Num2 << " = " << abc->getResult() << endl;// 释放内存delete abc;// 创建乘法计算器的对象abc = new MulCalculator;abc->m_Num1 = 10;abc->m_Num2 = 20;cout << abc->m_Num1 << " * " << abc->m_Num2 << " = " << abc->getResult() << endl;// 释放内存delete abc;
}int main(int argc, char const *argv[]) {test02();return 0;
}

7.3 纯虚函数和抽象类

//  纯虚函数和抽象类// 在多态中,通常父类中的实现是毫无意义的,主要是调用子类的实现。因此,父类函数一般声明为纯虚函数。// 纯虚函数语法:virtual 返回值类型 函数名 (参数列表) = 0;// 当类中拥有纯虚函数时,这个类也称为抽象类。// 抽象类的特点:
// 1. 无法实例化对象
// 2. 子类必须重写父类中的纯虚函数,否则也属于抽象类
#include <iostream>
#include <string>using namespace std;class Base {
public:virtual void func() = 0;
};class Son : public Base {
public:virtual void func() {cout << "Son func" << endl;}
};void test01() {// Base base; // 抽象类无法实例化对象// new Base; // 抽象类无法实例化对象Base *base = new Son;base->func();}int main(int argc, char const *argv[]) {test01();return 0;
}

多态案例 - 制作饮品

#include <iostream>
#include <string>using namespace std;// 制作饮品class AbstractDrink {
public:// 煮水virtual void Boil() = 0;// 冲泡virtual void Brew() = 0;// 倒入杯中virtual void PourInCup() = 0;// 加入辅料virtual void PutSomething() = 0;// 制作饮品void make() {Boil();Brew();PourInCup();PutSomething();}
};// 制作咖啡
class Coffee : public AbstractDrink {
public:virtual void Boil() {cout << "煮矿泉水" << endl;}virtual void Brew() {cout << "冲泡咖啡" << endl;}virtual void PourInCup() {cout << "倒入杯中" << endl;}void PutSomething() {cout << "加入糖和牛奶" << endl;}
};// 制作茶叶
class Tea : public AbstractDrink {
public:void Boil() {cout << "煮雪水" << endl;}void Brew() {cout << "冲泡茶叶" << endl;}void PourInCup() {cout << "倒入杯中" << endl;}void PutSomething() {cout << "加入柠檬" << endl;}
};void MakeDrink(AbstractDrink *drink) {drink->make();delete drink;
}void test01() {// Coffee *coffee = new Coffee;MakeDrink(coffee);Tea *tea = new Tea;MakeDrink(tea);// Coffee coffee2;// MakeDrink(&coffee2); // 这种写法会报错,因为coffee2是栈上的对象,当MakeDrink函数执行完毕后,coffee2对象就被销毁了,会导致coffee2对象访问非法
}int main(int argc, char const *argv[]) {test01();return 0;
}

7.5 虚析构和纯虚析构

// 虚析构和纯虚析构//多态使用时,如果子类中有属性开辟到堆区,那么父类指针在释放时无法调用到子类的析构代码
//解决方式:将父类的析构函数改为虚析构或者纯虚析构//虚析构和纯虚析构的共性:
// 可以解决父类指针释放子类对象时,无法调用到子类的析构代码问题
// 都需要有具体的函数实现//虚析构和纯虚析构的区别:
// 如果是纯虚析构函数,该类属于抽象类,无法实例化
#include <iostream>
#include <string>using namespace std;class Animal {
public:Animal(){cout << "Animal构造函数调用" << endl;}virtual void speak() = 0;// 利用虚析构可以解决父类指针释放子类对象时,无法调用到子类的析构代码问题// virtual ~Animal() { //     cout << "Animal析构函数调用" << endl;// }// 纯虚析构 需要声明也需要实现virtual ~Animal() = 0;
};Animal::~Animal() {cout << "Animal析构函数调用" << endl;
}class Cat : public Animal {
public:Cat(string name) {cout << "Cat构造函数调用" << endl;this->name = new string(name);}void speak() {cout << *name << "小猫在说话" << endl;}string *name;~Cat() {if (this->name != NULL){delete this->name;}cout << "Cat析构函数调用" << endl;}
};void test01() {Animal *animal = new Cat("Tom");animal->speak();// 父类指针在析构时,不会调用到子类的析构代码delete animal;
}int main(int argc, char const *argv[]) {test01();return 0;
}
// 总结:
// 1. 虚析构和纯虚析构就是用来解决父类指针释放子类对象,会导致子类对象的析构和释放不执行的问题
// 2. 如果子类中没有堆区数据,可以不写虚析构和纯虚析构
// 3. 拥有纯虚析构函数的类也属于抽象类

7.6 多态练习-电脑组装

#include <iostream>
#include <string>using namespace std;// 抽象不同零件的类
// 抽象的cpu
class CPU {// 计算函数
public:virtual void calculate() = 0;
};// 抽象的显卡
class VideoCard {
public:// 显示函数virtual void display() = 0;
};// 抽象的内存条
class Memory {// 存储函数
public:virtual void storage() = 0;
};// 电脑类
class Computer {
public:Computer(CPU *c, VideoCard *v, Memory *m) {cpu = c;vc = v;mem = m;}CPU *cpu;VideoCard *vc;Memory *mem;// 组装void work() {cpu->calculate();vc->display();mem->storage();}// 析构函数 释放零件~Computer() {cout << "computer 析构函数调用" << endl;if (cpu != NULL) {delete cpu;}if (vc != NULL) {delete vc;}if (mem != NULL) {delete mem;}}
};// 具体的厂商
// 具体的intel cpu
class IntelCPU : public CPU {
public:void calculate() {cout << "intel cpu 开始计算了" << endl;}
};
// 具体的intel 显卡
class IntelVideoCard : public VideoCard {
public:void display() {cout << "intel 显卡开始显示了" << endl;}
};
// 具体的intel 内存条
class IntelMemory : public Memory {
public:void storage() {cout << "intel 内存条开始存储了" << endl;}
};class LenovoCPU : public CPU {
public:void calculate() {cout << "Lenovo cpu 开始计算了" << endl;}
};
// 具体的intel 显卡
class LenovoVideoCard : public VideoCard {
public:void display() {cout << "Lenovo 显卡开始显示了" << endl;}
};
// 具体的intel 内存条
class LenovoMemory : public Memory {
public:void storage() {cout << "Lenovo 内存条开始存储了" << endl;}
};void test01() {Computer *computer1 = new Computer(new IntelCPU, new IntelVideoCard, new IntelMemory);computer1->work();delete computer1;cout << "-----------------" << endl;computer1 = new Computer(new LenovoCPU, new LenovoVideoCard, new LenovoMemory);computer1->work();delete computer1;
}int main(int argc, char const *argv[]) {test01();return 0;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词