samedi 8 janvier 2022

How to store objects in an array which is private member of class in c++?

I want to store Floor class objects in array of Hotel class which is private member. Here is my code:

class Hotel {
private:
    string name;
    Floor floor_ptr[3]; //error
    int number_of_floors;
public:
    Hotel() {
        name = "5-STAR HOTEL";
        number_of_floors = 3;
    }
    Floor set_floor_arr(Floor& obj) {
        for (int n = 0; n < number_of_floors; n++) {
            floor_ptr[n] = new Floor();//error 
            return floor_ptr[n];
        }
    }
};
class Floor {
private:
    int floor_number;
    int number_of_rooms;
public:
    Floor() :number_of_rooms{ 30 } {}
    void set_floor_number(int fn) {
        floor_number = fn;
    }
    int get_floor_number() {
        return floor_number;
    }
 };

I tried creating pointers in Hotel class and changing return type but every time gets a new error. So, how can I store objects of Floor class in array of Hotel class?

Aucun commentaire:

Enregistrer un commentaire