Dark Bit Factory & Gravity

PROGRAMMING => C / C++ /C# => Topic started by: mike_g on March 28, 2007

Title: Passing Objects into a function
Post by: mike_g on March 28, 2007
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.
Code: [Select]
#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.
Title: Re: Passing Objects into a function
Post by: Stonemonkey on March 28, 2007
I'm not totally sure as I'm trying to pick this sort of thing up myself but I think you should be sending the address of the first element of the array to the pointer in your function.
Title: Re: Passing Objects into a function
Post by: mike_g on March 28, 2007
Yeah I think you are right stonemonkey, but I already had a play around with several possibilities including using & to pass the address in but i couldent get it to work. So theres got to be something else wrong with it too.
Title: Re: Passing Objects into a function
Post by: Stonemonkey on March 28, 2007
I'm not sure if classes are done the same way but to do it with structs I think it would be something like:

void InitRooms(rooms*);


and to call it:
//send address of first room to the rooms pointer in the function
InitRooms(&room[0]);



with the function:

//----- INITIALIZE ROOMS ---------//
void InitRooms(rooms *room)
{
.....
Title: Re: Passing Objects into a function
Post by: mike_g on March 28, 2007
Quote from: stonemonkey
void InitRooms(rooms *room)

I hadent tried this yet. It looks good so I tried it, and now when I compile I only get 1 error message. Which seems like an improvement:

Code: [Select]
43 C:\Documents and Settings\Mike\Desktop\C++\Assignment Stage 2a.cpp cannot convert `rooms*' to `int*' for argument `1' to `void InitRooms(int*)'
I think that theres now just something wrong with the way I declare my function:
Code: [Select]
void InitRooms(int*);
But then again I may be wrong.
Title: Re: Passing Objects into a function
Post by: Stonemonkey on March 28, 2007
It should be a rooms pointer in the declaration, not an int pointer.
Title: Re: Passing Objects into a function
Post by: mike_g on March 28, 2007
It works ;D  I guess I should have been able to work that bit out myself really  :-\ , but hey thanks for the help :)
Title: Re: Passing Objects into a function
Post by: Stonemonkey on March 28, 2007
Cool (also for me as I'm just getting into this) but I hope I've not just done your homework for you :p
Title: Re: Passing Objects into a function
Post by: mike_g on March 28, 2007
No need to worry yourself, I still got plenty left to do ;)
Title: Re: Passing Objects into a function
Post by: Jim on March 28, 2007
Glad you worked it out!  In this case when you have an array of things,
Code: [Select]
thing arrayofthings[10];
then both
Code: [Select]
thing *thingptr;
thingptr = &arrayofthings[0];
thingptr = arrayofthings;
are pointers to the first thing (element) of the array.

Jim