加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
4多态案例3-选择电脑零件.cpp 1.03 KB
一键复制 编辑 原始数据 按行查看 历史
Sincere Xin 提交于 2022-06-02 01:55 . C++基础部分文件
#include<iostream>
#include<string>
using namespace std;
class computer
{
public:
virtual void brand() = 0;
virtual ~computer(){ }
};
class cpu :public computer
{
public:
cpu(string cpu_name)
{
this->cpu_name = new string(cpu_name);
}
void brand()
{
cout << "CPUƷΪ" << *cpu_name << endl;
}
~cpu()
{
if (cpu_name != NULL)
{
delete cpu_name;
cpu_name = NULL;
}
}
string *cpu_name;
};
class graphicscard :public computer
{
public:
graphicscard(string g_name)
{
this->graphicscard_name = new string(g_name);
}
void brand()
{
cout << "ԿƷΪ" << *graphicscard_name << endl;
}
~graphicscard()
{
if (graphicscard_name != NULL)
{
delete graphicscard_name;
graphicscard_name = 0;
}
}
string *graphicscard_name;
};
void brand()
{
computer *cpu0 = new cpu("Inter");
cpu0->brand();
delete cpu0;
computer *gra = new graphicscard("3060");
gra->brand();
delete gra;
}
int main()
{
brand();
system("pause");
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化