0

Greetings all,

I'm working on a classic Elevator Simulator problem and have a quick question:

How can I access a private data member of the container class from one of the contained classes?

My Building class (container) "has a" vector of Floor objects and "has a" vector of Elevator objects. How can I have the elevators be able to access the building's floors?

My Instructor has recommended that we stay away from declaring friends if possible.

Thanks!!!

Dave

3
Contributors
4
Replies
5
Views
8 Years
Discussion Span
Last Post by wildgoose
0

Thanks for the quick reply!

So I need to set the vector of Floors to protected and then derrive the elevators from class Building? That sounds like its kills the OO part of the program. Sounds like I'm missing something. All I need is for the two "Contained" classes to be able to access eachother.

0

Elevators shouldn't be a child of Building, they aren't buildings. You could make it public instead of going about all the hackery or adding getFloors/setFloors methods.

0

Ever hear of a friend class? But friends aren't allowed!

SO, create an access function to READ ONLY the value.


Within the base class.

class Foo
{
private:
    int nFruitCnt;

public:
     Foo();
    virtural ~Foo();

    int GetFruitCount(void) { return nFruitCnt; }
};

So any class that inherits this at any level of inheritance can get the value in the base class.
You can add a set function as well. It's an access member function!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.