*/
* Copyright (c) 2016,烟台大学计算机与控制工程学院
* All rights reserved.
* 文件名:text.cpp
* 作者:常轩
* 微信公众号:Worldhello
* 完成日期:2016年5月14日
* 版本号:V1.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;
}

最新文章

  1. Beginning Scala study note(3) Object Orientation in Scala
  2. ECharts分析xdebug跟踪日志
  3. HTTP基础08--追加协议
  4. HTML5文档结构语义:页眉的header和hgroup标签使用
  5. oracle视图总结(转)
  6. typedef与define基本使用
  7. web.xml中的contextConfigLocation的作用
  8. Mayor&#39;s posters(离散化线段树)
  9. python异常处理try,except,else,finally,raise
  10. 【iOS】随机三角瓷砖布局算法
  11. 深入Callable及Runnable两个接口 获取线程返回结果
  12. SSE推送数据
  13. centos7下kubernetes(15。kubernetes-外网访问service)
  14. dataTable 分页用法总结
  15. cookie详解(含vue-cookie)
  16. 如何制作 Objective-C 的UML图 [2]
  17. 2016.6.21——Add Binary
  18. laravel 模版引擎使用
  19. Nginx + uWSGI 配置django---终极版
  20. Every write operation executed on a document, deletes included

热门文章

  1. Macroevolution|Silent changes|CNEs|Transposable elements|Neutral sites
  2. ubuntu linux下解决“no java virtual machine was found after searching the following locations:”的方法
  3. Java注解基础
  4. poj-3658 Artificial Lake(模拟)
  5. 42)PHP,mysqli函数功能总结
  6. Elasticsearch-URL查询实例解析
  7. rsync备份(一)
  8. Windows电脑忘记密码
  9. 作业:for循环,迭代法和穷举法
  10. [Python] 使用Python 3 下载麦子学院视频