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

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

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

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

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

Тема: «Потоки ввода-вывода»

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

Брест 2021
Код:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

namespace A
{
class Data
{
string name;
int age;
public:
Data() {};
Data(string name, int age) :name(name), age(age) {};
~Data() {};

void WriteFile()
{
ofstream fout("D:\\Pivas.txt", ios::app);
fout << this->name << endl;
fout << this->age << endl;
fout.close();
}
friend ofstream& operator<<(ofstream& out, const Data& obj);

void BringToMonitor()
{
cout << "First class:\n";
cout << "Name: " << this->name << endl;
cout << "Age:" << this->age << endl;
}
};

ofstream& operator<<(ofstream& out, const Data& obj)


{
out << obj.name << endl;
out << obj.age << endl;
out.close();
return out;
}
};

class Char
{
char str[10];
public:
Char() {};
Char(int a)
{
cin.get();
cout << "\nEnter the string of 2 class:\n";
cin.getline(str, 10);
};
~Char() {};

char ReadFile()
{
string strBuff; char ch;
ifstream fin("D:\\Pivas.txt");
while (fin.get(ch))
strBuff += ch;
for (int i = 0; i < strBuff.size(); i++)
{
if (strBuff[i] != '\n')
this->str[i] = strBuff[i];
else
{
str[i] = '\0';
break;
}
}
fin.close();
return *str;
}

void BringToMonitor()
{
puts("\nName of the second class: ");
puts(str);
}
};

namespace A1
{
class Data
{
string name;
int age;
public:
Data() {};
Data(string name, int age) :name(name), age(age) {};
~Data() {};

void WriteFile()
{
string strBuff;
int tmp = 0;
ifstream fin("D:\\Pivas.txt", ios::trunc);
getline(fin, strBuff);
for (int i = 0; i < strBuff.size(); i++)
{
if (strBuff[i] != '\n')
this->name[i] = strBuff[i];
else
{
break;
tmp = i;
}
}
for (int i = tmp; i < strBuff.size(); i++)
{
this->age = (int)strBuff[i] - 48;
}
fin.close();
}

void BringToMonitor()
{
cout << "Third class:\n";
cout<< "Name: " << this->name << endl;
cout<< "Age:" << this->age << endl;
}
};
}
int main()
{
setlocale(0, "");
string str;
int age;
ofstream fout("D:\\Pivas.txt");

cout << "Enter the first name:\n";


cin >> str;
cout << "Enter the age of the man:\n";
cin >> age;
A::Data First(str, age);
fout << First;
First.BringToMonitor();

Char Second;
Second.ReadFile();
Second.BringToMonitor();

cout << "\nEnter the third name:\n";


cin >> str;
cout << "Enter the age of the man:\n";
cin >> age;
A1::Data Third(str, age);
Third.WriteFile();
Third.BringToMonitor();

cin.get();
return 0;
}

Результаты:

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


ввода-вывода C++ (иерархия классов iostream).

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