943,678 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1024
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 22nd, 2009
-2

PLZ!!!! HEEEELP!!! OOP Questions!

Expand Post »
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)????
Last edited by Acute; Aug 22nd, 2009 at 12:38 pm.
Similar Threads
Reputation Points: 8
Solved Threads: 3
Junior Poster
Acute is offline Offline
128 posts
since Mar 2009
Aug 22nd, 2009
0

Re: PLZ!!!! HEEEELP!!! OOP Questions!

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.
Last edited by firstPerson; Aug 22nd, 2009 at 1:16 pm.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Aug 23rd, 2009
0

Re: PLZ!!!! HEEEELP!!! OOP Questions!

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.
Reputation Points: 193
Solved Threads: 75
Posting Pro in Training
thelamb is offline Offline
426 posts
since Aug 2008
Aug 23rd, 2009
0

Re: PLZ!!!! HEEEELP!!! OOP Questions!

An acute thread title. Does the thread title need a fixing.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Aug 30th, 2009
0

Re: PLZ!!!! HEEEELP!!! OOP Questions!

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

c++ Syntax (Toggle Plain Text)
  1. //***ELEVATOR_MODEL.DEV***
  2.  
  3. //BUILDING.H
  4. //The function-elements are in BUILDING.CPP
  5. #ifndef BUILDING_H
  6. #define BUILDING_H
  7. #include <iostream.h>
  8. #include <stdlib.h>
  9. #include "elevator.h"
  10. class building
  11. {
  12. public:
  13. building(int=2);
  14. int GetMaxLevel() const;
  15. void SetMaxLevel(int);
  16. elevator e1;
  17. private:
  18. int MaxLevel;
  19. };
  20. #endif
  21.  
  22.  
  23. //BIULDING.CPP
  24. //Function-elements of BUILDING class
  25. #include <iostream.h>
  26. #include <stdlib.h>
  27. #include "building.h"
  28.  
  29. building::building(int l)
  30. {
  31. SetMaxLevel(l);
  32. }
  33.  
  34. int building::GetMaxLevel() const
  35. {
  36. return MaxLevel;
  37. }
  38.  
  39. void building::SetMaxLevel(int level)
  40. {
  41. if(level>1) MaxLevel=level; else MaxLevel=2;
  42. }
  43.  
  44.  
  45. //ELEVATOR.H
  46. #ifndef ELEVATOR_H
  47. #define ELEVATOR_H
  48. #include <iostream.h>
  49. #include <stdlib.h>
  50. class elevator
  51. {
  52. public:
  53. elevator();
  54. void MoveTo(int);
  55. int GetPosition() const;
  56. void OpenDoor();
  57. void CloseDoor();
  58. private:
  59. int position;
  60. bool is_door_opened;
  61. };
  62. #endif
  63.  
  64.  
  65. //ELEVATOR.CPP
  66. #include <iostream.h>
  67. #include <stdlib.h>
  68. #include "elevator.h"
  69.  
  70. elevator::elevator()
  71. {
  72. position=1;
  73. is_door_opened=false;
  74. }
  75.  
  76. void elevator::MoveTo(int l)
  77. {
  78. if(l!=position)
  79. {
  80. if(is_door_opened==true) CloseDoor();
  81. cout<<"Elevator has moved to level "<<l
  82. <<" from level "<<position<<endl;
  83. position=l;
  84. OpenDoor();
  85. }
  86. else cout<<"!!Trying to set current level:no reaction"<<endl;
  87. }
  88.  
  89. void elevator::OpenDoor()
  90. {
  91. is_door_opened=true;
  92. cout<<"The Doors are opened"<<endl;
  93. }
  94.  
  95. void elevator::CloseDoor()
  96. {
  97. is_door_opened=false;
  98. cout<<"The Doors are closed"<<endl;
  99. }
  100.  
  101. int elevator::GetPosition() const
  102. {
  103. return position;
  104. }
  105.  
  106.  
  107. //PASSENGER.H
  108. #ifndef PASSENGER_H
  109. #define PASSENGER_H
  110. #include <iostream.h>
  111. #include <stdlib.h>
  112. #include "building.h"
  113. #include "elevator.h"
  114. class passenger
  115. {
  116. public:
  117. passenger();
  118. void SetBuilding(building &);
  119. void CallElevator(); //Button
  120. void ChoseLevel(int); //Button
  121. void SetLocation(int);
  122. private:
  123. building b1;
  124. int current_level;
  125. bool allowed;
  126. };
  127. #endif
  128.  
  129.  
  130. //PASSENGER.CPP
  131. #include <iostream.h>
  132. #include <stdlib.h>
  133. #include "building.h"
  134. #include "elevator.h"
  135. #include "passenger.h"
  136.  
  137. passenger::passenger()
  138. {
  139. allowed=false;
  140. }
  141. //-------------------------------------------------------------------
  142. void passenger::SetBuilding(building &house)
  143. {
  144. b1=house;
  145. allowed=true;
  146. current_level=1;
  147. cout<<"The passenger is in the building"<<endl;
  148. }
  149. //---------------------------------------------------------------------
  150. void passenger::SetLocation(int level)
  151. {
  152. if(allowed==true)
  153. {
  154. if( level<=( b1.GetMaxLevel() ) )
  155. {
  156. current_level=level;
  157. cout<<"The passenger is on the "<<current_level<<" level"<<endl;
  158. }
  159. else cout<<"Passenger is Out of MaxLevel!"<<endl;
  160. }
  161. else cout<<"Set the building location of passenger first!"<<endl;
  162. }
  163. //-----------------------------------------------------------------------
  164. void passenger::CallElevator()
  165. {
  166. if(allowed==true)
  167. {
  168. cout<<"The passenger has called the elevator"<<endl;
  169. if(b1.e1.GetPosition()!=current_level) b1.e1.MoveTo(current_level);
  170. else b1.e1.OpenDoor();
  171. }
  172. else cout<<"Set the building location of passenger first!"<<endl;
  173. }
  174. //-----------------------------------------------------------------------
  175. void passenger::ChoseLevel(int l)
  176. {
  177. if(allowed==true)
  178. {
  179. if( l<=(b1.GetMaxLevel() ) )
  180. {
  181. cout<<"The passenger pushed button "<<l<<" in the elevator"<<endl;
  182. b1.e1.MoveTo(l);
  183. current_level=l;
  184. }
  185. else cout<<"Level is Out of MaxLevel!"<<endl;
  186. }
  187. else cout<<"Set the building location of passenger first!"<<endl;
  188. }
  189.  
  190.  
  191. //ELEVATOR_MODEL_DRIVER.CPP
  192. #include <iostream.h>
  193. #include <stdlib.h>
  194. #include <time.h>
  195. #include "building.h"
  196. #include "elevator.h"
  197. #include "passenger.h"
  198.  
  199. using namespace std;
  200.  
  201. main()
  202. {
  203. const int i=5;
  204. building office(i); //5-leveled building has been created
  205. passenger worker1;
  206. int start, finish, n; //the passenger will appear on level "start"
  207. //and go to level "finish"
  208. srand(time(NULL));
  209.  
  210. cin>>n; //n times passenger will appear on different levels
  211.  
  212. worker1.SetBuilding(office); //the passenger now is in a building "office"
  213.  
  214. for(int j=0; j<n; j++)
  215. {
  216. start=finish=(rand() % i) + 1;
  217. while(start==finish){ finish=(rand() % i) + 1; }
  218. cout<<endl;
  219. worker1.SetLocation(start);
  220. worker1.CallElevator();
  221. worker1.ChoseLevel(finish);
  222. }
  223. system("PAUSE");
  224. return 0;
  225. }
Last edited by Acute; Aug 30th, 2009 at 12:19 pm.
Reputation Points: 8
Solved Threads: 3
Junior Poster
Acute is offline Offline
128 posts
since Mar 2009
Aug 30th, 2009
0

Re: PLZ!!!! HEEEELP!!! OOP Questions!

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.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,862 posts
since Dec 2008
Aug 30th, 2009
0

Re: :::Need OOP advise:::

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.
Reputation Points: 72
Solved Threads: 26
Posting Whiz in Training
GDICommander is offline Offline
209 posts
since Jun 2008
Sep 1st, 2009
0

Re: PLZ!!!! HEEEELP!!! OOP Questions!

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"!)))

c++ Syntax (Toggle Plain Text)
  1. //***ELEVATOR_MODEL.DEV***
  2.  
  3. //BUILDING.H
  4. //The function-elements are in BUILDING.CPP
  5. #ifndef BUILDING_H
  6. #define BUILDING_H
  7. #include <iostream.h>
  8. #include <stdlib.h>
  9. #include "elevator.h"
  10. class building
  11. {
  12. public:
  13. building(int=2);
  14. int GetMaxLevel() const;
  15. void SetMaxLevel(int);
  16. elevator e1;
  17. private:
  18. int MaxLevel;
  19. };
  20. #endif
  21.  
  22.  
  23. //BIULDING.CPP
  24. //Function-elements of BUILDING class
  25. #include <iostream.h>
  26. #include <stdlib.h>
  27. #include "building.h"
  28.  
  29. building::building(int l)
  30. {
  31. SetMaxLevel(l);
  32. }
  33.  
  34. int building::GetMaxLevel() const
  35. {
  36. return MaxLevel;
  37. }
  38.  
  39. void building::SetMaxLevel(int level)
  40. {
  41. if(level>1) MaxLevel=level; else MaxLevel=2;
  42. }
  43.  
  44.  
  45. //ELEVATOR.H
  46. #ifndef ELEVATOR_H
  47. #define ELEVATOR_H
  48. #include <iostream.h>
  49. #include <stdlib.h>
  50. class elevator
  51. {
  52. public:
  53. elevator();
  54. void MoveTo(int);
  55. int GetPosition() const;
  56. void OpenDoor();
  57. void CloseDoor();
  58. private:
  59. int position;
  60. bool is_door_opened;
  61. };
  62. #endif
  63.  
  64.  
  65. //ELEVATOR.CPP
  66. #include <iostream.h>
  67. #include <stdlib.h>
  68. #include "elevator.h"
  69.  
  70. elevator::elevator()
  71. {
  72. position=1;
  73. is_door_opened=false;
  74. }
  75.  
  76. void elevator::MoveTo(int l)
  77. {
  78. if(l!=position)
  79. {
  80. if(is_door_opened==true) CloseDoor();
  81. cout<<"Elevator has moved to level "<<l
  82. <<" from level "<<position<<endl;
  83. position=l;
  84. OpenDoor();
  85. }
  86. else cout<<"!!Trying to set current level:no reaction"<<endl;
  87. }
  88.  
  89. void elevator::OpenDoor()
  90. {
  91. is_door_opened=true;
  92. cout<<"The Doors are opened"<<endl;
  93. }
  94.  
  95. void elevator::CloseDoor()
  96. {
  97. is_door_opened=false;
  98. cout<<"The Doors are closed"<<endl;
  99. }
  100.  
  101. int elevator::GetPosition() const
  102. {
  103. return position;
  104. }
  105.  
  106.  
  107. //PASSENGER.H
  108. #ifndef PASSENGER_H
  109. #define PASSENGER_H
  110. #include <iostream.h>
  111. #include <stdlib.h>
  112. #include "building.h"
  113. #include "elevator.h"
  114. class passenger
  115. {
  116. public:
  117. passenger();
  118. void SetBuilding(building &);
  119. void CallElevator(); //Button
  120. void ChoseLevel(int); //Button
  121. void SetLocation(int);
  122. private:
  123. building *b1;
  124. int current_level;
  125. bool allowed;
  126. };
  127. #endif
  128.  
  129.  
  130. //PASSENGER.CPP
  131. #include <iostream.h>
  132. #include <stdlib.h>
  133. #include "building.h"
  134. #include "elevator.h"
  135. #include "passenger.h"
  136.  
  137. passenger::passenger()
  138. {
  139. allowed=false;
  140. }
  141. //-------------------------------------------------------------------
  142. void passenger::SetBuilding(building &house)
  143. {
  144. b1=&house;
  145. allowed=true;
  146. current_level=1;
  147. cout<<"The passenger is in the building"<<endl;
  148. }
  149. //---------------------------------------------------------------------
  150. void passenger::SetLocation(int level)
  151. {
  152. if(allowed==true)
  153. {
  154. if( level<=( b1->GetMaxLevel() ) )
  155. {
  156. current_level=level;
  157. cout<<"The passenger is on the "<<current_level<<" level"<<endl;
  158. }
  159. else cout<<"Passenger is Out of MaxLevel!"<<endl;
  160. }
  161. else cout<<"Set the building location of passenger first!"<<endl;
  162. }
  163. //-----------------------------------------------------------------------
  164. void passenger::CallElevator()
  165. {
  166. if(allowed==true)
  167. {
  168. cout<<"The passenger has called the elevator"<<endl;
  169. if(b1->e1.GetPosition()!=current_level) b1->e1.MoveTo(current_level);
  170. else b1->e1.OpenDoor();
  171. }
  172. else cout<<"Set the building location of passenger first!"<<endl;
  173. }
  174. //-----------------------------------------------------------------------
  175. void passenger::ChoseLevel(int l)
  176. {
  177. if(allowed==true)
  178. {
  179. if( l<=(b1->GetMaxLevel() ) )
  180. {
  181. cout<<"The passenger pushed button "<<l<<" in the elevator"<<endl;
  182. b1->e1.MoveTo(l);
  183. current_level=l;
  184. }
  185. else cout<<"Level is Out of MaxLevel!"<<endl;
  186. }
  187. else cout<<"Set the building location of passenger first!"<<endl;
  188. }
  189.  
  190. //ELEVATOR_MODEL_DRIVER.CPP
  191. #include <iostream.h>
  192. #include <stdlib.h>
  193. #include <time.h>
  194. #include "building.h"
  195. #include "elevator.h"
  196. #include "passenger.h"
  197.  
  198. using namespace std;
  199.  
  200. main()
  201. {
  202. const int i=5;
  203. building office(i);
  204. int start, finish, n, j;
  205.  
  206. srand(time(NULL));
  207.  
  208. cin>>n;
  209.  
  210. for(j=0; j<n; j++)
  211. {
  212. start=finish=(rand() % i) + 1;
  213. while(start==finish){ finish=(rand() % i) + 1; }
  214. cout<<endl;
  215. passenger *ptr = new passenger;
  216. ptr->SetBuilding(office);
  217. ptr->SetLocation(start);
  218. ptr->CallElevator();
  219. ptr->ChoseLevel(finish);
  220. delete ptr;
  221. }
  222. system("PAUSE");
  223. return 0;
  224. }
Reputation Points: 8
Solved Threads: 3
Junior Poster
Acute is offline Offline
128 posts
since Mar 2009
Sep 1st, 2009
0

Re: PLZ!!!! HEEEELP!!! OOP Questions!

I've just read through your code a little(not detailed) and saw this:
cpp Syntax (Toggle Plain Text)
  1. building::building(int l)
  2. {
  3. SetMaxLevel(l);
  4. }

But because this is the constructor of building there is a faster way to initialize variables, you can do it like this:
cpp Syntax (Toggle Plain Text)
  1. building::building( int l ) :
  2. 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.
Reputation Points: 193
Solved Threads: 75
Posting Pro in Training
thelamb is offline Offline
426 posts
since Aug 2008
Sep 2nd, 2009
0

Re: PLZ!!!! HEEEELP!!! OOP Questions!

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!
Reputation Points: 8
Solved Threads: 3
Junior Poster
Acute is offline Offline
128 posts
since Mar 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: What can I do better? C++ Homework
Next Thread in C++ Forum Timeline: Global variables... OOP trap?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC