
ФЕДЕРАЛЬНОЕ ГОСУДАРСТВЕННОЕ БЮДЖЕТНОЕ ОБРАЗОВАТЕЛЬНОЕ УЧРЕЖДЕНИЕ ВЫСШЕГО ОБРАЗОВАНИЯ "САНКТ-ПЕТЕРБУРГСКИЙ ГОСУДАРСТВЕННЫЙ УНИВЕРСИТЕТ ТЕЛЕКОММУНИКАЦИЙ ИМ. ПРОФ. М. А. БОНЧ-БРУЕВИЧА"
Факультет инфокоммуникационных сетей и систем
Кафедра программной инженерии и вычислительной техники
Лабораторная работа №3
«ОТНОШЕНИЕ НАСЛЕДОВАНИЯ»
по дисциплине «Объектно-ориентированное программирование»
Выполнил:
студент 2 курса
дневного отделения
группы ИКПИ-81
Коваленко Л. А.
Санкт-Петербург 2019 а. Постановка задачи
Дополнить систему, состоящую из двух классов COne и CTwo, которые были разработаны в лабораторной работе 2, новым классом CThree. Новый класс должен быть связан public наследованием с классом CTwo. Класс CThree должен имеет одно поля, которое выбирается студентом самостоятельно. Для разрабатываемого класса написать конструкторы умолчания, с параметрами и конструктор копирования, деструктор, методы доступа и метод print(). Написать тестовую программу для проверки работоспособности разработанных классов.
Б. Таблицы атрибутов классов
Таблица атрибутов класса COne
N |
Назначение |
Идентификатор |
Секция |
1 |
Число типа long |
L (long) |
protected |
2 |
Строка типа std::string |
S (std::string) |
protected |
Таблица атрибутов класса CTwo
N |
Назначение |
Идентификатор |
Секция |
1 |
Указатель на объект типа COne |
P (COne *) |
protected |
2 |
Строка типа std::string |
S (std::string) |
protected |
Таблица атрибутов класса CThree
N |
Назначение |
Идентификатор |
Секция |
1 |
Указатель на объект типа COne |
P (COne *) |
protected |
2 |
Строка типа std::string |
S (std::string) |
protected |
3 |
Указатель на массив |
a (double *) |
protected |
В. Код программы
Код программы с классами COne, CTwo и CThree |
#include <iostream> #include <string> #include <stdexcept> using std::cin; using std::cout; using std::endl; using std::string; class COne { protected: long L; string S; public: // Конструктор explicit COne(string input = "", long L = 0) : S(std::move(input)), L(L) {} // Конструктор копирования COne(const COne &arg) { L = arg.L; S = arg.S; } // Оператор копирования COne &operator=(const COne &arg) { COne temp(arg); std::swap(L, temp.L); std::swap(S, temp.S); return *this; } // Конструктор перемещения COne(COne &&arg) noexcept { L = arg.L, arg.L = 0; S = std::move(arg.S); } // Оператор перемещения COne &operator=(COne &&arg) noexcept { if (this != &arg) { std::swap(L, arg.L); std::swap(S, arg.S); arg.L = 0, arg.S.clear(); } return *this; } // Деструктор virtual ~COne() { S.clear(); }; // Получение доступа к значению L const long &getValue() const { return L; } // Получение доступа к строке S const string &getString() const { return S; } // Длина строки size_t len() const { return S.size(); } // Печать полей класса virtual void print() const { cout << "COne {" << L << ", \"" << S << "\"}"; } friend class CTwo; friend class CThree; }; class CTwo { protected: string S; COne *P; // ОТНОШЕНИЕ ВКЛЮЧЕНИЯ public: // Конструктор explicit CTwo(string s = "", string ps = "", int number = 0) : S(std::move(s)) { P = new COne(std::move(ps), number); } // Конструктор копирования CTwo(const CTwo &arg) { P = new COne(*arg.P); S = arg.S; } // Оператор копирования CTwo &operator=(const CTwo &arg) { CTwo temp(arg); std::swap(P, temp.P); std::swap(S, temp.S); return *this; } // Конструктор перемещения CTwo(CTwo &&arg) noexcept { P = arg.P, arg.P = nullptr; S = std::move(arg.S); } // Оператор перемещения CTwo &operator=(CTwo &&arg) noexcept { if (this != &arg) { std::swap(P, arg.P); std::swap(S, arg.S); delete arg.P, arg.P = nullptr; arg.S.clear(); } return *this; } // Деструктор virtual ~CTwo() { delete P; S.clear(); } // Получение доступа к объекту типа COne const COne *getCOne() const { return P; } // Получение доступа к строке S const string &getString() const { return S; } // Длина строки size_t len() const { return S.size(); } // Печать полей класса virtual void print() const { cout << "CTwo [ "; if (P) { cout << '\"' << S << "\", "; P->print(); } else { cout << "undefined"; } cout << " ]"; } }; class CThree : public CTwo { // ОТНОШЕНИЕ НАСЛЕДОВАНИЯ protected: double *a; public: // Конструктор explicit CThree(string s, string ps, int number = 0) : CTwo(std::move(s), std::move(ps), number) { if (number > 0) { a = new double[number] {0}; } else { throw std::bad_array_new_length(); } } // Конструктор копирования CThree(const CThree &arg) : CThree(arg.S, arg.P->S, arg.P->L) { std::copy(arg.a, arg.a + P->L, a); } // Оператор копирования CThree &operator=(const CThree &arg) { CThree temp(arg); swap(*this, temp); return *this; } // Конструктор перемещения CThree(CThree &&arg) noexcept : CTwo(std::move(arg)) { a = arg.a, arg.a = nullptr; } // Оператор перемещения CThree &operator=(CThree &&arg) noexcept { if (this != &arg) { swap(*this, arg); arg.S.clear(); delete arg.P, arg.P = nullptr; delete[] arg.a, arg.a = nullptr; } return *this; } // Деструктор virtual ~CThree() { delete[] a; } // Оператор индексации double &operator[](size_t idx) { return a[idx]; } // Печать полей класса void print() const override { cout << "CThree [ "; CTwo::print(); if (a) { cout << ", [" << a[0]; for (size_t i = 0; i < P->L; ++i) { cout << ", " << a[i]; } cout << "]"; } else { cout << " undefined"; } cout << " ]"; } // Дружественная функция, меняющая поля двух объектов местами friend void swap(CThree &first, CThree &second) noexcept { std::swap(first.P, second.P); std::swap(first.a, second.a); std::swap(first.S, second.S); } }; |