16级C++课程设计 第二题

来源:互联网 发布:学简单英语口语的软件 编辑:程序博客网 时间:2024/04/25 13:17

2.设计一个宾馆类(学号尾号偶数完成)
私有成员为,宾馆名称,宾馆所有房间编号,采用指针存储动态数组方式存储宾馆房间编号。
重载“+”操作,表示两个宾馆合并,宾馆名称由两个宾馆名称连接到一起,房间编号则是前一个宾馆的房间编号前面加字符“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;}