16级C++课程设计 第二题
2017-06-09 20:25
381 查看
2.设计一个宾馆类(学号尾号偶数完成)
私有成员为,宾馆名称,宾馆所有房间编号,采用指针存储动态数组方式存储宾馆房间编号。
重载“+”操作,表示两个宾馆合并,宾馆名称由两个宾馆名称连接到一起,房间编号则是前一个宾馆的房间编号前面加字符“1”,后面一个宾馆的房间编号前面加字符“2”,然后将房间编号合成一个数组
重载[]操作直接获得第i个房间的编号。
私有成员为,宾馆名称,宾馆所有房间编号,采用指针存储动态数组方式存储宾馆房间编号。
重载“+”操作,表示两个宾馆合并,宾馆名称由两个宾馆名称连接到一起,房间编号则是前一个宾馆的房间编号前面加字符“1”,后面一个宾馆的房间编号前面加字符“2”,然后将房间编号合成一个数组
重载[]操作直接获得第i个房间的编号。
#include <iostream> #include <string> #include <vector> using namespace std; class hotel { string name; vector<string>No; public: hotel(){} hotel(string n, vector<string>mem) :name(n), No(mem) {}; string getname() { return name; } vector<string> getmem() { return No; } void add(string n) { No.push_back(n); } void setname(string n) { name = n; } void del(string name) { for (auto i = No.begin(); i != No.end(); ++i) if (*i == name) { No.erase(i); cout << "删除成功" << endl; return; } cout << "删除失败" << endl; } void output() { cout << name << endl; for (auto i : No) cout << i << " "; cout << endl; } string operator [](int index) { if (index >= No.size() || index < 0) { cout << "下标错误!返回空串。" << endl; return ""; } return No[index]; } }; hotel operator +(hotel a, hotel b) { vector<string>t1(a.getmem()); for (auto &i : t1) i = "2:" + i; t1.reserve(100); vector<string>t2(b.getmem()); for (auto &i : t2) i = "1:" + i; t1.insert(t1.end(), t2.begin(), t2.end()); return hotel(a.getname() + b.getname(),t1); } int main() { hotel a; a.setname("第一家宾馆"); a.add("1"); a.add("2"); hotel b; b.setname(" 第二家宾馆"); b.add("1"); b.add("2"); hotel c = a + b; c.del("2:2"); c.output(); cout << c[0] << endl; return 0; }
相关文章推荐
- 16级C++课程设计 第三题
- 16级C++课程设计 题目1 “高校人员信息管理系统设计”
- C++课程设计指导书
- C++版数据结构课程设计(一元多项式)
- [设计] C++课程设计-ATM (纪念大一)
- 关于c++ 课程设计(学生信息管理系统)
- C++课程设计总结
- c++程序课程设计的内容--职工工资管理系统
- C++课程设计 之 俄罗斯方块
- C++课程设计( 南阳康佳公司的设备管理系统 )
- C++课程设计 之 俄罗斯方块
- c++课程设计之通讯录电话簿管理设计
- Linux下写的一个C++课程设计
- C/C++语言课程设计任务书
- c++课程设计题目--"信用卡帐户管理系统"
- c++ 课程设计 ——公司设备管理系统
- 课程设计C++
- C++课程设计 面积运算
- c++课程设计
- [C++]数据结构课程设计:迷宫老鼠1.0