dynamic objects, agregation and calling member functions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pdring
    New Member
    • Oct 2007
    • 15

    #1

    dynamic objects, agregation and calling member functions

    hi guys,
    can you look at my main frunction and give me any help on how to get this program to work without copying the block of code twice (as in this code).
    I have tryed to put the code into a function and then basically called it twice but the updateBill member function does not work correctly ( it does when the code is copyed ). i also thought about using a for loop, this works but I then must assume that i want to run this code x number of times.
    why does it not work in a function? is it because when you call the function you are creating a new object and when it is run its course the object is out of scope and therefore unable to update?

    Code:
    void main (void)
    {
    char guestName [10];
    int roomNumber = 1;
    float nightsStayed;
    float thisRoomValue = 50;
    float roomTotal = 0;
    
    room newroom;
    
            newroom.allocateGuest();  //this creates a dynamic object (also an aggregate of another class
            newroom.accessFirstName(guestName);
            cout <<"Room: "<<roomNumber<< " has been allocated to guest: "
                                                                  <<guestName<<endl;
            cout <<"how many nights has the guest stayed? : ";
            cin >> nightsStayed;
            newroom.setNoNights(nightsStayed);
            newroom.setRoomCost(thisRoomValue);
            roomTotal = newroom.getBillValue();
            newroom.setBillValue(roomTotal);
            newroom.UpdateBill(roomTotal);
            newroom.deAllocateGuest();
    
            newroom.allocateGuest();
            newroom.accessFirstName(guestName);
            cout <<"Room: "<<roomNumber<< " has been allocated to guest: "
                                                                  <<guestName<<endl;
            cout <<"how many nights has the guest stayed? : ";
            cin >> nightsStayed;
            newroom.setNoNights(nightsStayed);
            newroom.setRoomCost(thisRoomValue);
            roomTotal = newroom.getBillValue();
            newroom.setBillValue(roomTotal);
            newroom.UpdateBill(roomTotal);
            newroom.deAllocateGuest();
    
            getchar();
    }
    I am using borland 5.5 on windows
    Thanks for any help
    Phil
  • arnaudk
    Contributor
    • Sep 2007
    • 425

    #2
    This may not be related to your probem, but your code looks a little fishy. Maybe you're just posting some unrealistic example here to illustrate your problem, but:
    • You setBillValue to the result of getBillValue, where's the logic in that?
    • Having setBillValue, you then updateBillValue which seems superfluous, and update to the same value... ?

    You may want to rethink your design.

    Now, you say that if you enclose your repeated code in a function (which I presume takes as value a (reference to a) room object, guestName variable, etc.) then it doesn't work the second time it is called.
    Is this also true if you remove all functions between allocateGuest and deallocateGuest ?
    If so, perhaps you could post the declaration of that function you tried so we can see what you did and also some illustration of how you allocateGuest and deallocateGuest to make things clear. It could be you're inadvertently creating a local copy of newroom in the function and calling its methods will have no bearing on the newroom in main(), for example. That would be fixed by passing newroom by reference.

    Comment

    • pdring
      New Member
      • Oct 2007
      • 15

      #3
      thanks for replying to my problem,
      the idea behind this project is to declare two classes, guest and room. and make an association between the two classes. the main function is just a way to check that a dynamic object of guest is created within a member function of room class, I have called this allocateGuest() . also, when the guest 'checks out' the dynamic object of guest is destroyed, - deAllocateGuest (). the amount of money owed by each guest is destroyed with the object so that the next guest bill is at zero. but I also need to display the total amount that the room has earned to date. the code I have written no doubt is a bit messy as I am still struggling to learn c++ but it works perfectly as it is, I just dont like the fact that I am calling the member functions twice and am searching for an alternative, as I said putting the code into a function and calling it from main resets the rooms total to zero as well as the guest every time it is called.
      hope this makes some sense of my earlier posting.
      thanks.
      PHIL

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Yes, it does appear that your class model is incorrect.

        True, a room has a guest but it is the guest that has the bill and not the room. I mean, at a real hotel the rooms do not pay the bills. The guests do.

        Now, the room has a rate. So as I see it, your create a series of rooms with varous rates then create guests that can stay in various rooms. You would determine the bill by iterating the rooms the guest has stayed in and multiplied the number of days by the room rate.

        Your class model should mimic the real world.

        Comment

        • arnaudk
          Contributor
          • Sep 2007
          • 425

          #5
          Originally posted by pdring
          ...as I said putting the code into a function and calling it from main resets the rooms total to zero as well as the guest every time it is called.
          See the second part of my reply - your function should take newroom by reference:
          [code=cpp]
          void processroom(roo m& theroom) // theroom passed by reference
          {
          // process theroom
          }

          void processroom(roo m theroom) // theroom passed by value
          {
          // process a local copy of theroom
          }
          [/code]
          In the second version processroom, you can do what you want to theroom in the function but all changes will be lost when the function exits because you are manipulating a local copy of theroom. That could be why the total is zero; theroom you created in main() never changes.

          Comment

          Working...