操作符重载
来源:互联网 发布:gov.cn域名代注册 编辑:程序博客网 时间:2024/06/06 09:23
操作符重载
#include <iostream>
#include <string>
using namespace std;
//定义水果类
class Fruit
{
string name;
string colour;
public:
friend istream& operator>>(istream&,Fruit&); //输入流
friend ostream& operator<<(ostream& out,const Fruit& fruit); //输出流
void print()
{
cout<<colour<<" "<<name<<endl;
}
Fruit(const string nst="apple",const string cst="green"):name(nst),colour(cst)
{
}
~Fruit()
{
}
};
//重载操作符
ostream& operator<<(ostream& out, const Fruit &s)
{
out<<s.colour<<" "<<s.name<<endl;
return out;
}
istream& operator>>(istream& in,Fruit &s)
{
in>>s.colour>>s.name;
if (!in)
{
cerr<<"Wrong input!"<<endl;
}
return in;
}
主程序:
Fruit apple;
cin>>apple;
cout<<apple;
- c++重载操作符
- C++ 操作符重载
- 操作符重载浅析
- 操作符重载
- 10.8 操作符重载
- 操作符重载
- 操作符重载手册
- C#操作符重载
- 操作符重载手册
- 操作符重载手册
- 六操作符重载
- 操作符的重载
- C#操作符重载
- 多态,操作符重载
- 操作符重载集锦
- 重载操作符
- 操作符重载
- 操作符重载手册
- Pro Android学习笔记(八):了解Content Provider(下中)
- strcore.cpp内存泄露问题
- Eclipse设置、调优、使用
- 如何查询mysql表结构
- LightSwitch学习--(一)lsml文件
- 操作符重载
- java mongo MapReduce
- 开发一个简易的任务监控程序
- 分割命令: split
- awk命令使用
- VMware vSphere 服务器虚拟化之十七 桌面虚拟化之安装View链接服务器
- mkdir递归创建目录
- 李代数(第2版)
- static_cast