Академический Документы
Профессиональный Документы
Культура Документы
Учреждение образования
«Брестский государственный технический университет»
Кафедра ИИТ
Лабораторная работа №8
Выполнил:
Студент 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;
}
};
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");
Char Second;
Second.ReadFile();
Second.BringToMonitor();
cin.get();
return 0;
}
Результаты: