Вы находитесь на странице: 1из 3

Министерство образования Республики Беларусь

Учреждение образования
«Брестский государственный технический университет»
Кафедра ИИТ

Лабораторная работа №7

По дисциплине «ППвИС за III семестр»

Тема: «Шаблоны»

Выполнил:
Студент 2 курса
Группы ИИ-19
Салей О.В
Проверил:
Монтик Н.С

Брест 2021
Код:
#include <iostream>

using namespace std;

namespace A
{
template<typename T1, typename T2>
class Data
{
T1 a;
T2 str;
public:
Data(int a, string str) :a(a), str(str) {};
~Data() {};
void GetAB()
{
cout << "переменная интового в первом классе = " << this->a << endl;
cout << "переменная строкового типа в первом классе = " << this->str << endl;
}
};
}

template<typename T1, typename T2>


class Int
{
T1 a;
T2 b;
public:
Int(T1 a, T2 b) :a(a), b(b) {};
~Int() {};
void GetAB()
{
cout << "переменная а во втором классе = " << this->a << endl;
cout << "переменная b во втором классе = " << this->b << endl;
}
int Minus()
{
if (this->a > this->b)
return this->a - this->b;
else
return this->a - this->b;
}
};

namespace B
{
template<typename T1, typename T2, typename T3>
class Data
{
public:
Data(T1 info1, T2 info2, T3 info3) :info1(info1), info2(info2), info3(info3) {};
~Data() {};
void ShowInfo()
{
cout << this->info1 << " " << this->info2 << " " << this->info3 << endl;
}
T1 GetInfo1()
{
return this->info1;
}
T2 GetInfo2()
{
return this->info2;
}
T3 GetInfo3()
{
return this->info3;
}
private:
T1 info1;
T2 info2;
T3 info3;
};
};

int main()
{
setlocale(0, "");

A::Data<int, string> A(5, "Petya");


A.GetAB();
cout << endl;

Int<int, double> B(2, 1.724);


B.GetAB();
cout << "Разность двух переменных во 2 классе = " << B.Minus() << endl << endl;

B::Data<double, string, int> C(5.8765, "wasdfff", 777);


C.ShowInfo();
cout << "Info of third class: " << C.GetInfo1() << " " << C.GetInfo2() << " " <<
C.GetInfo3() << endl;

cin.get();
return 0;
}

Результаты:

Вывод: В ходе лабораторной работы был ознакомлен с правилом написания шаблонов


пользовательских классов.

Вам также может понравиться