[019]转--C++ operator关键字(重载操作符)
原博客:http://www.cnblogs.com/speedmancs/archive/2011/06/09/2076873.html
class person{
private:
int age;
public:
person(int a){
this->age=a;
}
inline bool operator == (const person &ps) const;
};
实现方式如下:
inline bool person::operator==(const person &ps) const
{
if (this->age==ps.age)
return true;
return false;
}
调用方式如下:
#include
using namespace std;
int main()
{
person p1();
person p2();
if(p1==p2) cout<<”the age is equal!”< return ;
}
这里,因为operator ==是class person的一个成员函数,所以对象p1,p2都可以调用该函数,上面的if语句中,相当于p1调用函数==,把p2作为该函数的一个参数传递给该函数,从而实现了两个对象的比较。
class person
{
public:
int age;
public:
}; bool operator==(person const &p1 ,person const & p2)
//满足要求,做操作数的类型被显示指定
{
if(p1.age==p2.age)
return true;
return false;
}
调用方式如下:
int main()
{
person rose;
person jack;
rose.age=;
jack.age=;
if(rose==jack)
cout<<"ok"< return ;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
#include <iostream> using namespace std; class A { public : A( double _data = 0.0):data(_data){} A& operator = ( const A& rhs) { data = rhs.data; return * this ; } friend A operator + ( const A& lhs, const A& rhs); friend A operator - ( const A& lhs, const A& rhs); friend A operator * ( const A& lhs, const A& rhs); friend A operator + ( const A& lhs, double rhs); friend A operator + ( double lhs, const A& rhs); friend A operator * ( const A& lhs, double rhs); friend A operator * ( double lhs, const A& rhs); friend A operator - ( const A& lhs, double rhs); friend A operator - ( double lhs, const A& rhs); friend ostream& operator << (ostream& fout,A& a); // A& operator += (const A& rhs); // A& operator -= (const A& rhs); // A& operator *= (const A& rhs); private : double data; }; A operator + ( const A& lhs, const A& rhs) { A res(0); res.data = lhs.data + rhs.data; return res; } A operator - ( const A& lhs, const A& rhs) { A res(0); res.data = lhs.data - rhs.data; return res; } A operator * ( const A& lhs, const A& rhs) { A res(0); res.data = lhs.data * rhs.data; return res; } A operator + ( const A& lhs, double rhs) { A res(0); res.data = lhs.data + rhs; return res; } A operator + ( double lhs, const A& rhs) { A res(0); res.data = lhs + rhs.data; return res; } A operator * ( const A& lhs, double rhs) { A res(0); res.data = lhs.data * rhs; return res; } A operator * ( double lhs, const A& rhs) { A res(0); res.data = lhs * rhs.data; return res; } A operator - ( const A& lhs, double rhs) { A res(0); res.data = lhs.data - rhs; return res; } A operator - ( double lhs, const A& rhs) { A res(0); res.data = lhs - rhs.data; return res; } ostream& operator << (ostream& fout,A& a) { fout << a.data ; return fout; } int main( int argc, char * argv[]) { A a(2.3); A b(1.2); A d(3.4); A c; c = a + b + d; c=a+b; c=a+1.0; c=a-b; c=a-1.0; c=a*b; c=a*1.0; cout << c << endl; c=1.0+2.0*a*a-3.0*a*b; cout << c << endl; return 0; } |
[019]转--C++ operator关键字(重载操作符)的更多相关文章
- C++ operator(重载操作符) 【转】
转自:http://www.cnblogs.com/xiangxiaodong/archive/2012/02/12/2348144.html operator是C++的关键字,它和运算符一起使用,表 ...
- C++ operator关键字(重载操作符)(转)
operator是C++的关键字,它和运算符一起使用,表示一个运算符函数,理解时应将operator=整体上视为一个函数名. 这是C++扩展运算符功能的方法,虽然样子古怪,但也可以理解:一方面要使运算 ...
- C++中operator关键字(重载操作符)
operator是C++的关键字,它和运算符一起使用,表示一个运算符函数,理解时应将operator=整体上视为一个函数名. 这是C++扩展运算符功能的方法,虽然样子古怪,但也可以理解:一方面要使运算 ...
- C++ operator关键字(重载操作符)
operator是C++的关键字,它和运算符一起使用,表示一个运算符函数,理解时应将operator=整体上视为一个函数名. 这是C++扩展运算符功能的方法,虽然样子古怪,但也可以理解:一方面 ...
- C++ 友元(friend关键字)、类中的重载、操作符重载(operator关键字)
C++ 中友元的用法: 1.在类中使用friend关键字声明 2.类的友元可以是其它类或者具体函数 3.友元不是类的一部分 4.友元不受类中访问级别的限制 5.友元可以直接访问具体类中的所有成员. 友 ...
- 重载操作符 operator overloading 学习笔记
重载操作符,只是另外一种调用函数的方法和表现方式,在某些情况它可以让代码更简单易读.注意不要过度使用重载操作符,除非它让你的类更简单,让你的代码更易读. 1语法 如下: 其中友元,关键字不是必须的,但 ...
- C++的重载操作符(operator)介绍(转)
本文主要介绍C++中的重载操作符(operator)的相关知识. 1. 概述 1.1 what operator 是C++的一个关键字,它和运算符(如=)一起使用,表示一个运算符重载函数,在理解时可将 ...
- C++重载操作符operator
operator是C++关键字,用于对C++进行扩展: 1.可以被重载的操作符:new,new[],delete,delete[],+,-,*,/,%,^,&,|,~,!,=,<,> ...
- C++ 学习六 operator关键字(重载)
转载:http://blog.sina.com.cn/s/blog_4b3c1f950100kker.html operator是C++的关键字,它和运算符一起使用,表示一个运算符函数,理解时应将 o ...
随机推荐
- C# 判断两张图片是否一致的快速方法
#region 判断图片是否一致 /// <summary> /// 判断图片是否一致 /// </summary> /// <param name="img& ...
- 我的电脑在用Microsoft Script Editor 调试,关不了?
是不是上网后经常出现错误提示框,那在浏览器的工具选“internet选项”进入“高级”在“禁用脚本调试(internet explor)”和“禁用脚本调试”前勾上.Microsoft Script E ...
- Gradle使用手册(一):为什么要用Gradle?
原文地址:http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Using-sourceCompatibility-1. ...
- 记录一次Jmeter性能测试
一.引言 之前有总结过如何写Java请求测试用例类,写完测试脚本调通之后,信心满满地以为我准备好可以开始性能测试了.结果在评审测试计划的时候,当即被项目组狠狠的扇了一耳光,各种不确定的点:性能指标不明 ...
- 【转载】linux命令行计算器bc的一个“坑”
[转载自]http://blog.chinaunix.net/uid-174325-id-3518953.html 结论:ibase,obase可以使用在不同的计算公式里,但是尽量把obase放iba ...
- Tomcat普通用户部署教程(生产服务器)
1.环境准备 JDK安装 解压 tar xf tomcat-xx.tar.gz -C /data/soft cd /data/soft 重命名 mv tomcat-xx tom ...
- SGU-495 Kids and Prizes 概率DP
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=495 题意:有n个盒子,每个盒子里面放了一个奖品,m个人轮流去选择盒子,如果盒子里面 ...
- [Tutorial] Using the RasPi as a WiFi hostspot
http://www.raspberrypi.org/forums/viewtopic.php?f=36&t=19120 http://wireless.kernel.org/en/users ...
- 如何为可扩展系统进行Java Socket编程
从简单I/O到异步非阻塞channel的Java Socket模型演变之旅 上世纪九十年代后期,我在一家在线视频游戏工资工作,在哪里我主要的工作就是编写Unix Unix Berkley Socket ...
- Lodop Web打印插件使用
<object id="LODOP_OB" classid="clsid:2105C259-1E0C-4534-8141-A753534CB4CA" st ...