Download presentation
Presentation is loading. Please wait.
Published byElfrieda Pierce Modified over 8 years ago
1
2000 Deitel & Associates, Inc. All rights reserved. Optional Case Study - Chapter 6 Outline 6.1 Introduction 6.2 Implementation: Visibility 6.3 Implementation: Handles 6.4 Implementation: Class Header Files Bell Clock Door Light Building FloorButton ElevatorButton FloorButton Scheduler Floor Elevator 6.5 Conclusion
2
2000 Deitel & Associates, Inc. All rights reserved. 6.1 Introduction Chapters 1 to 5 –Fundamentals of object orientation –Developed an object-oriented design Body of Chapter 6 –Details of programming with C++ classes This section –Begin implementing our design in C++ –Use UML class diagram to outline C++ header files Define classes
3
2000 Deitel & Associates, Inc. All rights reserved. 6.2 Implementation: Visibility public or private ? –Data members generally private visibility –Member functions and operations? Invoked by clients Therefore, must be public visibility In UML ( + ) indicates public ( - ) indicates private –As we write header files, place + items into public, - items into private
4
2000 Deitel & Associates, Inc. All rights reserved. 6.2 Implementation: Visibility (II) Building + runSimulation( ) : void - on : bool = false Light + turnOff( ) : void + turnOn( ) : void + ringBell( ) : void Bell - pressed : bool = false FloorButton + pressButton( ) : void + resetButton( ) : void - pressed : bool = false ElevatorButton + pressButton( ) : void + resetButton( ) : void Person - ID : int + stepOntoFloor( ) : void + exitElevator( ) : void + enterElevator( ) : void - time : int = 0 Clock + getTime( ) : int + tick( ) : void - occupied : bool = false Floor + elevatorArrived( ) : void + isOccupied( ) : bool + personArrives( ) :void - open : bool = false Door + openDoor( ) : void + closeDoor( ) : void Scheduler - floor1ArrivalTime : int - floor2ArrivalTime : int + processTime( time : int ) : void Elevator - currentFloor : int = 1 - direction : enum = up - capacity : int = 1 - arrivalTime : int - moving : bool = false + summonElevator( ) : void + prepareToLeave( ) : void + processTime( time : int ) : void + personEnters( ) : void + personExits( ) : void
5
2000 Deitel & Associates, Inc. All rights reserved. 6.3 Implementation: Handles Handles –For an object of class A to communicate with an object of class B, it must have a handle to the object of class B Know name, hold a reference, or have a pointer –Body of Chapter 6 discussed how to implement handles as references and pointers References become attributes of the class –Until we discuss composition in chapter 7, we cannot implement all the classes we have Next Slide –List of classes and the classes they must have handles to
6
2000 Deitel & Associates, Inc. All rights reserved. 6.3 Implementation: Handles (II)
7
2000 Deitel & Associates, Inc. All rights reserved. 6.4 Implementation: Class Header Files Examine header files for each class –To demonstrate how constructors and destructors run, we will code them to display messages (implementation in Chapter 7)
8
2000 Deitel & Associates, Inc. All rights reserved. Outline Bell Header public: Constructor, destructor, and function ringBell 1// bell.h 2// Definition for class Bell. 3#ifndef BELL_H 4#define BELL_H 5 6class Bell { 7public: 8 Bell(); // constructor 9 ~Bell(); // destructor 10 void ringBell(); // ring the bell 11}; 12 13#endif // BELL_H
9
2000 Deitel & Associates, Inc. All rights reserved. Outline Clock Header public: Constructor, destructor, tick, and getTime private: time attribute In simulation, Building increments time with tick once per second, and gets current value with getTime 1// clock.h 2// Definition for class Clock. 3#ifndef CLOCK_H 4#define CLOCK_H 5 6class Clock { 7public: 8 Clock(); // constructor 9 ~Clock(); // destructor 10 void tick(); // increment clock by one second 11 int getTime(); // returns clock's current time 12private: 13 int time; // clock's time 14}; 15 16#endif // CLOCK_H
10
2000 Deitel & Associates, Inc. All rights reserved. Outline Person Header Person objects created dynamically, so must be implemented differently (more Chapter 7) public: Constructor, destructor, operations private: ID number 1// person.h 2// definition of class Person 3#ifndef PERSON_H 4#define PERSON_H 5 6class Person { 7public: 8 Person( int ); // constructor 9 ~Person(); // destructor 10 int getID(); // returns person's ID 11 12 void stepOntoFloor(); 13 void enterElevator(); 14 void exitElevator() 15private: 16 int ID; // person's unique ID # 17}; 18 19#endif // PERSON_H
11
2000 Deitel & Associates, Inc. All rights reserved. Outline Door Header public: Constructor, destructor, member functions private: open status Door needs a handle to a Person object, which is dynamically created (more Chapter 7) 1// door.h 2// Definition for class Door. 3#ifndef DOOR_H 4#define DOOR_H 5 6class Door { 7public: 8 Door(); // constructor 9 ~Door(); // destructor 10 11 void openDoor(); 12 void closeDoor(); 13private: 14 bool open; // open or closed 15}; 16 17#endif // DOOR_H
12
2000 Deitel & Associates, Inc. All rights reserved. Outline Light Header public: Constructor, destructor, member functions private: data member on We need to distinguish between the two lights (one on each floor) Name each light (declare char *name ) Add char* parameter to constructor so it can initialize name 1// light.h 2// Definition for class Light. 3#ifndef LIGHT_H 4#define LIGHT_H 5 6class Light { 7public: 8 Light( char * ); // constructor 9 ~Light(); // destructor 10 void turnOn(); // turns light on 11 void turnOff(); // turns light off 12private: 13 bool on; // on or off 14 char *name; 15}; 16 17#endif // LIGHT_H
13
2000 Deitel & Associates, Inc. All rights reserved. Outline Building Header public: Constructor, destructor, member function A building object will run the simulation. int parameter: number of seconds to run simulation private: Need handles to composite objects (delay implementation until Chapter 7) 1// building.h 2// Definition for class Building. 3#ifndef BUILDING_H 4#define BUILDING_H 5 6class Building { 7public: 8 Building(); // constructor 9 ~Building(); // destructor 10 11 // runs simulation for specified amount of time 12 void runSimulation( int ); 13private: 14 // In Chapter 7, we show how to include: 15 // one object of class Clock 16 // one object of class Scheduler 17 // one object of class Elevator 18 // two objects of class Floor 19}; 20 21#endif // BUILDING_H
14
2000 Deitel & Associates, Inc. All rights reserved. Outline ElevatorButton Header public: Constructor, destructor, member functions private: pressed attribute We include a handle to an Elevator object (using a reference) 1// elevatorButton.h 2// Definition for class ElevatorButton. 3#ifndef ELEVATORBUTTON_H 4#define ELEVATORBUTTON_H 5 6class Elevator; // forward declaration 7 8class ElevatorButton { 9public: 10 ElevatorButton( Elevator & ); // constructor 11 ~ElevatorButton(); // destructor 12 13 void pressButton(); // press the button 14 void resetButton(); // reset the button 15private: 16 bool pressed; // state of button 17 18 // reference to button's elevator 19 Elevator &elevatorRef; 20}; 21 22#endif // ELEVATORBUTTON_H References must be initialized when declared, but we cannot assign data members in the header file. Reference therefore initialized in constructor; pass Elevator reference to constructor (line 10). Line 6 is a forward declaration. It allows us to reference an object without including the header.
15
2000 Deitel & Associates, Inc. All rights reserved. Outline FloorButton Header Header identical to ElevatorButton except we declare variable floorNumber (type int ) Objects of type floorButton need to know to which floor they belong (passed as argument to constructor) 1// floorButton.h 2// Definition for class FloorButton. 3#ifndef FLOORBUTTON_H 4#define FLOORBUTTON_H 5 6class Elevator; // forward declaration 7 8class FloorButton { 9public: 10 FloorButton( int, Elevator & ); // constructor 11 ~FloorButton(); // destructor 12 13 void pressButton(); // press the button 14 void resetButton(); // reset the button 15private: 16 int floorNumber; // number of the button's floor 17 bool pressed; // state of button 18 19 // reference to button's elevator 20 Elevator &elevatorRef; 21}; 22 23#endif // FLOORBUTTON_H
16
2000 Deitel & Associates, Inc. All rights reserved. Outline Scheduler Header public: Constructor, destructor, member function private : scheduleTime and delayTime (not operations) References to floors 1 and 2 (scheduler needs these for isOccupied call) Arrival times for floors 1 and 2 1// scheduler.h 2// definition for class Scheduler 3#ifndef SCHEDULER_H 4#define SCHEDULER_H 5 6class Floor; // forward declaration 7 8class Scheduler { 9public: 10 Scheduler( Floor &, Floor & ); // constructor 11 ~Scheduler(); // destructor 12 void processTime( int ); // set scheduler's time 13private: 14 15 // method that schedules arrivals to a specified floor 16 void scheduleTime( Floor & ); 17 18 // method that delays arrivals to a specified floor 19 void delayTime( Floor & ); 20 21 Floor &floor1Ref; 22 Floor &floor2Ref; 23 int floor1ArrivalTime; 24 int floor2ArrivalTime; 25}; 26 27#endif // SCHEDULER_H
17
2000 Deitel & Associates, Inc. All rights reserved. Outline Floor Header public: Constructor, destructor, member functions Example: Elevator invokes elevatorLeaving, and Floor object turns off its light 1// floor.h 2// Definition for class Floor. 3#ifndef FLOOR_H 4#define FLOOR_H 5 6class Elevator; // forward declaration 7 8class Floor { 9public: 10 Floor( int, Elevator & ); // constructor 11 ~Floor(); // destructor 12 13 // return true if floor is occupied 14 bool isOccupied(); 15 16 // return floor's number 17 int getNumber(); 18 19 // pass a handle to new person coming on floor 20 void personArrives(); 21 22 // notify floor that elevator has arrived 23 void elevatorArrived(); 24 25 // notify floor that elevator is leaving 26 void elevatorLeaving(); 27 28 // declaration of FloorButton component (see Chapter 7) 29
18
2000 Deitel & Associates, Inc. All rights reserved. Outline Floor Header private: floorNumber (for output purposes, so floor knows where it is) Constructor takes an int, to initialize floorNumber Elevator reference Another component (more Chapter 7) 30private: 31 int floorNumber; // the floor's number 32 Elevator &elevatorRef; // pointer to elevator 33 // declaration of Light component (see Chapter 7) 34}; 35 36#endif // FLOOR_H
19
2000 Deitel & Associates, Inc. All rights reserved. Outline Elevator Header public : Constructor, destructor, member functions (operations) Rename last two functions to differentiate between people waiting and passengers 1// elevator.h 2// Definition for class Elevator. 3#ifndef ELEVATOR_H 4#define ELEVATOR_H 5 6class Floor; // forward declaration 7 8class Elevator { 9public: 10 Elevator( Floor &, Floor & ); // constructor 11 ~Elevator(); // destructor 12 13 // request that elevator service a particular floor 14 void summonElevator( int ); 15 16 // prepare elevator to leave 17 void prepareToLeave(); 18 19 // give time to elevator 20 void processTime( int ); 21 22 // notify elevator that passenger is boarding 23 void passengerEnters(); 24 25 // notify elevator that passenger is exiting 26 void passengerExits(); 27
20
2000 Deitel & Associates, Inc. All rights reserved. Outline Elevator Header private: Declare attributes Declare Floor references (initialized by the constructor) We do not declare capacity attribute - in implementation, we write code so only one person in elevator at a time 28 // declaration of ElevatorButton component (see Chapter 7) 29private: 30 bool moving; // elevator state 31 int direction; // current direction 32 int currentFloor; // current location 33 34 // time for arrival at a floor 35 int arrivalTime; 36 37 // References to floors serviced by elevator 38 Floor &floor1Ref; 39 Floor &floor2Ref; 40 41 // declaration of Door component (see Chapter 7) 42 // declaration of Bell component (see Chapter 7) 43}; 44 45#endif // ELEVATOR_H
21
2000 Deitel & Associates, Inc. All rights reserved. 6.5 Conclusion Next chapter –Full code for simulation New ideas –Composite relationships –Dynamic creation of objects –static and const data members and functions Chapter 9 –Use inheritance to further improve our design
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.