概述
重载<<和>>运算符
- 为什么要重载<< 和 >>
为了更方便的实现复杂对象的输入和输出。 - 在 C++ 中,左移运算符<<可以和 cout 一起用于输出,因此也常被称为“流插入运算符”或者“输出运算符”。
- 实际上,<<本来没有这样的功能,之所以能和 cout 一起使用,是因为被重载了。
- 两种方式实现:
第一种使用成员函数,不推荐,该方式没有实际意义
第二种使用友元函数实现
重载<<运算符 方式一:(使用成员函数, 不推荐,该方式没有实际意义)
Boy.h
#pragma once
#include <string>
#include <iostream> //包含头文件
using namespace std;
class Boy{
public:
Boy(const string name = "无名", int age = 0, int salary = 0);
//输出运算符重载
ostream& operator<<(ostream& os) const;
private:
string name;//姓名
int age; //年龄
int salary; //薪资
};
Boy.cpp
#include <sstream>
#include "Boy.h"
Boy::Boy(const string name, int age, int salary){
this->name = name;
this->age = age;
this->salary = salary;
}
//返回值是ostream的引用
ostream& Boy::operator<<(ostream& os) const{
os << "姓名:" << name << "t年龄:" << age << "t薪资:" << salary;
return os;
}
main.cpp
#include <Windows.h>
#include "Boy.h"
using namespace std;
int main(void) {
Boy boy1("张三", 18, 10000);
//调用 ostream& operator<<(ostream& os) const;
boy1 << cout << endl;
system("pause");
return 0;
}
重载<<运算符 方式二:使用友元函数
Boy.h
#pragma once
#include <string>
#include <iostream>
using namespace std;
class Boy{
public:
Boy(const string name = "无名", int age = 0, int salary = 0);
//使用友元函数 输出运算符重载
friend ostream& operator<<(ostream& os, const Boy& boy);
private:
string name;//姓名
int age; //年龄
int salary; //薪资
};
Boy.cpp
#include <sstream>
#include "Boy.h"
Boy::Boy(const string name, int age, int salary){
this->name = name;
this->age = age;
this->salary = salary;
}
main.cpp
#include <Windows.h>
#include "Boy.h"
using namespace std;
//这种方式方便输出 cout<<对象<<endl ;
ostream& operator<<(ostream& os, const Boy& boy) {
os << "姓名:" << boy.name << "t年龄:" << boy.age << "t薪资:" << boy.salary;
return os;
}
int main(void) {
Boy boy1("张三", 18, 10000);
//调用 friend ostream& operator<<(ostream& os, const Boy& boy);
cout << boy1 << endl;
system("pause");
return 0;
}
重载>>运算符: 使用友元函数
Boy.h
#pragma once
#include <string>
#include <iostream>
using namespace std;
class Boy{
public:
Boy(const string name = "无名", int age = 0, int salary = 0);
friend ostream& operator<<(ostream& os, const Boy& boy);
//使用友元函数 输入运算符重载
friend istream& operator>>(istream& is, Boy& boy);
private:
string name;//姓名
int age; //年龄
int salary; //薪资
};
Boy.cpp
#include <sstream>
#include "Boy.h"
Boy::Boy(const string name, int age, int salary){
this->name = name;
this->age = age;
this->salary = salary;
}
main.cpp
#include <iostream>
#include <Windows.h>
#include "Boy.h"
using namespace std;
ostream& operator<<(ostream& os, const Boy& boy) {
os << "姓名:" << boy.name << "t年龄:" << boy.age << "t薪资:" << boy.salary;
return os;
}
istream& operator>>(istream& is, Boy& boy) {
cout << "请输入姓名:";
is >> boy.name;
cout << "请输入年龄:";
is >> boy.age;
cout << "请输入薪资:";
is >> boy.salary;
return is;
}
int main(void) {
Boy boy1("张三", 18, 10000);
cout << boy1 << endl;
//调用输入重载运算符 friend istream& operator>>(istream& is, Boy& boy);
cin >> boy1; //把张三改为李四#include <iostream>
#include <Windows.h>
#include "Boy.h"
using namespace std;
ostream& operator<<(ostream& os, const Boy& boy) {
os << "姓名:" << boy.name << "t年龄:" << boy.age << "t薪资:" << boy.salary;
return os;
}
istream& operator>>(istream& is, Boy& boy) {
cout << "请输入姓名:";
is >> boy.name;
cout << "请输入年龄:";
is >> boy.age;
cout << "请输入薪资:";
is >> boy.salary;
return is;
}
int main(void) {
Boy boy1("张三", 18, 10000);
cout << boy1 << endl;
//调用输入重载运算符 friend istream& operator>>(istream& is, Boy& boy);
cin >> boy1;
cout << boy1 << endl;
system("pause");
return 0;
}
cout << boy1 << endl;
system("pause");
return 0;
}
最后
以上就是淡定音响为你收集整理的C++基础知识 - 重载<<和>>运算符的全部内容,希望文章能够帮你解决C++基础知识 - 重载<<和>>运算符所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复