- •Рейтингова система оцінювання знань та умінь з ооп 2012-2013 н.Р.
- •Вимоги до звіту з лабораторних робіт
- •Класи потоків Лабораторна робота 1
- •1.1 Теоретичні відомості
- •1.1.1. Методи класів потоків
- •1.1.2. Маніпулятори
- •1.1.3. Деякі методи класів файлових потоків
- •1.2 Приклад програми Умова задачі
- •Алгоритм задачі
- •Текст програми
- •Результати виконання програми
- •1.3 Варіанти завдань
- •1.4 Контрольні запитання
- •Інкапсуляція. Інтерфейси класів . Діаграми прецедентів і класів Лабораторна робота 2
- •2.1 Теоретичні відомості Етапи об’єктно-орієнтованого проектування та моделювання
- •Діаграма варіантів використання(use-case diagram)
- •Ідентифікація класів
- •Ідентифікація атрибутів
- •Ідентифікація операцій (методів)
- •Типові схеми uml діаграм
- •2.2 Програма роботи
- •2.3 Варіанти завдань
- •2.4 Приклад виконання роботи Задача
- •Об’єктно-орієнтоване проектування Зображення класів в uml
- •Ідентифікація атрибутів
- •Структура проекту
- •Інтерфейси та реалізації класів програми
- •2.5 Контрольні запитання
- •Конструктори та деструктори класів Діаграми прецедентів і класів Лабораторна робота 3
- •3.1 Теоретичні відомості
- •3.2 Програма роботи
- •3.3 Варіанти завдань
- •3.4 Приклад виконання роботи Задача
- •Реалізація конструкторів і деструкторів деяких класів
- •3.5 Контрольні запитання
- •Методи класів: аксесори, ітератори, утілити, предикатні функції . Діаграми класів, діяльності, послідовності Лабораторна робота № 4
- •4.1 Теоретичні відомості
- •4.2 Програма роботи
- •4.3 Варіанти завдань
- •4.4 Приклад виконання роботи Задача
- •Реалізація деяких методів-аксесорів, предикатних функцій, функцій-утілит, методів, які забезпечують функціональність об’єктів
- •Результати виконання роботи
- •4.5 Контрольні запитання
- •Константні та статичні члени класів. Вбудовані функції. Діаграми класів, послідовності Лабораторна робота № 5
- •5.1 Теоретичні відомості
- •5.2 Програма роботи
- •5.3 Варіанти завдань
- •5.4 Приклад виконання роботи Задача 5_1
- •Приклади деяких константних елементів даних і методів класів
- •Фрагмент результатів виконання роботи
- •Задача 5_2
- •Приклади деяких статичних елементів даних і методів класів
- •Фрагмент результатів виконання роботи
- •5.5 Контрольні запитання
- •Перевантаження операцій та функцій. Дружні функції Діаграми класів, станів і переходів Лабораторна робота 6
- •6.1 Теоретичні відомості
- •6.2 Програма роботи
- •6.3 Варіанти завдань
- •6.4 Приклад виконання роботи
- •6.5 Контрольні запитання
- •Просте успадкування класів. Поліморфізм. Віртуальні функції Діаграма класів, кооперацій. Лабораторна робота 7
- •7.1 Теоретичні відомості
- •7.2 Програма роботи
- •9.2. Приклад програми Умова задачі 9
- •Алгоритм задачі 9
- •Текст програми 9
- •Результати роботи програми 9
- •9.3. Варіанти завдань
- •9.4. Контрольні запитання
- •Методи внутрішнього сортування (сортування масивів) Лабораторна робота 10
- •Методи зовнішнього сортування (сортування файлів) Лабораторна робота 11
Реалізація конструкторів і деструкторів деяких класів
// building.cpp // implementation of the class Building #include <iostream> using namespace std; #include "building.h" // constructor - initialization Building::Building() : floor1(1, elevator ) , floor2(2, elevator ) , elevator(floor1, floor2 ) , scheduler(floor1, floor2 ) { cout << "building created" << endl; }
//copy constructor Building::Building(Building& object) : floor1(1 , object.elevator ) , floor2(2 , object.elevator ) , elevator(object.floor1, object.floor2) , scheduler(object.floor1,object.floor2) {cout<<"second building created"<<endl;} // destructor Building::~Building() { cout << "building destroyed" << endl; } // реалізації решти методів |
// floor.cpp // implementation of the class Floor #include <iostream> using namespace std; #include "floor.h" #include "person.h" #include "elevator.h" // constructor initialization Floor::Floor(int number, Elevator &elevatorHandle ) : floorButton( number, elevatorHandle) , floorNumber( number ) , elevatorRef( elevatorHandle ) , occupantPtr ( 0 ) , light(floorNumber==1?"floor1":"floor2") { cout << "floor " << floorNumber << " created" << endl; } // destructor Floor::~Floor() { cout << "floor " << floorNumber << " destroyed" << endl; } |
// elevator.cpp // implementation of the class Elevator. #include <iostream> using namespace std; #include "elevator.h" #include "person.h" #include "floor.h"
// constructor initialization Elevator::Elevator( Floor &firstFloor, Floor &secondFloor ) : elevatorButton( *this ) , moving( false ) , direction( 0 ) , currentFloor( 1 ) , arrivalTime( 0 ) , floor1Ref( firstFloor ) , floor2Ref( secondFloor ) , passengerPtr( 0 ) { cout << "elevator created" << endl; }
|
//продолжение elevator.cpp /copy constructor Elevator::Elevator(Elevator& object) : elevatorButton( object.elevatorButton) , moving( object.moving) , direction( object.direction) , currentFloor( object.currentFloor) , arrivalTime( object.arrivalTime) , floor1Ref( object.floor1Ref) , floor2Ref( object.floor2Ref) , passengerPtr( object.passengerPtr) {cout <<"copy elevator created" << endl;}
// destructor Elevator::~Elevator() { cout << "elevator destroyed" << endl; } // реалізації решти методів |
// elevatorButton.cpp: // implementation of the class //ElevatorButton #include <iostream> using namespace std; #include “elevatorButton.h” #include “elevator.h” // constructor ElevatorButton::ElevatorButton( Elevator &elevatorHandle ) :pressed(false) { cout << “elevator button created” << |
//продолжение elevatorButton.cpp: endl; } // destructor ElevatorButton::~ElevatorButton() { cout << “elevator button destroyed” << endl; } |
// person.cpp // implementation of the class Person. #include <iostream> using namespace std; #include "person.h" #include "floor.h" #include "elevator.h" //default constructor Person::Person() { ID=destinationFloor=personCount=0; cout<<"default initialization of man take place - void object created"<<endl;}
// constructor-initialization Person::Person( int destFloor ) : ID( ++personCount ) , destinationFloor( destFloor ) , personCount(0) {cout<<"person "<<ID<<" created on floor "<<destinationFloor<<endl;}
//copy constructor Person::Person(Person& man ) {//total number of persons personCount=man.personCount; ID=man.ID; // person's unique ID # destinationFloor=man.destinationFloor; cout<<"person "<<ID<<" created on floor "<<destinationFloor<<" again";}
|
//продолжение person.cpp //condtrictor tramsformation Person::Person( string tabnumber ) //use class string and methods: { const char* str1=tabnumber.c_str(); char s1[2]; strncpy(s1,str1,2); ID=atoi(s1); //первые два символа-> ID string str2 = tabnumber.substr(2,1); const char* s2=str2.c_str(); personCount=atoi(s2);//next two - //personCount string str3 = tabnumber.substr(3,2); const char* s3=str3.c_str(); destinationFloor=atoi(s3); //next two //symbols ->destinationFloor cout<<"constructor transformation"<<endl; cout<<"person "<<ID<<" created on floor" <<destinationFloor<<" count=" <<personCount<<endl;} // destructor Person::~Person() {cout << "person "<<ID<<" exits simulation on floor "<< destinationFloor << " (person destructor invoked)" << endl; } |
// Driver for the simulation. #include <iostream> #include <string> using namespace std; #include "building.h" #include "person.h"
void main() { int duration; //time of simulation cout << "Enter run time: "; cin >> duration; cin.ignore(); // ignore return char Building office1; // create the building Person man; //default constructor
string code; cout<<"enter string code for man"<<endl; cin>>code; Person one(code); //constructor- //transformation: string to 3 int value |
//продолжение функции main() cout << endl << "*** ELEVATOR SIMULATION BEGINS ***" << endl << endl; Person manager(1); Building office2=office1; //copy con //structor Elevator elevator2=office2.GetElevator(); //copy constructor Person clerk=manager; //copy constructor cout<<" in new office"<<endl; office1.RunSimulation( duration ); // start simulation cout << "*** ELEVATOR SIMULATION ENDS***" << endl; system("pause"); } |
Рис. 3.1 Результати виконання роботи