Скачиваний:
1
Добавлен:
15.08.2023
Размер:
38.39 Кб
Скачать

ФЕДЕРАЛЬНОЕ ГОСУДАРСТВЕННОЕ БЮДЖЕТНОЕ ОБРАЗОВАТЕЛЬНОЕ УЧРЕЖДЕНИЕ ВЫСШЕГО ОБРАЗОВАНИЯ "САНКТ-ПЕТЕРБУРГСКИЙ ГОСУДАРСТВЕННЫЙ УНИВЕРСИТЕТ ТЕЛЕКОММУНИКАЦИЙ ИМ. ПРОФ. М. А. БОНЧ-БРУЕВИЧА"

Факультет инфокоммуникационных сетей и систем

Кафедра сетей связи и передачи данных

ЛАБОРАТОРНАЯ РАБОТА №2

«ОТНОШЕНИЕ ВКЛЮЧЕНИЯ»

по дисциплине «Объектно-ориентированное программирование»

Выполнили:

студент 2-го курса

дневного отделения

группы ИКПИ-92

Козлов Никита

Санкт-Петербург

2020

Постановка задачи

Разработать определения двух класса “Класс №1” и “Класс №2”, которые связаны отношением включения. Поля разрабатываемых классов считаются заданными и приводятся в таблице. Для всех классов требуется написать три вида конструкторов (умолчания, с параметрами и конструктор копирования), деструктор, перегруженный оператор присваивания, методы доступа и метод print(), распечатывающий значения полей объекта. Написать тестовую программу для проверки работоспособности разработанных классов

Задача

Вариант

Класс №1

Свойства

Класс №2

Свойства

13

CDescription

int number

string comand

CFootbalPlayer

string fullname

Код программы

Файл CPlayer.h

#pragma once

#include <iostream>

using namespace std;

class CPlayerDescription

{

private:

int num;

string comand;

public:

CPlayerDescription() { num = 0; comand = "DEFAULT_COMAND"; };

CPlayerDescription(int number, string comand_name) { num = number; comand = comand_name; };

~CPlayerDescription() { num = 0; comand = ""; };

void setNumber(int number);

void setComand(string comand_name);

/// <summary>

/// Gets number from private field,

/// use for function in:

/// <seealso cref="FootbalPlayer::getPlayerInfo"/>

/// </summary>

int getNumber();

/// <summary>

/// Gets comand name from private field,

/// use for function in:

/// <seealso cref="FootbalPlayer::getPlayerInfo"/>

/// </summary>

string getComand();

bool operator == (const CPlayerDescription& source);

};

class FootbalPlayer

{

private:

string fullname;

CPlayerDescription description;

public:

FootbalPlayer() { setPlayerName("DEFAULT"); };

FootbalPlayer(string name, int number, string comand) {

setPlayerName(name);

description.setNumber(number);

description.setComand(comand);

};

~FootbalPlayer() {};

/// <summary>

/// Sets name to footbal player

/// </summary>

void setPlayerName (string name);

/// <summary>

/// Sets number to footbal player

/// </summary>

void setPlayerNumber(int number);

/// <summary>

/// Sets comand name to footbal player

/// </summary>

void setPlayerComand(string comand_name);

/// <summary>

/// Print all player info

/// </summary>

void getPlayerInfo();

bool operator == (const FootbalPlayer& source);

};

Файл CPlayer.cpp

#include "CPlayer.h"

void FootbalPlayer::setPlayerName(string name)

{

this->fullname = name;

}

void FootbalPlayer::getPlayerInfo()

{

cout <<"[ "<< fullname << ", "<< description.getNumber()<<", "<< description.getComand()<< " ]"<< endl;

}

void FootbalPlayer::setPlayerNumber(int number)

{

description.setNumber(number);

}

void FootbalPlayer::setPlayerComand(string comand_name)

{

description.setComand(comand_name);

}

bool FootbalPlayer::operator==(const FootbalPlayer& source)

{

if (this->fullname == source.fullname & description == source.description)

return true;

else

return false;

}

void CPlayerDescription::setNumber(int number)

{

this->num = number;

}

void CPlayerDescription::setComand(string comand_name)

{

this->comand = comand_name;

}

int CPlayerDescription::getNumber()

{

return this->num;

}

string CPlayerDescription::getComand()

{

return this->comand;

}

bool CPlayerDescription::operator==(const CPlayerDescription& source)

{

if (this->num == source.num & this->comand == source.comand)

return true;

else

return false;

}

Файл main.cpp

#include "CPlayer.h"

void main()

{

FootbalPlayer a("Friedrich Nietzsche", 16, "OverMan");

FootbalPlayer b("Friedrich Nietzsche", 16, "OverMan");

FootbalPlayer c("Karl Marx'", 3, "DasKapital");

a.getPlayerInfo();

b.getPlayerInfo();

c.getPlayerInfo();

if (a == b)

cout << "Equal" << endl;

else

cout << "Not equal" << endl;

if (a == c)

cout << "Equal" << endl;

else

cout << "Not equal" << endl;

}

Работа программы