
PL1 / ПЗ1_ООП
.pdfМіністерство освіти і науки України
Харківський національний університет радіоелектроніки
Кафедра системотехніки Звіт з практичної роботи № 1
З ОСНОВ ПРОЕКТУВАННЯ ТА РЕАЛІЗАЦІЇ ПРОСТИХ КЛАСІВ
Виконали: студенти групи |
Перевірили: |
КНТ-23-1 |
доц. Вишняк М. Ю. |
Зайцев М. Ю. |
|
Бойко А. Ю. |
|
Кравченко Р. С. |
|
Локтіонов Р. В. |
|
Літвін В. О. |
|
Харків 2024

4. Створити клас ABONENT, який містить такі поля: прізвище абонента; ініціали абонента; номер телефону; домашня адреса. Написати програму, що використовує цей клас і виконує такі дії:
-вводить з клавіатури масив даних TELEFON, що складається з № змінних типу ABONENT;
-виводить на екран прізвище, ініціали та домашню адресу за введеним номером телефону, або виводить повідомлення про його відсутність.
Код:
#include <iostream> #include <string> #include <vector> using namespace std;
class Abonent { string surname; string pNumber; string address;
public:
static int ID_All;
Abonent(const string& surname, const string& tel, const string& adress)
: surname(surname), pNumber(tel), address(adress) {}
void setSur(const string& surname)
{
this->surname = surname;
}
void setTel(const string& tel)
{
this->pNumber = tel;
}
void setAdd(const string& address)
{
this->address = address;
}
const string getSurname() const
{
return this->surname;
}
const string getPNumber() const
{
return this->pNumber;
}
const string getAddress() const
{
return this->address;
}
};

static void addAbonent(std::vector<Abonent>& telefon)
{
system("cls");
string surname, tel, address;
std::cout << "Enter surname: "; std::cin >> surname;
std::cout << "Enter phone number: "; std::cin >> tel;
std::cout << "Enter adress: "; std::cin >> address;
telefon.emplace_back(surname, tel, address);
}
static void showAbonents(const std::vector<Abonent>& telefon)
{
if (telefon.empty())
{
system("cls");
std::cout << "There are no abonents in tour telefon:(\n";
system("pause"); return;
}
for(size_t i = 0;i < telefon.size(); ++i)
{
auto& temp = telefon[i];
std::cout << i + 1 << ":\nSurname: " << temp.getSurname() << "\nAddress: " << temp.getAddress() <<
"\nPhone Number: " << temp.getPNumber() << std::endl <<
std::endl;
}
system("pause");
}
int main()
{
std::vector<Abonent> telefon; char choice;
bool exit = false; while (!exit)
{
system("cls");
std::cout << "Hello to the Telefon!\n" << "What do you want to do today?\n" << "1) Show abonents\n" <<
"2) Add abonent\n" << "3) Exit\n";
std::cin >> choice; switch (choice)
{
case '1': showAbonents(telefon); break;
case '2': addAbonent(telefon);

break;
default:
std::cout << "Goodbye!"; exit = true;
break;
}
}
return EXIT_SUCCESS;
}
7. Створити клас ВООК, який містить такі поля: назва книги;
автори книги; дата друку; ціна книги. Написати програму, що використовує цей клас і виконує такі дії: вводить з клавіатури масив даних SHOP, що складається з № змінних типу BOOK; виводить на екран всі книги які були надруковані в заданому році.
Код:
#include <iostream> #include <cstring> #include <vector> #include <stdio.h>
class Book
{
public:
struct Date
{
int day; std::string month; int year;
}date;
std::string name; std::string authors; int price;
Book() {}
Book(std::string name, std::string authors, int day, std::string month, int year, int price)
{
this->name = name; this->authors = authors; this->price = price;
this->date.day = day; this->date.month = month; this->date.year = year;
}
static void addValues(std::vector<Book>& SHOP, int N)
{
for (int i = 0; i < N; i++) { Book book;

std::cout << "Name:"; std::cin >> book.name;
std::cout << "Authors:"; std::cin >> book.authors;
std::cout << "Day:"; std::cin >> book.date.day;
std::cout << "Month:"; std::cin >> book.date.month;
std::cout << "Year:"; std::cin >> book.date.year;
std::cout << "Price:"; std::cin >> book.price;
SHOP.push_back(book);
}
}
};
int main()
{
int year = 0; int N = 0;
std::string chooseClass;
std::cout << "How many books do you want to add? "; std::cin >> N;
std::vector<Book> SHOP; Book::addValues(SHOP, N);
std::cout << "What year do you want to see?"; std::cin >> year;
std::cout << "Books, that are printed in " << year << std::endl; for (const auto& bookInDataBase : SHOP)
if (bookInDataBase.date.year == year)
std::cout << "Name: " << bookInDataBase.name << ", Authors: " << bookInDataBase.authors << ", Price: " << bookInDataBase.price << "$" << std::endl;
return 0;
}
19. «Матриця 3х3 цілих чисел».
Дані класу: статичний масив елементів матриці. Функції класу: додавання й множення матриць, множення й додавання матриці із цілим числом, порівняння двох матриць, обчислення визначника матриці, транспонування матриці.
#include <string> #include <vector> #include <iostream>
using namespace std;

class Matrix { public:
int mat[3][3];
Matrix(int(&a)[3][3])
{
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) mat[i][j] = a[i][j];
}
void print()
{
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++) cout << mat[i][j] << "\t";
cout << endl;
}
cout << endl;
}
const int determinant()
{
return (mat[0][0] * mat[1][1] * mat[2][2]) + (mat[0][1] * mat[1][2] * mat[2][0]) + (mat[0][2] * mat[1][0] * mat[2][1]) - (mat[0][2] * mat[1][1] * mat[2][0]) - (mat[0][1] * mat[1][0] * mat[2][2]) - (mat[0][0] * mat[1][2] * mat[2][1]);
}
void transportirovanie()
{
Matrix ma(mat);
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) mat[i][j] = ma.mat[j][i];
}
bool operator==(Matrix& other)
{
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
if (mat[i][j] != other.mat[i][j]) return false;
return true;
}
Matrix operator+(Matrix& other)
{
Matrix temp(mat);
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) temp.mat[i][j] += other.mat[i][j];
return temp;
}

Matrix operator+(int number)
{
Matrix temp(mat);
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) temp.mat[i][j] += number;
return temp;
}
Matrix operator*(int number)
{
Matrix temp(mat);
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) temp.mat[i][j] *= number;
return temp;
}
Matrix operator*(Matrix& other)
{
int temp[3][3];
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
temp[i][j] = (mat[i][0] * other.mat[0][j]) + (mat[i][1] * other.mat[1][j]) +
(mat[i][2] * other.mat[2][j]);
return Matrix(temp);
}
};
int main()
{
int temp[3][3] = { 1,2,3,4,5,6,7,8,9 }; int temp2[3][3] = { 1,2,3,4,5,6,7,8,9 };
Matrix m1(temp);
Matrix m2(temp2);
Matrix show = m1;
cout << "m1,m2:" << endl << endl; show.print();
show = m1 + m2;
cout << "m1+m2" << endl << endl; show.print();
show = m1 * m1;
cout << "m1*m2" << endl << endl; show.print();
cout << "m1+10" << endl << endl; show = m1 + 10;
show.print();
cout << "m1*10" << endl << endl; show = m1 * 10;
show.print();
cout << "Determinate: " << show.determinant();

cout << endl << endl << "Transportirovanie" << endl << endl; show.transportirovanie();
show.print();
int a = |
m2 |
== m1; |
|
cout |
<< |
endl << "Does m1==m2?" << endl << endl; |
|
cout |
<< |
a; |
|
}