I'm working on a C++ assignment at the moment and I want to pass an array of objects into a function. I think I got to use a pointer to the array, but what variable type would it be? Heres my code anyway. My problem is with the InitRooms() function.
#include <iostream>
#include <string.h>
using namespace std;
#define num_rooms 10
class guests
{
private:
char surname[10];
int bill;
public:
guests();
void EndGuest();
char* GetName();
void AddCharge(int);
float GetBill();
};
class rooms
{
private:
int number;
float cost;
public:
rooms();
int GetNumber();
float GetCost();
//Added setter functions
void SetNumber(int);
void SetCost(float);
};
void InitRooms(int*);
int main()
{
int i;
rooms room[num_rooms]; //Constuct 10 rooms as globals
InitRooms(*room[]); //Set the variables for each room
for(i=1; i<=10; i++)
cout << "Room: " << room[i].GetNumber() << " at $" << room[i].GetCost() << "\n";
cin >> i;
}
//---- GUEST MEMBER FUNCTIONS ----//
guests::guests()
{
cout << "enter name: ";
cin >> surname;
bill=0;
}
char* guests::GetName() { return surname; }
void guests::AddCharge(int charge) { bill=bill+charge; }
float guests::GetBill() { return bill; }
//---- ROOM MEMBER FUNCTIONS -----//
rooms::rooms()
{
number=0;
cost=0;
}
int rooms::GetNumber() { return number; }
float rooms::GetCost() { return cost; }
void rooms::SetNumber(int n) { number = n; }
void rooms::SetCost(float c) { cost = c; }
//----- INITIALIZE ROOMS ---------//
void InitRooms(int *room[])
{
for(int i=1; i<=10; i++) room[i].SetNumber(i);
room[1].SetCost(100); room[2].SetCost(90);
room[3].SetCost(85.5); room[4].SetCost(80);
room[5].SetCost(80); room[6].SetCost(50);
room[7].SetCost(50); room[8].SetCost(45.5);
room[9].SetCost(45.5); room[10].SetCost(40);
}
I know an easy way to get this to work would be to have the array of room as a global. One of my teachers last year went berserk on me for using a global, so I'd like find out how to do this. Cheers.