第8周项目1-程序阅读——运算符重载
来源:互联网 发布:淘宝商家不提供发票 编辑:程序博客网 时间:2024/06/05 19:08
#include <iostream>using namespace std;class Sample{private: int x;public: Sample () { } Sample(int a){x=a;} void disp(){cout<<"x="<<x<<endl;} friend Sample operator+( Sample &s1, Sample &s2); //友元函数};Sample operator+( Sample &s1, Sample &s2) //运算符重载{ return Sample(s1.x+s2.x);}int main(){ Sample obj1(10); Sample obj2(20); Sample obj3; obj3=obj1+obj2; obj3.disp(); return 0;}
#include <iostream>using namespace std;class Sample{private: int x;public: Sample() {} Sample (int a){x=a;} void disp(){cout<<"x="<<x<<endl;} Sample operator+(Sample &s);};Sample Sample:: operator+( Sample &s) //为成员函数{ return Sample(x+s.x);}int main(){ Sample obj1(20); Sample obj2(20); Sample obj3; obj3=obj1+obj2; obj3.disp(); return 0;}
#include<iostream>using namespace std;class Wages//“工资”类{ double base;//基本工资 double bonus;//奖金 double tax;//税金public: Wages(double CBase, double CBonus,double CTax): base(CBase), bonus(CBonus),tax(CTax) {} double getPay()const;//返回应付工资额 Wages operator+(Wages w)const;//重载加法};double Wages::getPay()const{ return base+bonus-tax;}Wages Wages::operator+(Wages w)const{ return Wages(base+w.base, bonus+w.bonus,tax+w.tax);}int main(){ Wages wl(2000,500,100),w2(5000,1000,300); cout<<(wl+w2).getPay()<<endl; return 0;}比较大小
#include<iostream>using namespace std;class Pair{ int m; int n;public: Pair(int i, int j):m(i),n(j) {} bool operator >(Pair p) const;};bool Pair::operator>(Pair p)const{ if (m!=p.m) return m>p.m; return n>p.n;}int main(){ Pair p1(3,4),p2(4,3), p3(4,5); cout<<(p1>p2)<<(p2>p1)<<(p2>p3)<<(p3>p2); return 0;}输出:0101
0 0
- 第8周项目1-程序阅读——运算符重载
- 第7周-项目0-阅读程序-运算符重载
- 第八周——阅读程序-运算符重载
- 第八周项目零(1):阅读程序:运算符重载
- 第八周 程序阅读-运算符重载
- 第十二周 运算符重载阅读程序
- 第8周项目1运算符重载
- 2013级C++第8周(春)项目——运算符重载 【项目1】实现复数类中的运算符重载
- 第8周项目 程序阅读(1)
- 2013级C++第8周(春)项目——运算符重载
- 第12周项目1—实现复数类的运算符重载——3
- 第9周项目1——复数类中的运算符重载(续)
- 第12周项目1——实现复数类的运算符重载
- 第12周项目1—实现复数类的运算符重载 (1)
- 第13周项目1—分数类中的运算符重载 (1)
- 第12周项目1—实现复数类中的运算符重载 (2)
- 第12周项目1—实现复数类中的运算符重载 (3)
- 第13周项目1—分数类中的运算符重载
- iOS图片AVMakeRectWithAspectRatioInsideRect妙用
- 程序员职业生涯的11个阶段程序人生
- 函数指针与实参推断
- Last_Errno: 1062,Last_Error: Error 'Duplicate entry '...' for key 'PRIMARY'' on query. 的详细分析过程
- UVA 12304 2D Geometry 110 in 1!(计算几何)
- 第8周项目1-程序阅读——运算符重载
- postgreSQL添加多边形
- 模仿qq列表的ActiveX控件
- 第三次作业:《国际贸易学》—保护贸易理论
- c++读取文件的最后一行
- ant手动编译apk,其中包括第三方Library和so文件
- 网络大数据分析 -- 使用 ElasticSearch + LogStash + Kibana 来可视化网络流量
- ajax中get和post的区别
- ToolRunner运行Hadoop原理分析