[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 ...
随机推荐
- Azure 中的多个 VM NIC 和网络虚拟设备
YU-SHUN WANG Azure 网络高级项目经理 在 2014 年欧洲 TechEd 大会上,我们宣布了在Azure VM 中为多个网络接口 (NIC) 提供支持,并与多家重要厂商合作,在 Az ...
- Android 自动换行流式布局的RadioGroup
效果图 用法 使用FlowRadioGroup代替RadioGroup 代码 import android.content.Context; import android.util.Attribute ...
- 【转】【iOS】导航栏那些事儿
原文网址:http://www.jianshu.com/p/f797793d683f 参考文章 navigationItem UINavigationItem UINavigationBar UIBa ...
- 【转】Android项目中编译 C的模块
原文网址:http://blog.csdn.net/Harrison_zhu/article/details/4057738 Android编译环境本身比较复杂,且不像普通的编译环境:只有顶层目录下才 ...
- 【转】使用NDK生成native C/C++的可执行程序
原文网址:http://www.linuxidc.com/Linux/2011-08/40901.htm 众所周知, NDK可以生成lib,让java程序通过jni来调用,其实,NDK也可以生成C/C ...
- Project Euler 26 Reciprocal cycles
题意:求1到n中所有数的倒数中循环小数循环体最长的数 解法:如果一个数的质因子只有2和5,那么这个数的倒数一定不是一个循环小数.如果一个数含有质因子2或5,那么它的循环体和去掉质因子2和5之后的数的循 ...
- 【转】经典SQL语句大全
原博客地址:http://www.cnblogs.com/yubinfeng/archive/2010/11/02/1867386.html (超级纳闷为啥没有转载的功能) 一.基础 1.说明:创建数 ...
- [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.5.9
(Schur's Theorem) If $A$ is positive, then $$\bex \per(A)\geq \det A. \eex$$ Solution. By Exercise I ...
- e2e 自动化集成测试 架构 实例 WebStorm Node.js Mocha WebDriverIO Selenium Step by step (三) SqlServer数据库的访问
上一篇文章“e2e 自动化集成测试 架构 京东 商品搜索 实例 WebStorm Node.js Mocha WebDriverIO Selenium Step by step 二 图片验证码的识别” ...
- CCF 认证
题意:字符串替换 string+map的应用 #include<iostream> #include<stdio.h> #include<stdlib.h> #in ...