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

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2009
Posts: 23
Reputation: Acute is an unknown quantity at this point 
Solved Threads: 1
Acute's Avatar
Acute Acute is offline Offline
Newbie Poster

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

 
-1
  #1
Aug 22nd, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,359
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 171
firstPerson's Avatar
firstPerson firstPerson is online now Online
Nearly a Posting Virtuoso

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

 
0
  #2
Aug 22nd, 2009
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.
1) What word becomes shorter if you add a letter to it? 
      [ Solved by : niek_e, Paul Thompson, SgtMe, murtan, xavier666, jonsca]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
      [*solved by : murtan, xavier666]
3) What is the 123456789th prime numer?
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 39
Reputation: thelamb is on a distinguished road 
Solved Threads: 7
thelamb thelamb is offline Offline
Light Poster

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

 
0
  #3
Aug 23rd, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,701
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 483
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

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

 
0
  #4
Aug 23rd, 2009
An acute thread title. Does the thread title need a fixing.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 23
Reputation: Acute is an unknown quantity at this point 
Solved Threads: 1
Acute's Avatar
Acute Acute is offline Offline
Newbie Poster

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

 
0
  #5
Aug 30th, 2009
Hi! I've finished my elevator and it works good!)
If you have some ideas to improve, plz, let me know!

  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,359
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 171
firstPerson's Avatar
firstPerson firstPerson is online now Online
Nearly a Posting Virtuoso

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

 
0
  #6
Aug 30th, 2009
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.
1) What word becomes shorter if you add a letter to it? 
      [ Solved by : niek_e, Paul Thompson, SgtMe, murtan, xavier666, jonsca]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
      [*solved by : murtan, xavier666]
3) What is the 123456789th prime numer?
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 147
Reputation: GDICommander is an unknown quantity at this point 
Solved Threads: 19
GDICommander's Avatar
GDICommander GDICommander is offline Offline
Junior Poster

Re: :::Need OOP advise:::

 
0
  #7
Aug 30th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 23
Reputation: Acute is an unknown quantity at this point 
Solved Threads: 1
Acute's Avatar
Acute Acute is offline Offline
Newbie Poster

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

 
0
  #8
Sep 1st, 2009
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"!)))

  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. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 39
Reputation: thelamb is on a distinguished road 
Solved Threads: 7
thelamb thelamb is offline Offline
Light Poster

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

 
0
  #9
Sep 1st, 2009
I've just read through your code a little(not detailed) and saw this:
  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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 23
Reputation: Acute is an unknown quantity at this point 
Solved Threads: 1
Acute's Avatar
Acute Acute is offline Offline
Newbie Poster

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

 
0
  #10
Sep 2nd, 2009
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!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC