欢迎来到天天文库
浏览记录
ID:37916740
大小:60.50 KB
页数:7页
时间:2019-06-02
《C++基础之运算符重载》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、运算符重载输入输出:#includeusingnamespacestd;classEmp{stringname;doublesalary;public:Emp(stringname="",doublesalary=0):name(name),salary(salary){}voidshow()const{cout<2、m&operator>>(istream&is,Emp&e){//也可以直接写进类中returnis>>e.name>>e.salary;}};//输出运算符函数ostream&operator<<(ostream&o,constEmp&e){o<3、intx=10;//cout.operator<<(int);cout<>(e2),operator>>(cin,e2)cin>>e2;cout<usingnamespacestd;classPerson{stringname;intage;public:4、Person(stringname="",intage=0):name(name),age(age){}Person&operator++(){//前加加(成员函数)age++;return*this;}/*friendPerson&operator++(Person&p){//前加加(有元函数)p.age++;returnp;}*/constPersonoperator++(int){//后加加(成员函数)Personold=*this;age++;returnold;}/*friendconstPersonoperator++(Person&p,int5、){//后加加(友元函数)Personold=p;p.age++;returnold;}*/friendostream&operator<<(ostream&o,constPerson&p){returno<usingnamespacestd;classInteger{int6、x;public:Integer(intx=0):x(x){}friendostream&operator<<(ostream&o,constInteger&i){returno<>(istream&is,Integer&i){returnis>>i.x;}/*constIntegeroperator+(constInteger&i){returnx+i.x;}*/friendconstIntegeroperator+(constInteger&a,constInteger&b){returnInt7、eger(a.x+b.x);}constIntegeroperator-(constInteger&i)const{returnx-i.x;}friendbooloperator<(constInteger&a,constInteger&b){returna.x8、iostream>usingnamespacestd;classD
2、m&operator>>(istream&is,Emp&e){//也可以直接写进类中returnis>>e.name>>e.salary;}};//输出运算符函数ostream&operator<<(ostream&o,constEmp&e){o<3、intx=10;//cout.operator<<(int);cout<>(e2),operator>>(cin,e2)cin>>e2;cout<usingnamespacestd;classPerson{stringname;intage;public:4、Person(stringname="",intage=0):name(name),age(age){}Person&operator++(){//前加加(成员函数)age++;return*this;}/*friendPerson&operator++(Person&p){//前加加(有元函数)p.age++;returnp;}*/constPersonoperator++(int){//后加加(成员函数)Personold=*this;age++;returnold;}/*friendconstPersonoperator++(Person&p,int5、){//后加加(友元函数)Personold=p;p.age++;returnold;}*/friendostream&operator<<(ostream&o,constPerson&p){returno<usingnamespacestd;classInteger{int
3、intx=10;//cout.operator<<(int);cout<>(e2),operator>>(cin,e2)cin>>e2;cout<usingnamespacestd;classPerson{stringname;intage;public:
4、Person(stringname="",intage=0):name(name),age(age){}Person&operator++(){//前加加(成员函数)age++;return*this;}/*friendPerson&operator++(Person&p){//前加加(有元函数)p.age++;returnp;}*/constPersonoperator++(int){//后加加(成员函数)Personold=*this;age++;returnold;}/*friendconstPersonoperator++(Person&p,int
5、){//后加加(友元函数)Personold=p;p.age++;returnold;}*/friendostream&operator<<(ostream&o,constPerson&p){returno<
6、x;public:Integer(intx=0):x(x){}friendostream&operator<<(ostream&o,constInteger&i){returno<>(istream&is,Integer&i){returnis>>i.x;}/*constIntegeroperator+(constInteger&i){returnx+i.x;}*/friendconstIntegeroperator+(constInteger&a,constInteger&b){returnInt
7、eger(a.x+b.x);}constIntegeroperator-(constInteger&i)const{returnx-i.x;}friendbooloperator<(constInteger&a,constInteger&b){returna.x8、iostream>usingnamespacestd;classD
8、iostream>usingnamespacestd;classD
此文档下载收益归作者所有