Author Topic: Passing Objects into a function  (Read 5399 times)

0 Members and 1 Guest are viewing this topic.

Offline mike_g

  • Amiga 1200
  • ****
  • Posts: 435
  • Karma: 34
    • View Profile
Passing Objects into a function
« 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.

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Passing Objects into a function
« Reply #1 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.
« Last Edit: March 28, 2007 by Stonemonkey »

Offline mike_g

  • Amiga 1200
  • ****
  • Posts: 435
  • Karma: 34
    • View Profile
Re: Passing Objects into a function
« Reply #2 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.

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Passing Objects into a function
« Reply #3 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)
{
.....

Offline mike_g

  • Amiga 1200
  • ****
  • Posts: 435
  • Karma: 34
    • View Profile
Re: Passing Objects into a function
« Reply #4 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.

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Passing Objects into a function
« Reply #5 on: March 28, 2007 »
It should be a rooms pointer in the declaration, not an int pointer.

Offline mike_g

  • Amiga 1200
  • ****
  • Posts: 435
  • Karma: 34
    • View Profile
Re: Passing Objects into a function
« Reply #6 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 :)

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Passing Objects into a function
« Reply #7 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

Offline mike_g

  • Amiga 1200
  • ****
  • Posts: 435
  • Karma: 34
    • View Profile
Re: Passing Objects into a function
« Reply #8 on: March 28, 2007 »
No need to worry yourself, I still got plenty left to do ;)

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Passing Objects into a function
« Reply #9 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
Challenge Trophies Won: