zstuc++小实验:酒店住房退房,要求使用运算符重载,纯虚函数,多态

仅供参考,禁止抄袭!!!

实验要求:
实现对抽象类的继承,通过operator函数调用的形式,实现运算符的重载

实验内容:
一个小型快捷酒店有5个房间,其中3个标准间,2个大床间,可在柜台办理入住或退房。

标准间180元/天,大床间120元/天,押金都是100元。 请利用类的多态特性实现该系统。

 
这个需要事先创建一个number.txt的文件,并在其中输入3 5,分别代表标准间个数和大床房个数

在这里插入图片描述
在这里插入图片描述

 
代码如下

#include
#include
using namespace std;

//房间的基类
class Room
{
public:

	//预定房间
	virtual void Reserve() = 0;

	//退订房间
	virtual void Cancel() = 0;

	int m_Num;//房间剩余数
	int m_Deposit;//押金
};

//标准间
class StandardRoom:public Room
{
public:

	//预定房间
	virtual void Reserve();

	//退订房间
	virtual void Cancel();
};

int stand;//标准间房间个数
int big;//大床房房间个数
int num;//顾客预定房间个数

//预定标准间
void StandardRoom::Reserve()
{
	//打开文件
	fstream fs;
	fs.open("number.txt", ios::in );
	fs >> stand >> big;
	cout << "标准间剩余:" << stand << "个!" << endl
		<< "请输入您要预定的房间个数!" << endl;
	while (true)
	{
		cin >> num;
		if (num > stand)
		{
			cout << "输入有误,请重新输入!" << endl;
		}
		else
		{
			stand -= num;
			break;
		}
	}
	//cout << "stand:" << stand << endl;
	fs.close();
	fs.open("number.txt", ios::trunc|ios::out);
	fs << stand << " " << big;

	cout << "标准间180元/天,押金100元" << endl;
	cout << "预定成功!" << endl;
	
	fs.close();
}

//退订标准间
void StandardRoom::Cancel()
{
	//打开文件
	fstream fs;
	fs.open("number.txt", ios::in);
	fs >> stand >> big;
	cout << "标准间剩余:" << stand << "个!" << endl
		<< "请输入您要退订的房间个数!" << endl;
	while (true)
	{
		cin >> num;
		if (num + stand>3)
		{
			cout << "输入有误,请重新输入!" << endl;
		}
		else
		{
			stand += num;
			break;
		}
	}
	//cout << "stand:" << stand << endl;
	fs.close();
	fs.open("number.txt", ios::trunc | ios::out);
	fs << stand << " " << big;

	cout << "退订成功,已退还押金!" << endl;
	fs.close();
}
//大床间
class BigBedRoom :public Room
{
public:

	//预定房间
	virtual void Reserve();

	//退订房间
	virtual void Cancel();
};

//预定大床间
void BigBedRoom::Reserve()
{

	//打开文件
	fstream fs;
	fs.open("number.txt", ios::in);
	fs >> stand >> big;
	cout << "大床间剩余:" << big << "个!" << endl
		<< "请输入您要预定的房间个数!" << endl;
	while (true)
	{
		cin >> num;
		if (num > big)
		{
			cout << "输入有误,请重新输入!" << endl;
		}
		else
		{
			big -= num;
			break;
		}
	}
	//cout << "stand:" << stand << endl;
	fs.close();
	fs.open("number.txt", ios::trunc | ios::out);
	fs << stand << " " << big;

	cout << "大床间120元/天,押金100元" << endl;
	cout << "预定成功!" << endl;
	
	fs.close();
}

//退订大床间
void BigBedRoom::Cancel()
{
	//打开文件
	fstream fs;
	fs.open("number.txt", ios::in);
	fs >> stand >> big;
	cout << "大床间剩余:" << big << "个!" << endl
		<< "请输入您要退订的房间个数!" << endl;
	while (true)
	{
		cin >> num;
		if (num + big>5)
		{
			cout << "输入有误,请重新输入!" << endl;
		}
		else
		{
			big += num;
			break;
		}
	}
	//cout << "stand:" << stand << endl;
	fs.close();
	fs.open("number.txt", ios::trunc | ios::out);
	fs << stand << " " << big;

	cout << "退订成功,已退还押金!" << endl;
	fs.close();
}
void ShowMenu()
{
	cout << " ------------------------------------" << endl;
	cout << "|                                    |" << endl;
	cout << "|        请输入您要进行的操作!      |" << endl;
	cout << "|                                    |" << endl;
	cout << "|            1、入住标准间           |" << endl;
	cout << "|                                    |" << endl;
	cout << "|            2、入住大床间           |" << endl;
	cout << "|                                    |" << endl;
	cout << "|            3、退订标准间           |" << endl;
	cout << "|                                    |" << endl;
	cout << "|            4、退订大床间           |" << endl;
	cout << "|                                    |" << endl;
	cout << "|            5、查看房间剩余         |" << endl;
	cout << "|                                    |" << endl;
	cout << "|            0、退出                 |" << endl;
	cout << "|                                    |" << endl;
	cout << " ------------------------------------" << endl;
}

StandardRoom sm;
BigBedRoom bbr;

ostream& operator<<(ostream& out, StandardRoom& s)
{
	out << "标准间剩余:" << s.m_Num << endl;
	return out;
}

ostream& operator<<(ostream& out, BigBedRoom& b)
{
	out << "大床间剩余:" << b.m_Num << endl;
	return out;
}
void Check()
{
	//打开文件
	fstream fs;
	fs.open("number.txt", ios::in);
	fs >> stand >> big;

	sm.m_Num = stand;
	bbr.m_Num = big;

	cout << sm << bbr;
}

int main()
{
	

	int choice;
	//登陆界面
	while (true)
	{
		ShowMenu();
		cin >> choice;

		//入住标准间
		if (choice == 1)
		{
			sm.Reserve();
			system("pause");
			system("cls");
		}
		//入住大床间
		else if (choice == 2)
		{
			bbr.Reserve();
			system("pause");
			system("cls");
		}
		//退订标准房
		else if (choice == 3)
		{
			sm.Cancel();
			system("pause");
			system("cls");
		}
		//退订大床房
		else if (choice == 4)
		{
			bbr.Cancel();
			system("pause");
			system("cls");
		}
		//查看房间余量
		else if (choice == 5)
		{
			Check();
			system("pause");
			system("cls");
		}
		//退出
		else if (choice == 0)
		{
			break;
			cout << "欢迎您下次使用!" << endl;

			system("pause");
			system("cls");
		}
		else
		{
			cout << "输入错误!" << endl;
			system("pause");
			system("cls");
		}
	}
}

如果这篇文章对你有帮助,记得点赞关注哦

你可能感兴趣的:(c++)