Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

PL6 / ПЗ6_ООП

.pdf
Скачиваний:
0
Добавлен:
20.12.2024
Размер:
240.33 Кб
Скачать

Міністерство освіти і науки України

Харківський національний університет радіоелектроніки

Кафедра системотехніки Звіт з практичної роботи № 6

З ОСНОВ ПРОЕКТУВАННЯ ТА РЕАЛІЗАЦІЇ ПРОСТИХ КЛАСІВ

Виконали: студенти групи

Перевірили:

КНТ-23-1

доц. Вишняк М. Ю.

Зайцев М. Ю.

 

Бойко А. Ю.

 

Кравченко Р. С.

 

Локтіонов Р. В.

 

Літвін В. О.

 

Харків 2024

06 Pract Templates

Templates

PRG-1. Create a templated class Array that can handle an array of objects of any type and any size in the heap. Define an add member function to add elements to the end of the array. Define a print function to print all elements in the array. Test your program with arrays of type int, double, and char.

Solution:

#include <iostream>

template<class T> class Array

{

private:

size_t size; size_t capacity; T* data;

public:

Array() : size(0), capacity(0), data(nullptr) { }

Array(size_t capacity) : capacity(capacity), size(0)

{

this->data = new T[this->capacity];

}

void add(const T num)

{

if (this->size >= this->capacity)

{

this->capacity = (this->capacity + 1) * 2;

T* temp = new T[capacity]; if (this->data)

{

std::copy(this->data, this->data + this->size, temp);

delete[] data;

}

this->data = temp;

}

this->data[size] = num; ++this->size;

}

void print() const

{

if (this->size == 0) return;

for (int i = 0; i < size; i++) std::cout << data[i] << ' ';

std::cout << '\n';

}

void erase(const size_t index)

{

if (index < 0 || index >= this->size) return;

T* temp = new T[capacity];

for (size_t i = 0; i < size; i++)

{

if (i > index)

temp[i - 1] = data[i];

else if (i < index) temp[i] = data[i];

}

delete[] data;

this->data = temp; --this->size;

}

T& operator[](const size_t index)

{

if (index < 0 || index >= this->size) return data[0];

return data[index];

}

~Array()

{

delete[] data;

}

};

int main()

{

Array<int> int_array;

for (int i = 0; i < 10; i++) int_array.add(i);

int_array.print(); int_array.erase(2); int_array.print();

Array<double> double_array;

for (double i = 0; i < 10; i++) double_array.add(i);

double_array.print(); double_array.erase(7); double_array.print();

Array<char> char_array;

for (char i = 57; i < 66; i++) char_array.add(i);

char_array.print(); char_array.erase(4); char_array.print(); return 0;

}

PRG-2. A queue is a first-in, first-out structure. An example of a queue is a line of people waiting to be served. We can implement a queue using an array in which the items are inserted (enqueued) at the end of the array and removed (dequeued) from the front. Create a templated class and a queue of any type. Then test the queue in two separate application files: one as a queue of integers, the other as a queue of strings. Add appropriate try-catch blocks to catch adding to the full queue or removing from an empty queue.

Solution:

#include <iostream>

template<class T> class Queue

{

private:

size_t capacity; size_t size;

T* data;

public:

Queue() : capacity(0), size(0), data(nullptr) { }

Queue(int capacity) : capacity(capacity), size(0)

{

this->data = new T[capacity];

}

size_t getSize() const { return size; }

void emplace(T num)

{

try {

if (size >= capacity)

throw "Queue is overloaded!";

this->data[this->size] = num; this->size++;

}

catch (const char* exp) { std::cerr << exp << std::endl;

}

}

T& front()

{

try

{

if (this->size == 0)

throw "You have zero elements!";

return this->data[0];

}

catch (const char* exp)

{

std::cerr << exp << std::endl;

}

}

void erase()

{

try

{

if (this->size == 0)

throw "You have zero elements!";

T* temp = new T[this->capacity];

for (size_t i = 1; i < this->size; i++) temp[i - 1] = this->data[i];

delete[] data;

this->data = temp; this->size--;

}

catch (const char* exp)

{

std::cerr << std::endl << exp;

}

}

~Queue()

{

delete[] data;

}

};

int main() { Queue<int> q(10);

for (int i = 0; i <= 10; i++) q.emplace(i);

while (q.getSize())

{

std::cout << q.front() << ' '; q.erase();

}

q.erase();

return 0;

}