#include<iostream>
#include<vector>
using namespace std;

class test{
public:
     int v;
   /*构造函数*/
     test():v(0){}
     test(const int &a):v(a){}
     test(const test &t1):v(t1.v){} 
    
   /*以下重载小于号 < */
     //比较两个对象的大小 
     bool operator<(const test &t1) const{ 
         return (v < t1.v);
     }
     //比较对象和int的大小 
     bool operator<(const int &t1) const{ 
         return (v < t1);
     }
     //友元函数,比较int和对象的大小 
     friend inline bool operator<(const int &a, const test & t1){
         return (a < t1.v);
     }
    
   /*以下重载赋值号 = */
     //对象间赋值 
     test & operator=(const test &t1){
         v = t1.v;
         return *this;
     }
     //int赋值给对象 
     test & operator=(const int &t1){
         v = t1;
         return *this;
     }
    
   /*以下重载加号 + */
     //对象加上 int 
     test operator+(const int & a){
         test t1;
         t1.v = v + a;
         return t1;
     }
     //对象加对象 
     test operator+(test &t1){    //注意这里不用&,而下面+=则要用&
         test t2;
         t2.v = v + t1.v;
         return t2;
     }
    
   /*以下重载加等号 += */  
     //对象加上对象 
     test &operator+=(const test &t1){
         v += t1.v;
         return *this;
     }  
     //对象加上int
     test &operator+=(const int &a){
         v += a;
         return *this;
     }

/*以下重载双等号 == */  
     //对象==对象 
     bool operator==(const test &t1)const{
         return (v == t1.v);
     }  
     //对象==int
     bool operator==(const int &t1)const{
         return (v == t1);
     }  
    
   /*以下重载 输入>> 输出<< */
     /*友元函数,输出对象*/
     friend inline ostream & operator << (ostream & os, test &t1){
         cout << "class t(" << t1.v << ")" << endl;
         return os;
     }
     /*友元函数,输入对象*/
     friend inline istream & operator >> (istream & is, test &t1){
         cin >> t1.v;
         return is;
     }
};

int main(){
     test t0, t1(3);
     test t2(t1);
     cout << t0 << t1 << t2;
     cin >> t1;
     t2 = t1;
     t2 += t1;
     t1 += 10;
     cout << t2;
     if(t1 < t2) cout << "t1 < t2"; 
     else if(t1 == t2) cout << "t1 = t2";
     else /* t1 > t2*/ cout << "t1 > t2"; 
     cout <<endl;
     system("pause");
     return 0;
}

最新文章

  1. CocoaPods的安装、使用、以及遇到的问题
  2. Spark笔记:RDD基本操作(上)
  3. 用NPOI从DataBase到Excel
  4. 转贴 IT外企那点儿事完整版
  5. 一个通用的makefile
  6. 【python-mysql】在ubuntu下安装python-mysql环境
  7. 自己遇到的Android虚拟机出现的错误及解决方法【不断更新】
  8. 【转】android布局属性详解
  9. list用法详解
  10. 通过web代理进行跨域访问,http请求返回超时的问题定位
  11. Android NDK 环境搭建 + 测试例程
  12. JavaWeb文件的上传与下载(1)
  13. Two 观察者 observer pattern
  14. python实现四则运算和效能分析
  15. webpack,配置,上手,例子
  16. appium的log详细分析
  17. odoo开发笔记--前端搜索视图--按照时间条件筛选
  18. GDI+ DrawString字间距问题
  19. 【JavaScript】使用setInterval()函数作简单的轮询操作
  20. SpringMVC 学习笔记(处理器映射器的配置)

热门文章

  1. HTML的格式、内容容器、表格标签
  2. SecureCRT上传和下载文件(下载默认目录)
  3. ThinkPHP3.2 volist嵌套循环显示原理
  4. 廖雪峰js教程笔记5 Arrow Function(箭头函数)
  5. Smart原则
  6. js获取一个对象的所以属性和值
  7. vector初始化
  8. 下载安全程序需谨慎 黑客盯上XP用户
  9. ASP.NET MVC中使用highcharts 生成简单的折线图
  10. Drawing Arc Using ArcSegment in XAML