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

Отчеты по лабам / Лабораторная №5 Акименко

.docx
Скачиваний:
4
Добавлен:
20.12.2021
Размер:
310.17 Кб
Скачать

Дисциплина: Объектно-ориентированное программирование

Группа: ИКПИ-02

Акименко Полина

Вариант 2

ЛАБОРАТОРНАЯ РАБОТА N 5

Шаблоны классов. Работа с исключительными

ситуациями языка С++

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

В настоящей лабораторной работе необходимо решить две задачи, связанные с организацией шаблонов классов. Первая из задач состоит в преобразовании в шаблон класс Комплексных Чисел с двумя полями, который был разработан в первой лабораторной работе по ООП. Вторая задача состоит в разработке шаблона класса для динамического одномерного массива. При решении второй задачи следует предусмотреть обработку исключительных ситуаций.

2. Исходный код

Файл ComplexNumbers.cpp:

#include <stdio.h>

#include <stdlib.h>

template <typename R = double, typename I = double>

class complex_c

{

R real;

I img;

public:

complex_c()

{

real = 0;

img = 0;

}

complex_c(const complex_c& arg)

{

this->real = arg.real;

this->img = arg.img;

}

complex_c(R real, I img)

{

this->real = real;

this->img = img;

}

complex_c operator+(const complex_c& arg)

{

complex_c result;

result.real = this->real + arg.real;

result.img = this->img + arg.img;

return result;

}

complex_c operator-(const complex_c& arg)

{

complex_c result;

result.real = this->real - arg.real;

result.img = this->img - arg.img;

return result;

}

complex_c operator*(const complex_c& arg)

{

complex_c result;

result.real = this->real*arg.real - this->img*arg.img;

result.img = this->real*arg.img + this->img*arg.real;

return result;

}

complex_c operator/(const complex_c& arg)

{

complex_c result;

result.real = (this->real*arg.real + this->img*arg.img) / (arg.real*arg.real + arg.img*arg.img);

result.img = (this->img*arg.real - this->real*arg.img) / (arg.real*arg.real + arg.img*arg.img);

return result;

}

R get_real()

{

return real;

}

I get_img()

{

return img;

}

void print()

{

printf("%f + i%f\n", real, img);

}

};

int main()

{

{

complex_c<> complex;

complex.print();

}

{

complex_c<float, float> complex(35.2456, 624345.642);

complex.print();

}

{

complex_c<float, double> complex_arg(425.542, 24624.735);

complex_c<float, double> complex(complex_arg);

complex.print();

}

printf("\n");

{

complex_c<float> complex1(325.6524, 1834.673);

complex_c<float> complex2(2577.533, 35.8466);

(complex1 + complex2).print();

}

{

complex_c<> complex1(325.6524, 1834.673);

complex_c<> complex2(2577.533, -35.8466);

(complex1 - complex2).print();

}

{

complex_c<> complex1(-3.5, 0);

complex_c<> complex2(25, 64.5);

(complex1 * complex2).print();

}

{

complex_c<> complex1(13.67, 144.25);

complex_c<> complex2(41.4, 4);

(complex1 / complex2).print();

}

system("pause");

}

Файл Array.cpp:

#include <stdio.h>

#include <typeinfo>

#include <stdlib.h>

template <size_t S = 8, typename T = int, T dv = 0>

class static_array

{

T arr[S];

int size = S;

size_t typeSize = sizeof(T);

public:

static_array()

{

for (int i = 0; i < S; i++)

arr[i] = dv;

}

T& operator[](int index)

{

if (index < 0)

throw "error: index below zero";

else if (index >= S)

throw "error: index out of bounds";

else return arr[index];

}

int get_size()

{

return S;

}

size_t get_typeSize()

{

return typeSize;

}

void print()

{

if (typeid(T) == typeid(char))

{

printf("contents of static array with type \"character\":\nindex\tvalue\n");

for (int i = 0; i < S; i++)

printf("%d\t%c\n", i, arr[i]);

}

else if (typeid(T) == typeid(bool))

{

printf("contents of static array with type \"boolean\":\nindex\tvalue\n");

for (int i = 0; i < S; i++)

if (arr[i]) printf("%d\ttrue\n", i);

else printf("%d\tfalse\n", i);

}

else

{

printf("contents of static array with type \"digit\":\nindex\tvalue\n");

for (int i = 0; i < S; i++)

printf("%d\t%d\n", i, arr[i]);

}

}

};

int main()

{

{

static_array<6, bool, false> arr;

try

{

arr[4] = true;

} catch (const char* e)

{

printf("%s\n", e);

}

arr.print();

}

printf("\n");

{

static_array<10> arr;

try

{

arr[8] += 12;

arr[-1] = 7;

} catch (const char* e)

{

printf("%s\n\n", e);

}

arr.print();

}

printf("\n");

{

static_array<4, char, 'o'> arr;

try

{

arr[1] = 'f';

arr[16] = 'b';

} catch (const char* e)

{

printf("%s\n\n", e);

}

arr.print();

}

system("pause");

}

3. Скриншоты работы программы

Рисунок 1 - Результат работы программы ComplexNumbers.cpp

Рисунок 2 - Результат работы программы Array.cpp