| | |
PLZ!!!! HEEEELP!!! OOP Questions!
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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)????
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.
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.
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?•
•
Join Date: Aug 2008
Posts: 39
Reputation:
Solved Threads: 7
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.
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.
Hi! I've finished my elevator and it works good!)
If you have some ideas to improve, plz, let me know!
If you have some ideas to improve, plz, let me know!
c++ Syntax (Toggle Plain Text)
//***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; }
Last edited by Acute; Aug 30th, 2009 at 12:19 pm.
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.
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? 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.
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"!
)))
Great thnx to "TheLamb"!
))) c++ Syntax (Toggle Plain Text)
//***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; }
•
•
Join Date: Aug 2008
Posts: 39
Reputation:
Solved Threads: 7
I've just read through your code a little(not detailed) and saw this:
But because this is the constructor of building there is a faster way to initialize variables, you can do it like this:
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.
cpp Syntax (Toggle Plain Text)
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:
cpp Syntax (Toggle Plain Text)
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.
![]() |
Similar Threads
- please very importent ( double linked list ) (C++)
- Regarding .net Framework (VB.NET)
- Got Computer Software Problems? PLZ anwser mine and other questions (IT Professionals' Lounge)
- database questions I have (Database Design)
- Time for a Clean Install of XP? (Windows tips 'n' tweaks)
- Plz need some practice questions (Computer Science)
- new Arctic Freezer 7; plz help (Motherboards, CPUs and RAM)
Other Threads in the C++ Forum
- Previous Thread: What can I do better? C++ Homework
- Next Thread: Global variables... OOP trap?
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






