gpt4 book ai didi

具有成员变量 vector 的 C++ 类

转载 作者:太空狗 更新时间:2023-10-29 21:24:29 25 4
gpt4 key购买 nike

我的任务是制作一个国家度假村的信息系统,能够从文件读取/写入度假村的数据并修改它。

class CTоurist{
string m_StrName;
string m_StrCountry;
int m_iAge;
public:
//constructors, mutators, accessors overloading operator <<,>>

};

我写这门课没有问题。在这里我有一个类,其中包含第一类对象的成员变量 vector

class CHotel
{
string m_strHotelName;
int m_iNumberOfBets;
double m_dAveragePrice; //average price per bet in the hotel
vector <CTourist> m_vecTourists; //vector of tourists rested in the hotel
public:
.....
};

还有一个类 Resort 包含作为第二类对象的成员变量 vector

class CResort
{
string m_ResortName;
unsigned m_Height;
vector<CHotel*> m_Hotels;
public:
.....
};

所以问题来了。我不确定如何为该 vector 变量编写访问器、修改器和构造函数,以便我可以使用它们的属性。感谢您检查,如果有人能帮我弄清楚这些功能,我将不胜感激!

最佳答案

如果我理解正确,您想知道从 cResort 获取酒店的最佳方式。

我会推荐

cHotel* GetHotelByName(std::string& a_sName)
{
for(int i = 0; i < m_Hotels.size(); ++i)
{
if(m_Hotel[i].GetName() == a_sName)
return m_Hotel[i]
}
return nullptr; // if non found return return null
}

并向您的酒店类添加一个 GetName 函数,该函数返回其名称的字符串。这也允许您设置名称等。

关于具有成员变量 vector <another class> 的 C++ 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15986538/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com