
OOP lec5 relations bettwen classes.ppt
- Количество слайдов: 15
Ковалюк Т. В. Об’єктно-орієнтоване програмування НТУУ „КПІ„
Діаграма класів з атрибутами Ковалюк Т. В. Об’єктно-орієнтоване програмування НТУУ „КПІ„
Діаграма класів з атрибутами Scheduler Building int floor 1 Arrival. Time int floor 2 Arrival. Time none Операції Floor int capacity=1 bool occuped=false Операції Зв’язок “Використовувати” Clock int time=0 Операції Зв’язок “Композиція (включення)” Elevator int current. Floor=1 int capacity=1 int arrival. Time bool mooving=false enum direction=up Операції Ковалюк Т. В. Об’єктно-орієнтоване програмування НТУУ „КПІ„
Реалізація зв’язків між классами Включення об’єкти одного класу є елементами даних іншого класу #include "elevator. h" #include "floor. h" #include "clock. h" #include "scheduler. h" class Building { public: Building(); // constructor ~Building(); // destructor void run. Simulation( int ); // run simulation for specified time private: Floor floor 1; // floor 1 object Floor floor 2; // floor 2 object Elevator elevator; // elevator object Clock clock; // clock object Scheduler scheduler; // scheduler object }; Ковалюк Т. В. Об’єктно-орієнтоване програмування НТУУ „КПІ„
Реалізація зв’язків між классами Використовування Floor Elevator &elevator. Ref; Посилання на об’єкти одного класу є елементами даних іншого класу Операції Elevator Floor &floor 1 Ref; Floor &floor 2 Ref; Операції Ковалюк Т. В. Об’єктно-орієнтоване програмування НТУУ „КПІ„
class Elevator; // forward declaration class Person; // forward declaration class Floor { public: static const int FLOOR 1; //common attributes for all objects of class "floor" static const int FLOOR 2; Floor. Button floor. Button; // floor. Button object private: const int floor. Number; // the floor's number, don't change the attribute Elevator &elevator. Ref; // pointer to elevator Person *occupant. Ptr; // pointer to person on floor Light light; // light object Ковалюк Т. В. Об’єктно-орієнтоване програмування НТУУ „КПІ„
public: Floor( int, Elevator & ); // constructor ~Floor(); // destructor ///////////////////////////// bool is. Occupied(); // return true if floor occupied int get. Number() ; // return floor's number // pass a handle to new person coming on floor void person. Arrives( Person * ); // notify floor that elevator has arrived Person *elevator. Arrived(); // notify floor that elevator is leaving void elevator. Leaving(); // notify floor that person is leaving floor void person. Boarding. Elevator(); }; Ковалюк Т. В. Об’єктно-орієнтоване програмування НТУУ „КПІ„
class Floor; // forward declaration class Person; // forward declaration class Elevator { public: Elevator( Floor &, Floor & ); // constructor ~Elevator(); // destructor void summon. Elevator( int ); // request to service a floor void prepare. To. Leave( bool ); // prepare to leave void process. Time( int ); // give time to elevator void passenger. Enters( Person * const ); // board a passenger void passenger. Exits(); // exit a passenger Elevator. Button elevator. Button; // note public object private: void process. Possible. Arrival(); void process. Possible. Departure(); void arrive. At. Floor( Floor & ); void move(); Ковалюк Т. В. Об’єктно-орієнтоване програмування НТУУ „КПІ„
// time to move between floors int current. Building. Clock. Time; // current time bool moving; // elevator state int direction; // current direction int current. Floor; // current location int arrival. Time; // time to arrive at a floor bool floor 1 Needs. Service; // floor 1 service flag bool floor 2 Needs. Service; // floor 2 service flag Floor &floor 1 Ref; Floor &floor 2 Ref; Person *passenger. Ptr; // reference to floor 1 // reference to floor 2 // pointer to current passenger Door door; Bell bell; // door object // bell object }; Ковалюк Т. В. Об’єктно-орієнтоване програмування НТУУ „КПІ„
Реалізація зв’язків між классами Використовування Покажчики на об’єкти одного класу є елементами даних іншого класу Person static int person. Count; const int ID; const int destination. Floor; Операції Elevator Person *passenger. Ptr; Операції Ковалюк Т. В. Об’єктно-орієнтоване програмування НТУУ „КПІ„
class Floor; // forward declaration class Elevator; // forward declaration class Person { public: Person(int ); // constructor ~Person(); // destructor void step. Onto. Floor( Floor & ); void enter. Elevator( Elevator &, Floor & ); ///////////////////////////////// int get. ID(); // returns person's ID void exit. Elevator(Floor &, Elevator & ); //////////////////////////////// private: static int person. Count; // total number of persons const int ID; // person's unique ID # const int destination. Floor; // destination floor # }; Ковалюк Т. В. Об’єктно-орієнтоване програмування НТУУ „КПІ„
Реалізація зв’язків між классами Доступ до методів і атрибутів об’єктів – через параметри методів Floor &floor Elevator &elevator, class Person: : enter. Elevator() class Floor class Elevator Floor: : person. Boarding. Elevator() Elevator: : passenger. Enters() Ковалюк Т. В. Об’єктно-орієнтоване програмування НТУУ „КПІ„
Building: : run. Simulation() Clock: : tick() Clock: : get. Time() Scheduler: : process. Time( ) Scheduler: : handle. Arrivals( ) Floor: : get. Number() Floor: : is. Occupied() Scheduler: : delay. Time() Scheduler: : create. New. Person() Floor: : get. Number() Person: : Person() Person: : get. ID() Scheduler: : schedule. Time() Person: : step. Onto. Floor( Ковалюк Т. В. Об’єктно-орієнтоване програмування НТУУ „КПІ„
Ковалюк Т. В. Об’єктно-орієнтоване програмування НТУУ „КПІ„
Ковалюк Т. В. Об’єктно-орієнтоване програмування НТУУ „КПІ„
OOP lec5 relations bettwen classes.ppt