不允许使用抽象类对象 纯虚函数没有强制代替项?

#include <iostream>
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 * cpu, VideoCard * vc, Memory * mem)
	{
		m_cpu = cpu;
		m_vc  = vc;
		m_mem =  mem;
	}
	void work()
	{
		m_cpu->calculate();
		m_vc->display();
		m_mem->storage();
	}
	~Computer()
	{

		//释放CPU零件
		if (m_cpu != NULL)
		{
			delete m_cpu;
			m_cpu = NULL;
		}

		//释放显卡零件
		if (m_vc != NULL)
		{
			delete m_vc;
			m_vc = NULL;
		}

		//释放内存条零件
		if (m_mem != NULL)
		{
			delete m_mem;
			m_mem = NULL;
		}
	}

private:
	CPU* m_cpu;//CPU指针
	VideoCard*m_vc;//显卡指针
	Memory* m_mem;//内存条指针
};

class intelCPU :public CPU
{
	virtual void calcualte()
	{
		cout << "intel的CPU开始计算" << endl;
	}
}; 

class intelVideoCard :public VideoCard
{
	virtual void dispaly()
	{
		cout << "intel的显卡开始计算" << endl;
	}
};

class intelMemory :public Memory
{
	virtual void storage()
	{
		cout << "intel的内存条开始计算" << endl;
	}
};
void test01()
{
	//创建零件
	CPU * intelCPU = new intelCPU ;
	VideoCard* intelCard = new intelVideoCard;
	Memory * intelMem = new intelMemory;
	cout << "开始工作" << endl;
	//创建电脑
	Computer * coumputer1 = new Computer(intelCPU, intelCard, intelMem);
	coumputer1 -> work();
	delete coumputer1;
}

int main() {

	test01();
	system("pause");

	return 0;
}

创建零件部分错误

3个中Memory成功,其他两个错误

求解

两个函数重载名字错误

  1. calculate 不是 calcualte
  2. display 不是 dispaly

一个变量名和类名一样冲突:

  • CPU * intelCPU = new intelCPU ;
    • intelCPU 是类名,不能再用来做变量名。
    • 最好写 new intelCPU()

我是怎么测试你的代码呢?

  1. 注释掉所有 Computer 相关的代码
  2. 只测试你提问的核心部分:抽象类继承的错误问题
  3. 阅读编译器错误,理解问题在哪,并修改

 

 

dispaly 和 calcualte 太草了。