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