0
点赞
收藏
分享

微信扫一扫

头歌实训项目【复读机的毁灭】

Brose 2022-04-26 阅读 134
c++
#include <iostream>
using namespace std;

class Repeater
{
	//复读机基类的声明
public:

	virtual void Play();
	virtual ~Repeater()
	{
		cout << "砰!" << endl;
	}

};
//复读机基类的定义
void Repeater::Play() {}




class ForRepeater : public Repeater
{
	//正向复读机的声明
	void Play() override;
	virtual~ForRepeater()
	{
		cout << "正·复读机 炸了" << endl;
	}

};
//正向复读机的定义
void ForRepeater::Play()
{
	cout << "没想到你也是一个复读机" << endl;
}

class RevRepeater : public Repeater
{
	//反向复读机的声明
	void Play() override;
	virtual~RevRepeater()
	{
		cout << "机读复·反 炸了" << endl;
	}

};
//反向复读机的定义
void RevRepeater::Play()
{
	cout << "机读复个一是也你到想没" << endl;
}



//普通函数
Repeater* CreateRepeater(int type)
{
	//根据type创建指定的复读机
	if (type == 0)
	{
		return  new ForRepeater ;
	}
	else if (type == 1)
	{
		return new RevRepeater;
	}

}

int main()
{
    int i;
    cin >> i;
    Repeater* ptr = CreateRepeater(i);
    ptr->Play();
    delete ptr;
}

举报

相关推荐

0 条评论