Hi, guys! I'm new in OOP, so i have some problems here...
I'm doing a model of an elevator. There are three classes: building, elevator(which is located in a building), passenger.

I have a class "building" which contains as a composition another class -"elevator". Class "elevator" has buttons(function-elements) inside, such as: ComeAndGetMe(), TakeMeToLevel(), etc.. So, the class passenger appears on randomized level in the main() program n times(consequently), and by using that buttons transports from level to level.

Question1: for instance, i have two building variables in the main(): b1 and b2. When passenger p1 appears, how can i make it to know in which building it appeared?? I mean, in order to use th buttons each passenger must know the building it belongs to, right?

Question2: If elevator e1 is a composition of a building b1, how can i use buttons of an elevator? b1.e1.TakeMeTo(5)????

kvprajapati commented: Title of thread must reflect your question. -2

Recommended Answers

All 11 Replies

For your question # 2, you can use the public interface of the elevator,
but it should be only allowed to be used by passenger, and not the
building class.

And I am not sure what question # 1 is asking. Care to elaborate.

You can keep a pointer to a b1 object in the passenger class.
So when a passenger 'spawns' you set one of the building objects in the passenger class.

Then you can call p1.building->ComeGetMe();

I think the building should then take care of calling the elevator function, because only the building knows which elevators it has (because you have 2 building you probably also have 2 elevators). If later you expand with multiple elevators you can set sort of a 'position' in the passenger that you hand over to the ComeGetMe() function and then the building calls the correct ComeGetMe() function on the correct elevator class.

I hope that made sense.

An acute thread title. Does the thread title need a fixing.

Hi! I've finished my elevator and it works good!)
If you have some ideas to improve, plz, let me know!

//***ELEVATOR_MODEL.DEV***

//BUILDING.H
//The function-elements are in BUILDING.CPP
#ifndef BUILDING_H
#define BUILDING_H
#include <iostream.h>
#include <stdlib.h>
#include "elevator.h"
class building
{
public:
       building(int=2);
       int GetMaxLevel() const;
       void SetMaxLevel(int);
       elevator e1;
private:
        int MaxLevel;
};
#endif


//BIULDING.CPP
//Function-elements of BUILDING class
#include <iostream.h>
#include <stdlib.h>
#include "building.h"

building::building(int l)
{
 SetMaxLevel(l);
}

int building::GetMaxLevel() const
{
 return MaxLevel;
}

void building::SetMaxLevel(int level)
{
 if(level>1) MaxLevel=level; else MaxLevel=2;
}


//ELEVATOR.H
#ifndef ELEVATOR_H
#define ELEVATOR_H
#include <iostream.h>
#include <stdlib.h>
class elevator
{
 public:
        elevator();
        void MoveTo(int);
        int GetPosition() const;
        void OpenDoor();
        void CloseDoor();
 private:
         int position;
         bool is_door_opened;
};
#endif


//ELEVATOR.CPP
#include <iostream.h>
#include <stdlib.h>
#include "elevator.h"

elevator::elevator()
{
 position=1;
 is_door_opened=false;
}

void elevator::MoveTo(int l)
{
 if(l!=position)
 {
  if(is_door_opened==true) CloseDoor();
  cout<<"Elevator has moved to level "<<l
      <<" from level "<<position<<endl;
  position=l;
  OpenDoor();
 }
 else cout<<"!!Trying to set current level:no reaction"<<endl;
}

void elevator::OpenDoor()
{
 is_door_opened=true;
 cout<<"The Doors are opened"<<endl;
}

void elevator::CloseDoor()
{
 is_door_opened=false;
 cout<<"The Doors are closed"<<endl;
}

int elevator::GetPosition() const
{
 return position;
}


//PASSENGER.H
#ifndef PASSENGER_H
#define PASSENGER_H
#include <iostream.h>
#include <stdlib.h>
#include "building.h"
#include "elevator.h"
class passenger
{
 public:
        passenger();
        void SetBuilding(building &);
        void CallElevator();      //Button
        void ChoseLevel(int);     //Button
        void SetLocation(int);
 private:
         building b1;
         int current_level;
         bool allowed;
};
#endif


//PASSENGER.CPP
#include <iostream.h>
#include <stdlib.h>
#include "building.h"
#include "elevator.h"
#include "passenger.h"

passenger::passenger()
{
 allowed=false;
}
//-------------------------------------------------------------------
void passenger::SetBuilding(building &house)
{
 b1=house;
 allowed=true;
 current_level=1;
 cout<<"The passenger is in the building"<<endl;
}
//---------------------------------------------------------------------
void passenger::SetLocation(int level)
{
 if(allowed==true)
 {
  if( level<=( b1.GetMaxLevel() ) )
  {
   current_level=level;
   cout<<"The passenger is on the "<<current_level<<" level"<<endl;
  }
  else cout<<"Passenger is Out of MaxLevel!"<<endl;
 }
 else cout<<"Set the building location of passenger first!"<<endl;
}
//-----------------------------------------------------------------------
void passenger::CallElevator()
{
 if(allowed==true)
 {
  cout<<"The passenger has called the elevator"<<endl;
  if(b1.e1.GetPosition()!=current_level) b1.e1.MoveTo(current_level);
  else b1.e1.OpenDoor();
 }
 else cout<<"Set the building location of passenger first!"<<endl;
}
//-----------------------------------------------------------------------
void passenger::ChoseLevel(int l)
{
 if(allowed==true)
 {
  if( l<=(b1.GetMaxLevel() ) )
  {
   cout<<"The passenger pushed button "<<l<<" in the elevator"<<endl;
   b1.e1.MoveTo(l);
   current_level=l;
  }
  else cout<<"Level is Out of MaxLevel!"<<endl;
 }
 else cout<<"Set the building location of passenger first!"<<endl;
}


//ELEVATOR_MODEL_DRIVER.CPP
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
#include "building.h"
#include "elevator.h"
#include "passenger.h"

using namespace std;

main()
{
const int i=5;
building office(i);   //5-leveled building has been created
passenger worker1;
int start, finish, n;  //the passenger will appear on level "start"
					  //and go to level "finish"
srand(time(NULL));

cin>>n;    //n times passenger will appear on different levels

worker1.SetBuilding(office);  //the passenger now is in a building "office"

for(int j=0; j<n; j++)
{
 start=finish=(rand() % i) + 1;
 while(start==finish){ finish=(rand() % i) + 1; }
 cout<<endl;
 worker1.SetLocation(start);
 worker1.CallElevator();
 worker1.ChoseLevel(finish);
}
system("PAUSE");
return 0;
}

forgot the int in int main(), or does your compiler support default
int. And system command, argghhh. Throw them away. cin.get() would
be a better choice to "pause" the screen.

Looks good, but there is a piece of code I would like to show you:

if(is_door_opened==true)

You can simplify this conditionnal expression with:

if (IsDoorOpened())

and make this method private, if no other entity external to the class has a need for it. This is a refactoring tip (Simplify Conditional Expressions) and not a object-oriented tip. When you have a lot of AND and OR in a condition, better use a method like the one presented to make the code more readable.

Hi guys, a editted my elevator pro: i used pointer to building class in a passenger class, so it uses exact building instead of makng a copy of it.
Great thnx to "TheLamb"!:))))

//***ELEVATOR_MODEL.DEV***

//BUILDING.H
//The function-elements are in BUILDING.CPP
#ifndef BUILDING_H
#define BUILDING_H
#include <iostream.h>
#include <stdlib.h>
#include "elevator.h"
class building
{
public:
       building(int=2);
       int GetMaxLevel() const;
       void SetMaxLevel(int);
       elevator e1;
private:
        int MaxLevel;
};
#endif


//BIULDING.CPP
//Function-elements of BUILDING class
#include <iostream.h>
#include <stdlib.h>
#include "building.h"

building::building(int l)
{
 SetMaxLevel(l);
}

int building::GetMaxLevel() const
{
 return MaxLevel;
}

void building::SetMaxLevel(int level)
{
 if(level>1) MaxLevel=level; else MaxLevel=2;
}


//ELEVATOR.H
#ifndef ELEVATOR_H
#define ELEVATOR_H
#include <iostream.h>
#include <stdlib.h>
class elevator
{
 public:
        elevator();
        void MoveTo(int);
        int GetPosition() const;
        void OpenDoor();
        void CloseDoor();
 private:
         int position;
         bool is_door_opened;
};
#endif


//ELEVATOR.CPP
#include <iostream.h>
#include <stdlib.h>
#include "elevator.h"

elevator::elevator()
{
 position=1;
 is_door_opened=false;
}

void elevator::MoveTo(int l)
{
 if(l!=position)
 {
  if(is_door_opened==true) CloseDoor();
  cout<<"Elevator has moved to level "<<l
      <<" from level "<<position<<endl;
  position=l;
  OpenDoor();
 }
 else cout<<"!!Trying to set current level:no reaction"<<endl;
}

void elevator::OpenDoor()
{
 is_door_opened=true;
 cout<<"The Doors are opened"<<endl;
}

void elevator::CloseDoor()
{
 is_door_opened=false;
 cout<<"The Doors are closed"<<endl;
}

int elevator::GetPosition() const
{
 return position;
}


//PASSENGER.H
#ifndef PASSENGER_H
#define PASSENGER_H
#include <iostream.h>
#include <stdlib.h>
#include "building.h"
#include "elevator.h"
class passenger
{
 public:
        passenger();
        void SetBuilding(building &);
        void CallElevator();      //Button
        void ChoseLevel(int);     //Button
        void SetLocation(int);
 private:
         building *b1;
         int current_level;
         bool allowed;
};
#endif


//PASSENGER.CPP
#include <iostream.h>
#include <stdlib.h>
#include "building.h"
#include "elevator.h"
#include "passenger.h"

passenger::passenger()
{
 allowed=false;
}
//-------------------------------------------------------------------
void passenger::SetBuilding(building &house)
{
 b1=&house;
 allowed=true;
 current_level=1;
 cout<<"The passenger is in the building"<<endl;
}
//---------------------------------------------------------------------
void passenger::SetLocation(int level)
{
 if(allowed==true)
 {
  if( level<=( b1->GetMaxLevel() ) )
  {
   current_level=level;
   cout<<"The passenger is on the "<<current_level<<" level"<<endl;
  }
  else cout<<"Passenger is Out of MaxLevel!"<<endl;
 }
 else cout<<"Set the building location of passenger first!"<<endl;
}
//-----------------------------------------------------------------------
void passenger::CallElevator()
{
 if(allowed==true)
 {
  cout<<"The passenger has called the elevator"<<endl;
  if(b1->e1.GetPosition()!=current_level) b1->e1.MoveTo(current_level);
  else b1->e1.OpenDoor();
 }
 else cout<<"Set the building location of passenger first!"<<endl;
}
//-----------------------------------------------------------------------
void passenger::ChoseLevel(int l)
{
 if(allowed==true)
 {
  if( l<=(b1->GetMaxLevel() ) )
  {
   cout<<"The passenger pushed button "<<l<<" in the elevator"<<endl;
   b1->e1.MoveTo(l);
   current_level=l;
  }
  else cout<<"Level is Out of MaxLevel!"<<endl;
 }
 else cout<<"Set the building location of passenger first!"<<endl;
}

//ELEVATOR_MODEL_DRIVER.CPP
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
#include "building.h"
#include "elevator.h"
#include "passenger.h"

using namespace std;

main()
{
const int i=5;
building office(i);
int start, finish, n, j;

srand(time(NULL));

cin>>n;

for(j=0; j<n; j++)
{
 start=finish=(rand() % i) + 1;
 while(start==finish){ finish=(rand() % i) + 1; }
 cout<<endl;
 passenger *ptr = new passenger;
    ptr->SetBuilding(office);
    ptr->SetLocation(start);
    ptr->CallElevator();
    ptr->ChoseLevel(finish);
 delete ptr;
}
system("PAUSE");
return 0;
}

I've just read through your code a little(not detailed) and saw this:

building::building(int l)
{
       SetMaxLevel(l);
}

But because this is the constructor of building there is a faster way to initialize variables, you can do it like this:

building::building( int l ) :
     MaxLevel( l )

With the same method you can do the elevator constructor... but I will leave it up to you to find out how to do it ;).

Btw: the advantage of initializing your variables like this is that the compiler doesn't need to copy the 'l' variable, but you can find the exact details on google.

Also it is good practice to prefix private variables with m_ (so for example m_MaxLevel). Of course this is personal preference but later when you are using MaxLevel you will always see that it is private then.

Thnx for advises very much!:)))

But what if the parameter "l" will be zero or negative?? SetMaxLevel function checks it, thats why i used it there.
Anyway, thank u!

const int i=5;
building office(i);

You define the max level yourself before you hand it over to office - so you have full control over it. If later you want the user of your application to enter a maxlevel then you should check their input before creating the building object, and if they enter <= 0 you give them an error message and don't create the building.

Mm, ok! thnx! do u know some article about the method you gave me?(i could not find anything in google..)

P.S.: i'll definitely use your advise in the future, but in this program the driver can be different, maybe i'll write a driver where the user sets the max level? that's why i'll leave as it now.:)))

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.