#include<iostream>
using namespace std;
//多态,在子类中重写父类方法
class AbstructCaculator {
public:
virtual int getResult() = 0; //存虚函数 抽象类无法被实例化,
//子类必须重写抽象类中的函数,否则也属于抽象类 无法被实例化
public:
int num_A;
int num_B;
};
class subCa :public AbstructCaculator {
public:
int getResult() {
return num_A - num_B;
}
};
static void test(){
//父类指针或引用指向子类对象
AbstructCaculator *ab = new subCa;
ab->num_A = 10;
ab->num_B = 5;
cout << ab->getResult() << endl;
delete ab;
system("pause");
subCa sc;
sc.num_A = 55;
sc.num_B = 60;
cout << sc.num_A << " " << sc.num_B << endl;
cout << sc.getResult() << endl;
}
int main() {
test();
}
纯虚函数和抽象类
最新推荐文章于 2024-09-01 22:58:34 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)