c++重载operator的示例 非原创
#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++重载operator的示例 非原创的更多相关文章
- C++重载operator的示例
#include<iostream>#include<vector>using namespace std; class test{public: int v; / ...
- 老男孩Django笔记(非原创)
.WEB框架 MVC Model View Controller 数据库 模板文件 业务处理 MTV Model Template View 数据库 模板文件 业务处理 ############## ...
- Linux下high CPU分析心得【非原创】
非原创,搬运至此以作笔记, 原地址:http://www.cnitblog.com/houcy/archive/2012/11/28/86801.html 1.用top命令查看哪个进程占用CPU高ga ...
- 重载operator new实现检测内存泄漏是否可行
行与不行,就凭我这水平,说出来未免显示太过自大.不还,我还想根据自己的代码来讨论这个问题. 重载operator new来检测内存只的办法,那就是在new的时候记录指针地址及文件名.行号,在delet ...
- CSS样式命名整理(非原创)
非原创,具体出自哪里忘了,如果侵害您的利益,请联系我. CSS样式命名整理 页面结构 容器: container/wrap 整体宽度:wrapper 页头:header 内容:content 页面主体 ...
- 非原创。使用ajax加载控件
非原创.来自博客园老赵. public class ViewManager<T> where T : System.Web.UI.UserControl { private System. ...
- Java 表达式解析(非原创)
因项目需要,在网上找来一套表达式解析方法,由于原来的方法太过于零散,不利于移植,现在整理在同一文件内: 文件中包含5个内部类,源码如下: import java.util.ArrayList; imp ...
- Java Interface 是常量存放的最佳地点吗?(转帖学习,非原创)
Java Interface 是常量存放的最佳地点吗?(转帖学习,非原创) 由于java interface中声明的字段在编译时会自动加上static final的修饰符,即声明为常量.因而inter ...
- 用RD,GR,BL三个方法内代码生成一张图片(非原创,我只是完整了代码)
我公开以下图片的源代码,,是ppm格式的,,自己找到能打开的工具.. (非原创,我加工的代码,可直接执行运行输出,缩略图能看到效果) 这是原博客 http://news.cnblogs.com/n/ ...
随机推荐
- Luogu 4238 【模板】多项式求逆
疯狂补板中. 考虑倍增实现. 假设多项式只有一个常数项,直接对它逆元就可以了. 现在假如要求$G(x)$ $$F(x)G(x) \equiv 1 (\mod x^n)$$ 而我们已经求出了$H(x)$ ...
- code1006 等差数列
我绞尽脑汁想一个更好的算法,然而不能如愿,只好写一个n^3的了 很简单,就是暴力搜索(还好n<100) 先排序,然后循环i=1ton,j=i+1ton 把a[i]a[j]确定为等差数列开始的两个 ...
- vscode安装设置go
vscode安装设置go vscode安装go配置 1.下载最新的vscode: https://code.visualstudio.com/docs/?dv=win 2.下载go: https:// ...
- nhibernate 3.x新特性
http://www.360doc.com/content/14/0226/17/9316347_355904008.shtml
- python report中文显示乱码
环境:python2.7 测试框架: nose (1.3.7) nose-html-reporting (0.2.3) 问题:生成测试报告失败的时候,报告会抓取代码中的print,打开后看到的中文是乱 ...
- [GO]匿名字段的同名字段操作
package main import ( "fmt" ) type Person struct { name string sex byte age int } type Stu ...
- javascript总结24:Array常用的队列操作和排序方法
1 数组-引用类型 JavaScript中的内置对象 复习数组的使用 两种创建数组的方式 Array对象的属性 length 获取数组的长度(元素个数) 2 常用方法 : 检测数组 instanceo ...
- Android canvas bug
安卓4.1.1-4.1.2的webkit在渲染canvas元素时有bug. 具体表现是出现重影,即canvas的clearRect()方法不能彻底清空画布,仍然保留之前某个状态当“背景”. 目前的修复 ...
- 编写高质量代码改善C#程序的157个建议——建议64:为循环增加Tester-Doer模式而不是将try-catch置于循环内
建议64:为循环增加Tester-Doer模式而不是将try-catch置于循环内 如果需要在循环中引发异常,你需要特别注意,应为抛出异常是一个相当影响性能的过程.应该尽量在循环当中对异常发生的一些条 ...
- Activity Fragment转场动画
Activity转场动画 先介绍个动画的好例子:https://github.com/lgvalle/Material-Animations Activity的转场动画是通过overridePendi ...