[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 ...
随机推荐
- SPOJ 962 Intergalactic Map (从A到B再到C的路线)
[题意]在一个无向图中,一个人要从A点赶往B点,之后再赶往C点,且要求中途不能多次经过同一个点.问是否存在这样的路线.(3 <= N <= 30011, 1 <= M <= 5 ...
- apache开源项目--TIKA
Tika是一个内容抽取的工具集合(a toolkit for text extracting).它集成了POI, Pdfbox 并且为文本抽取工作提供了一个统一的界面.其次,Tika也提供了便利的扩展 ...
- jquery自动生成分页控件 - pagetest.js
/* pagenum:当前页数 theallnum:总条数 themeiye:每页显示多少条 */ function pagetest(pagenum, theallnum, themeiye) { ...
- (五)学习JavaScript之firstChild 属性
参考:http://www.w3school.com.cn/jsref/prop_node_firstchild.asp 定义和用法 firstChild 属性返回指定节点的首个子节点,以 Node ...
- Hibernate4.x之映射关系--双向1-n
双向1-n与双向n-1是完全相同的两种情形 双向1-n需要在1的一端可以访问n的一端,反之亦然. 域模型:从Order到Customer的多对一双向关联需要在Order类中定义一个Customer属性 ...
- SharePoint 2010 自定义 字段 类型--------省市区联动
转:http://www.cnblogs.com/sp007/p/3384310.html 最近有几个朋友问到了有关自定义字段类型的问题,为了让更多的人了解自定义字段类型的方法,特写一篇博客与大家分享 ...
- 如何使用spring中的Log4jConfigListener--删除
使用spring中的Log4jConfigListener有如如下好处: 1. 动态的改变记录级别和策略,不需要重启Web应用,如<Effective Enterprise Java> ...
- 28、activity之间传递数据&批量传递数据
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android ...
- Android视图SurfaceView的实现原理分析
http://blog.csdn.net/luoshengyang/article/details/8661317
- lightoj 1014
判断到根号n即可,另外使用dfs输出,不需要另开数组再排序. #include<cmath> #include<cstdio> int P, L, len, cnt; void ...