第十四章编程练习(4)

本文介绍如何使用C++类来创建一个动态员工队伍系统,允许用户根据角色(如普通员工、枪手、扑克玩家、坏蛋)输入详细信息并展示这些信息。通过命令行交互,系统能够创建不同角色的对象,每种角色都有其特定的行为和属性,如拔枪时间和扑克牌抽取。此实现展示了面向对象编程的概念和类的继承、多态性,并提供了交互式的员工创建和展示功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PERSIN.h

#pragma once
#ifndef PERSON_H_
#define PERSON_H_
#include <iostream>
class Person {
private:
	std::string fname;
	std::string sname;
public:
	Person();
	Person(const std::string & f, const std::string  & s);
	virtual ~Person() {};//记得定义虚析构函数,即使它什么都不做
	virtual void Set();
	virtual void Show()const;
};
class Gunslmger :virtual public Person {
private:
	double times;
	int digit;//枪手枪上的刻痕数
public:
	Gunslmger(const std::string f="null", const std::string s="null", double t=0.0, int d=0);
	Gunslmger(double time, int di);
	virtual ~Gunslmger() {};
	void Set();
	double Draw();//返回枪手拔枪时间
	virtual void Show()const;
};
class PokerPlays : virtual public Person {
public:
	virtual ~PokerPlays() {};
	PokerPlays(const std::string f="null",const std::string s="null");
	int Draw();//返回一个1-52的随机数显示扑克牌
	virtual void Show()const;//使用Person的Show方法

};
class BadDude :public Gunslmger, public PokerPlays {
private:
	double Badtime;
public:
	BadDude(const std::string  f="null",const std::string s="null",double ba=0.0);
	double Gdraw();//返回坏蛋的拔枪时间
	int Cdraw();//返回下一张扑克牌
	void Set();
	void Show()const;
};
#endif

PERSON.cpp

#include "PERSON.h"
#include <iostream>
#include <string>
Person::Person()
{};
Person::Person(const std::string & f, const std::string  & s)
{
	fname = f;
	sname = s;
}
void Person::Set()
{
	using std::cout;
	using std::cin;
	cout << "Please enter a fistname: ";
	getline(cin, fname);
	cout << "Please enter a secondname: ";
	getline(cin, sname);
}

void Person::Show() const
{
	using std::cout;
	using std::endl;
	cout << fname << endl
		<< sname << endl;
}

Gunslmger::Gunslmger(const std::string f, const std::string s, double t, int d) {};

Gunslmger::Gunslmger(double time, int di) :Person(), times(time), digit(di)
{
}
void Gunslmger::Set()
{
	Person::Set();
	std::cout << "Please enter a time of Gun: ";
	std::cin >> times;
	std::cout << "Please enter a digit: ";
	std::cin >> digit;
	while (std::cin.get() != '\n')
		continue;
}

double Gunslmger::Draw()
{
	return times;
}

void Gunslmger::Show() const
{
	Person::Show();
	std::cout << "time: " << times << std::endl
		<< "Digit: " << digit << std::endl;
}
PokerPlays::PokerPlays(const std::string f, const std::string s):Person(f,s)
{
}

int PokerPlays::Draw()
{
	int re = rand() % 52 + 1;
	return re;
}

void PokerPlays::Show() const
{
	Person::Show();
}
BadDude::BadDude(const std::string f, const std::string s, double ba)
	:Person(f,s),Badtime(ba)
{
}
double BadDude::Gdraw()
{
	return Badtime;
}

int BadDude::Cdraw()
{
	return PokerPlays::Draw();
}

void BadDude::Set()
{
	Person::Set();
	std::cout << "Please enter a time of BadDude: ";
	std::cin >> Badtime;
	while (std::cin.get() != '\n')
		continue;
}

void BadDude::Show() const
{
	Gunslmger::Show();
	std::cout << "Badtime: " << Badtime << std::endl;
}

main.cpp

#include <iostream>
#include <ctime>
#include "PERSON.h"
const int Size = 5;
int main()
{
	srand(time(0));
	using std::cin;
	using std::cout;
	using std::endl;
	using std::strchr;
	Person * temp[Size];
	int n;
	char ch;
	for (n = 0; n < Size; n++)
	{
		cout << "Enter the employee:\n"
			<< "w: Person   s: Gun\n"
			<< "t: Bad      q: quit\n";
		cin >> ch;
		while (strchr("wstq", ch) == nullptr)
		{
			cout << "Please enter a w, s,t or q";
			cin >> ch;
		}
		if (ch == 'q')
			break;
		switch (ch)
		{
		case 'w': temp[n]= new Person;
			break;
		case 's': temp[n] = new Gunslmger;
			break;
		case 't': temp[n] = new BadDude;
		}
		cin.get();
		temp[n]->Set();
	}
	cout << "\nHere is your staff:\n";
	int i;
	for (i = 0; i < n; i++)
	{
		cout << endl;
		temp[i]->Show();
	}
	for (i = 0; i < n; i++)
		delete temp[i];
	cout << "Bye\n";
	cin.get();
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值