C++运算符重载实例
来源:互联网 发布:spark sql与hive的区别 编辑:程序博客网 时间:2024/06/10 04:28
以下示例中定义了一个class test, 重载了<, +, +=, =, ==, <<, >>等符号:
#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;
}
- 运算符重载实例
- 运算符重载实例
- 运算符重载实例
- C++(笔记)浅析运算符重载&实例
- C++-运算符重载
- [C++]重载运算符
- C#:运算符重载
- c++-++运算符重载
- C++:重载运算符
- 运算符重载(C++)
- [c++]运算符重载
- 【C++】运算符重载
- C++--------------------------------------------运算符重载
- C#:运算符重载
- C++:运算符重载
- C++:运算符重载
- C#:运算符重载
- [C++]运算符重载
- excel快捷键
- 什么是小灵通PHS
- Project Euler - Problem 20
- MATLAB7.0程序打开后一会就自动关闭的解决方法
- VC6.0访问Access笔记
- C++运算符重载实例
- xp底下成功使用ssh登录ubuntu linux(采用密钥方式)
- 几个经典笑话
- cxf2.1.3 client invoke xfire 1.2.6 server
- Joomla menu
- VB - String.Format(3--->0003)
- Responses to "error: unable to resolve ' ' for transcoding的解决办法
- 今日插件的简单使用(转自ciahi的专栏)
- 2.LifeRay 的哪些东西该注意?