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

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值