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

Фрагмент результатів виконання роботи

Задача 5_2

В програму внесені зміни щодо оголошення статичних методів і статичних даних в інтерфейсі класу та їх реалізацій:

  1. Статичні елементи даних використовують для створення одної копії змінної для усіх об’єктів класу. Наприклад, кількість об’єктів класу Person зберігають у статичній змінній Person::personCount.

  2. Статичні функції застосовують для доступу до статичних членів класу.

  3. Статичні функції можна використовувати для запобігання доступу до нестатичних членів класу.

Приклади деяких статичних елементів даних і методів класів

// person.h

// definition of class Person

#ifndef PERSON_H

#define PERSON_H

#include<string>

using namespace std;

class Floor; // forward declaration

class Elevator; //forward declaration

class Person {

public:

Person();

Person( const int );

~Person();

Person( Person& ); //copy constructor

Person( string tabnumber);

int GetID() const;

void StepOntoFloor( Floor & );

void EnterElevator(Elevator&, Floor&);

void exitElevator(const Floor&, Elevator& ) const;

//lab6_2 return total number of persons

static int GetPersonCount()

{return personCount;};

private:

//lab6_2 total number of persons

static int personCount;

const int ID;

const int destinationFloor;

};

#endif // PERSON_H

// person.cpp

#include <iostream>

#include<string>

using namespace std;

#include "person.h"

#include "floor.h"

#include "elevator.h"

//lab6_2 ініціалізація стат змінної

int Person::personCount = 0;

Person::Person():ID(0),destinationFloor(0)

{ cout<<"default initialization of man take place - void object created"<<endl;}

// constructor-initialization

Person::Person(const int destFloor )

: ID( ++personCount )

, destinationFloor( destFloor )

{cout<<"person "<<ID<<" created on floor "<<destinationFloor<<endl;}

//lab6_2:copy constructor

Person::Person(Person& man):ID(man.ID),

destinationFloor(man.destinationFloor)

{ ++personCount;

cout<<"person (man) "<<ID<<" created on floor "<<destinationFloor<<" again";}

//lab6_2:constructor transformation

Person::Person( string tabnumber )

:ID(tabnumber[0])

,destinationFloor(tabnumber[1])

{ ++personCount;

string str2 = tabnumber.substr(2,1);

const char* s2=str2.c_str();

cout<<"constructor transformation"<<endl;

cout<<"person "<<ID<<" created on floor "<<destinationFloor<<" count="<< personCount<<endl; }

//. . . . . . . . . . .

// elevatorSimulation.cpp

// Driver for the simulation.

#include <iostream>

using namespace std;

#include <string>

#include "building.h"

#include "person.h"

void main(){

int duration;

cout << "Enter run time: ";

cin >> duration;

Building office1;

Person man; //default constructor

string code;

cout<<"enter string "<<endl;

cin>>code;

Person one(code);

cout << endl << "*** ELEVATOR SIMULATION BEGINS ***" << endl << endl;

Person manager(1);

Building office2=office1;

Elevator elevator2=office2.GetElevator();

Person clerk=manager;

cout<<" in new office"<<endl;

office1.RunSimulation( duration );

//виклик статичної функції

cout<<"number of person are " <<

Person::GetPersonCount()<<endl;

cout<<"*** ELEVATOR SIMULATION ENDS ***"<<endl;

system("pause");

}