CPP学习笔记(自用)——文件读写和友元

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类声明之前定义。

使用友元的另一种情况:函数需要访问两个类的私有数据

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值