user2272017
user2272017

Reputation: 33

C++ Class with member varialble vector <another class>

I have an assignment to make Information system about resorts in a country, be able to read/write data for the resorts from/to file and modifying it.

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

};

I don't have problem writing this class. And here I have class which contains as member variable vector of the first class's objects

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:
.....
};

And one more class Resort containing as member variable vector of the second class's objects

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

So here is the problem. I'm not sure how to write the accessor,mutator and constructors for that vector variable so I can use them property. Thank you for checking and if someone could help me figure out these functions I'll be really grateful!

Upvotes: 3

Views: 1538

Answers (3)

Joshua
Joshua

Reputation: 441

You can put the accessor and mutator functions in CTourist just like you would if they were not being stored in a Vector.

To utilize them once they are in CHotel you could add a function in CHotel that returns a pointer to a CTourist.

// Access a CTourist
Hotel.getTourist(1)->setName("Tourist name");

Adding a method that returns the number of tourist that visited a hotel would make it easier to loop through them.

for(int i = o; i < Hotel.touristCount(); ++i)
{
  // Do something useful
  std:: cout << "Hello " << Hotel.getTourist(i)->getName();
}

In that case your CHotel::touristCount() would be a wrapper around the vector<>.size();

If you do not want code outside of CHotel to have direct access to a CTourist object then create wrapper functions in CHotel that do what you would want to do externally.

i.e. std::cout << Hotel.getTouristName(1);

instead of

std::cout << Hotel.getTourist(1)->getName();

Upvotes: 1

Martin Prikryl
Martin Prikryl

Reputation: 202272

1) Accessor, mutator: There are plenty of options.

You can create another class like CTouristList (and CHotelList respectively), that wraps the vector, have it referenced from the CHotel class (accessor methods like CTouristList& CHotel::GetTouristList() and const CTouristList& CHotel::GetTouristList() const) and implement methods like CTouristList::Add, CTouristList::Remove, CTouristList::Get, etc.

Or you can add methods like CHotel::AddTourist() directly on the CHotel class.

2) Constructor. Nothing needed in constructor. But for vector<CHotel*> you may need destructor in CResort to explicitly free the CHotel instances. Though not sure why you want to use pointers to CHotel.

Upvotes: 1

Zanven
Zanven

Reputation: 154

if i understand correctly you want to know the best way to get your hotels from cResort.

i would recommend

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
}

and add a GetName function to your hotel class which returns a string of its name. this also allows you to SetName etc.

Upvotes: 1

Related Questions