C++中流重载使用方法

codelist_0.cpp

#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>
#include<stdlib.h>
#include<set>

using namespace std;
class type
{
private:
	int str1;
	int str2;
	int str3;
	
public:
	type() {
		str1 = 0, str2 = 0, str3 = 0;
	}
	~type() {};
	friend istream & operator>>(istream & in, type &cu ) {
		in >> cu.str1 >> cu.str2 >> cu.str3;
		return in;
	}
	friend ostream & operator<<(ostream & out, const type &st) {
		out << st.str1 << st.str2 << st.str3 << endl;
		return out;
	}
};

int main() {
	type type0;
	cin >> type0;
	cout << type0;
}

这应该是最简单的重载流示例了

最后由 RNAtlance203 编辑

那么有什么实用价值呢

如下
codelist_1.cpp

// codelist_1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>
#include<stdlib.h>
#include<set>

using namespace std;
class type
{
private:
	int str1;
	int str2;
	int str3;
	
public:
	type() {
		str1 = 0, str2 = 0, str3 = 0;
	}
	~type() {};
	friend istream & operator>>(istream & in, type &cu ) {
		in >> cu.str1 >> cu.str2 >> cu.str3;
		return in;
	}
	friend ostream & operator<<(ostream & out, const type &st) {
		out << st.str1 << st.str2 << st.str3 << endl;
		return out;
	}
	int getstr1() {
		return str1;
	}
	int getstr2() {
		return str2;
	}
	int getstr3() {
		return str3;
	}
};

int main() {
	type type0;
	ofstream fkb("fkb.txt");
	cin >> type0;
	fkb << type0.getstr1() << type0.getstr2() << type0.getstr3() << endl;
	return 0;
}

实际作用大概是省下了从键盘cin到文件整个过程的文字量,而且这份代码是示例,class只有3个私有成员,如果是大型工程,一个一个cin写那必然是要出人命的

最后由 RNAtlance203 编辑

还可以拓展一下 虽然用了臭名昭著的goto,(但是)对于这种规模的东西也没必要上那些高端操作其实就是偷懒
codelist_2.cpp

// codelist_2.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>
#include<stdlib.h>
#include<set>

using namespace std;
class type
{
protected:
	int str1;
	int str2;
	int str3;
	
public:
	type() {
		str1 = 0, str2 = 0, str3 = 0;
	}
	~type() {};
	friend istream & operator>>(istream & in, type &cu ) {
		in >> cu.str1 >> cu.str2 >> cu.str3;
		return in;
	}
	friend ostream & operator<<(ostream & out, const type &st) {
		out << st.str1 << st.str2 << st.str3 << endl;
		return out;
	}
	int getstr1() {
		return str1;
	}
	int getstr2() {
		return str2;
	}
	int getstr3() {
		return str3;
	}
};

void readFile() {
	type type0;
	ifstream fkb("fkb.txt");
	fkb >> type0;
	cout << type0 << endl;
	fkb.close();
}

void writeFile() {
	type type0;
	ofstream fkb("fkb.txt");
	cin >> type0;
	fkb << type0 << endl;
	fkb.close();
}



int main() {
	int choice;
MAIN: {
	system("cls");
	cout << "1=read/2=write/0=exit" << endl;
	cin >> choice;
	if (choice == 1) goto READ;
	else
	{
		if (choice == 2) goto WRITE;
		else return 0;
		}
	}

READ: {
	  readFile();
	  system("pause");
	  goto MAIN;
	}

WRITE:{
	  writeFile();
	  system("pause");
	  goto MAIN;
	}
	return 0;
}
最后由 RNAtlance203 编辑

还有个问题,就是如果私有或者受保护成员是字符串或者数组的话怎么整
(史前大坑)

还有,我是不写注释人,自己的代码只要自己看得懂即可

@rnatlance203C++中流重载使用方法 中说:

还可以拓展一下 虽然用了臭名昭著的goto,(但是)对于这种规模的东西也没必要上那些高端操作其实就是偷懒
codelist_2.cpp

// codelist_2.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>
#include<stdlib.h>
#include<set>

using namespace std;
class type
{
protected:
	int str1;
	int str2;
	int str3;
	
public:
	type() {
		str1 = 0, str2 = 0, str3 = 0;
	}
	~type() {};
	friend istream & operator>>(istream & in, type &cu ) {
		in >> cu.str1 >> cu.str2 >> cu.str3;
		return in;
	}
	friend ostream & operator<<(ostream & out, const type &st) {
		out << st.str1 << st.str2 << st.str3 << endl;
		return out;
	}
	int getstr1() {
		return str1;
	}
	int getstr2() {
		return str2;
	}
	int getstr3() {
		return str3;
	}
};

void readFile() {
	type type0;
	ifstream fkb("fkb.txt");
	fkb >> type0;
	cout << type0 << endl;
	fkb.close();
}

void writeFile() {
	type type0;
	ofstream fkb("fkb.txt");
	cin >> type0;
	fkb << type0 << endl;
	fkb.close();
}



int main() {
	int choice;
MAIN: {
	system("cls");
	cout << "1=read/2=write/0=exit" << endl;
	cin >> choice;
	if (choice == 1) goto READ;
	else
	{
		if (choice == 2) goto WRITE;
		else return 0;
		}
	}

READ: {
	  readFile();
	  system("pause");
	  goto MAIN;
	}

WRITE:{
	  writeFile();
	  system("pause");
	  goto MAIN;
	}
	return 0;
}

我是不知道我拓展了个什么玩意,我只知道大半夜很容易出错

函数void readFile()只能读第一行而且会把所有数据连在一起,一眼就能看出;
函数void writeFile()写入的时候会把所有数据都连在一起,再读取的话就gg;

修复的方法也很简单

//在class type里的插入流重载中--------------------
friend ostream & operator<<(ostream & out, blu &cu) {
	out << cu.s << '\t' << cu.c << '\t' << cu.p;
	return out;
}
//-----------------------------------------------

void readFile() {
	char str[100];
	ifstream fkb("fkb.txt");
	while (fkb.getline(str, 100)) {
		cout << str << endl;
	}
}

void writeFile() {
	type type0;
	ofstream fkb("fkb.txt", ios::app);
	cin >> type0;
	fkb << type0 << endl;
	fkb.close();
}
最后由 RNAtlance203 编辑

@rnatlance203C++中流重载使用方法 中说:

还有个问题,就是如果私有或者受保护成员是字符串或者数组的话怎么整
(史前大坑)

我发现我就是个人才,就算挖坑都不会把自己坑到

class blu {
protected:
	char s[100];
	char c[100];
	char p;
public:
	blu() {
		strcpy(s, "fsk");
		strcpy(c, "fkb");
		p = 'z';
	}
	~blu() {

	}
	friend istream & operator>>(istream & in, blu &cu) {
		in >> cu.s >> cu.c >> cu.p;
		return in;
	}
	friend ostream & operator<<(ostream & out, blu &cu) {
		out << cu.s << '\t' << cu.c << '\t' << cu.p;
		return out;
	}
};

这不是跟上面的整型变量一样了吗 成员函数都不用写了,爽死