C++的大多数运算符都可以通过operator来实现重载。

简单的operator+

#include <iostream>
using namespace std; class A
{
public:
A(int x){i=x;}
int i;
A &operator+(A &p)
{
this->i+=p.i;
return *this;
}
}; void main()
{
A a(),b(),c();
c=a+b;
cout<<c.i<<endl;
system("pause");
}

简单的operator++

#include <iostream>
using namespace std; class A
{
public:
A(int x){i=x;}
int i;
A &operator++()
{
this->i++;
return *this;
}
}; void main()
{
A a();
a++;
cout<<a.i<<endl;
system("pause");
}

深层operator=

#include <iostream>
using namespace std; class A
{
public:
A(int x){i=new int;*i=x;}
~A(){delete i;i=;}
A(const A&p)
{
i=new int;
*i=*(p.i);
}
int *i;
A &operator=(A &p)
{
*i=*(p.i);
return *this;
}
int pp(){return *i;}
}; void main()
{
A a(),b();
a=b;
cout<<a.pp()<<endl;
system("pause");
}

简单的输出运算符operator<<

注意:由于ostream类没有公有的复制构造函数,因此函数无法调用该类的复制构造函数来复制对象,必须按照引用的方式来接收与返回ostream对象。

#include <iostream>
using namespace std; class A
{
public:
A(int x,int y){rx=x;ry=y;}
int rx;
int ry;
}; ostream &operator << (ostream &s,const A &c)//cout是ostream的对象
{
s<<c.rx;//使用cout输出对象的值,语句可以直接翻译成cout<<c.rx
s<<c.ry;//使用cout输出对象的值,语句可以直接翻译成cout<<c.ry
return s;//返回cout
} void main()
{
A a(,),b(,);
cout<<a<<b;//(ostream &s,const A &c)==(cout,a)
system("pause");
}

简单的输出运算符-使用友元的方式operator<<

#include <iostream>
using namespace std; class A
{
public:
A(int x,int y){rx=x;ry=y;}
friend ostream &operator << (ostream &s,const A &c)
{
s<<c.rx;
s<<c.ry;
return s;
}
private:
int rx;
int ry;
}; void main()
{
A a(,),b(,);
cout<<a<<b;
system("pause");
}

operator++(++i与i++的实现)

#include <iostream>
using namespace std; class A
{
public:
A(int x){rx=x;}
int operator++() //++i
{
this->rx++; //其实这里this指针可以不用写因为你直接写rx++效果也是一样的,编译器会在编译的时候自动的为你加上this
return rx; //但是刚开始学语言的时候建议写一下可以加深this指针的概念
}
int operator++(int) //i++
{
int i=;
i=this->rx;
this->rx++;
return i;
}
friend ostream &operator << (ostream &s,const A &c)
{
s<<c.rx;
return s;
}
private:
int rx;
}; void main()
{
A a(),b();
cout<<a++<<endl;
cout<<++b<<endl;
system("pause");
}

operator输入输出,自增运算符

#include <iostream>
using namespace std; class A
{
public:
A(int x){rx=x;}
int operator++() //++i
{
this->rx++; //其实这里this指针可以不用写因为你直接写rx++效果也是一样的,编译器会在编译的时候自动的为你加上this
return rx; //但是刚开始学语言的时候建议写一下可以加深this指针的概念
}
int operator++(int) //i++
{
int i=;
i=this->rx;
this->rx++;
return i;
}
friend istream &operator >> (istream &s,A &c) //输入操作符重载
{
s>>c.rx;
return s;
}
friend ostream &operator << (ostream &s,const A &c) //输出操作符重载
{
s<<c.rx;
return s;
}
private:
int rx;
}; void main()
{
A a(),b();
cin>>a;
cin>>b;
cout<<a++<<endl;
cout<<++b<<endl;
system("pause");
}

最新文章

  1. jQuery Mobile 工具栏
  2. GJM : Unity3D - NetWork - Hight Level API ( HLAPI) [转载]
  3. 【学】CSS3的3D动画 ——3D旋转之骰子样式的钟表(2)上
  4. zw版【转发&#183;台湾nvp系列Delphi例程】HALCON Component Histogram
  5. jsp java 数据库 乱码总结
  6. Android 静默安装/后台安装
  7. ios 异步多线程 获取数据
  8. Struts2之—实现自己的结果集的定义ajax
  9. webAppbuilder微件使用教程2 常用微件介绍
  10. 【转】JDBC学习笔记(4)——PreparedStatement的使用
  11. c/c++拷贝构造函数和关键字explicit
  12. day 74 json 和 ajax 的实例
  13. 数据库SQL语言学习--上机练习3(插入 更新 删除)
  14. Linux下动态链接库加载路径
  15. SVN中检出 和 导出 的区别
  16. Beta阶段——Scrum 冲刺博客第三天
  17. linux下centos7中mysql崩溃问题的解决
  18. 简单的图形学(三)&mdash;&mdash;光源
  19. 关于vue.js父子组件数据传递
  20. Java package

热门文章

  1. 有趣 GIF 动图集 - 仿佛每张小动图都诉说了一个小笑话或者小故事
  2. Activity学习(四)——简单切换
  3. java集合之ArrayList的实现原理
  4. POJ 1027 The Same Game(模拟)
  5. node操作mysql数据库
  6. js对象小结
  7. Spring中RedirectAttributes对象重定向传参
  8. Jackson学习笔记-对象序列化
  9. JavaScript事件冒泡和事件委托
  10. JavaWeb笔记——三大组件之监听器