Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Метод указ к лабораторным работам ООП 3 семест...doc
Скачиваний:
5
Добавлен:
13.11.2019
Размер:
22.34 Mб
Скачать

Реалізація конструкторів і деструкторів деяких класів

// 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 Результати виконання роботи