2023-10-23
文件流
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Person
{
public:
char name[40];
int age;
};
int main()
{
ofstream ofs;
ofs.open("test.txt", ios::out);
ofs << "operator" << endl;
ofs << "name:Mizuki" << endl;
ofs << "gender:Male" << endl;
ofs << "color:Blue" << endl;
ofs.close();
ifstream ifs;
ifs.open("test.txt", ios::in);
if (!ifs.is_open())
{
cout << "文件打开失败!" << endl;
return 0;
}
// 读取数据
// 第一种
// char buf[1024] = {0};
// while (ifs >> buf)
// {
// cout << buf << endl;
// }
// 第二种
// char buf1[1024] = {0};
// while (ifs.getline(buf1, sizeof(buf1)))
// {
// cout << buf1 << endl;
// }
// 第三种
// string a;
// while (getline(ifs, a))
// {
// cout << a << endl;
// }
// 第四种
char c;
while ((c = ifs.get()) != EOF)
{
cout << c;
}
ifs.close();
// 二进制文件
ofstream ofss("erjinzhi.txt", ios::out | ios::binary); // 可以直接使用构造函数在这里写好
Person p = {"Mizuki", 18};
ofss.write((const char *)&p, sizeof(Person));
ofss.close();
// 二进制读文件一般使用read
ifstream ifss;
ifss.open("erjinzhi.txt", ios::in | ios::binary);
if (!ifss.is_open())
{
cout << "文件打卡失败!" << endl;
return 0;
}
Person pp;
ifss.read((char *)&pp, sizeof(Person));
cout << "----------------" << endl;
cout << "Name: " << pp.name << " Age: " << pp.age << endl;
ifss.close();
}
友元类
tv.h
#ifndef __TV_H__
#define __TV_H__
#include<iostream>
class Tv
{
private:
int state; // 状态
int volume;
int maxChannel;
int channel;
int mode;
int input;
public:
friend class Remote; // 友元Remote,遥控器类
enum
{
Off,
On
};
enum
{
MinVal,
MaxVal = 20
};
enum
{
Antenna,
Cable
}; // 有线接收或者广播接收
enum
{
TV,
DVD
};
Tv(int s = Off, int mc = 125) : state(s), volume(5), maxChannel(mc), channel(2), mode(Cable), input(TV) {} // 构造函数,使用默认赋值
// void onoff() { state = (state == On) ? Off : On; }
// 上述转换状态函数可以使用按位异或优化
void onoff() { state ^= 1; }
bool ison() const { return state == On; }
bool volup();
bool voldown();
void chanup();
void chandown();
void set_mode() { mode = (mode == Antenna) ? Cable : Antenna; }
void set_input() { input = (input == TV) ? DVD : TV; }
void settings() const; // 显示所有设置项
};
class Remote
{
private:
int mode;
public:
Remote(int m = Tv::TV) : mode(m) {}
bool volup(Tv &t) { return t.volup(); }
bool voldown(Tv &t) { return t.voldown(); }
bool onoff(Tv &t) { t.onoff(); }
bool chanup(Tv &t) { t.chanup(); }
bool chandown(Tv &t) { t.chandown(); }
void set_chan(Tv &t, int c) { t.channel = c; }
void set_mode(Tv &t) { t.set_mode(); }
void set_input(Tv &t) { t.set_input(); }
};
#endif
tv.cpp
#include "tv.h"
using namespace std;
bool Tv::volup()
{
if (volume < MaxVal)
{
volume++;
return true;
}
else
return false;
}
bool Tv::voldown()
{
if (volume > MinVal)
{
volume--;
return true;
}
else
return false;
}
void Tv::chanup()
{
if (channel < maxChannel)
channel++;
else
channel = 1;
}
void Tv::chandown()
{
if (channel > 1)
channel--;
else
channel = maxChannel;
}
void Tv::settings() const
{
cout << "TV is " << (state == Off ? "Off" : "On") << endl;
if (state == On)
{
cout << "Volume setting = " << volume << endl;
cout << "Channel setting = " << channel << endl;
cout << "Mode = " << (mode == Antenna ? "Antenna" : "Cable") << endl;
cout << "Input = " << (input == TV ? "TV" : "DVD") << endl;
}
}
use_TV.cpp
#include "tv.h"
int main()
{
using std::cout;
using std::endl;
Tv T114514;
cout << "Initial----------" << endl;
T114514.settings();
T114514.onoff();
T114514.chanup();
T114514.settings();
cout << "Adjust-----------" << endl;
Remote r;
r.set_chan(T114514, 5);
r.volup(T114514);
r.set_mode(T114514);
T114514.settings();
cout << "---------------" << endl;
Tv T1919810(Tv::On);
T1919810.set_mode();
r.volup(T1919810);
r.set_chan(T1919810, 114);
T1919810.settings();
return 0;
}
可以让两个类成为彼此的友元,但是注意使用Remote对象的Tv方法,原型可以在Remote类声明之前声明,但不能在Remote类声明之前定义。
使用友元的另一种情况:函数需要访问两个类的私有数据