[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 ...
随机推荐
- 基于Struts2的用户登录程序
基本步骤: 1.新建Java工程,File>New>Project>Web>Dynamic Web Project,并将工程命名为:Struts2_Demo 2.导入strut ...
- 【转】 C++中如何在一个构造函数中调用另一个构造函数
在C++中,一个类的构造函数没法直接调用另一个构造函数,比如: #ifndef _A_H_ #define _A_H_ #include <stdio.h> #include <ne ...
- 【转】 实现 Cocos2d-x 全局定时器
转自:http://www.tairan.com/archives/3998 cocos2d-x 中有自己的定时器实现,一般用法是在场景,层等内部实现,定时器的生命周期随着它们的消亡而消亡,就运行周期 ...
- 在win2008中配置ServU
因为08的防火墙要求比较高.很多端口都关闭,所以要设置防火墙. 首先设置入站规则 1.新建一条规则,规则类型选择“端口”,然后TCP,设置为20-21,60010-60020,然后允许链接,在配置文件 ...
- POJ 2435Navigating the City(bfs)
题意:给你一个地图,’+’代表十字路口,‘-’‘|’表示街道,‘.’表示建筑物,‘s’,’E’ 起点和终点.输出从起点到终点的的 最短路径(包括方向和沿该方向的经过的十字路口数) 分析:ans[i][ ...
- Python 连接mysql
下面我们使用MySQLdb 实现连接mysql 数据库并进行操作. #!/usr/bin/env python # -*-coding:UTF-8-*- import MySQLdb def conn ...
- .NET MVC4 ApiController拦截器的使用
目前在公司新开发了一个项目,第一次正式使用.NET MVC4来搭建,用拦截器来处理权限验证. 自定义拦截器需继承ActionFilterAttribute类,重写OnActionExecuting和O ...
- 使用jdbc连接上oracle的两种方法
1. 使用thin连接 优点:thin驱动都是纯Java代码,并且使用TCP/IP技术通过java的Socket连接上Oracle数据库,所以thin驱动是与平台无关的,无需安装Oracle客户端,只 ...
- 查看linux中的TCP连接数【转】
转自:http://blog.csdn.net/he_jian1/article/details/40787269 查看linux中的TCP连接数 本文章已收录于: 计算机网络知识库 分类: ...
- JS单词形式的运算符
1.void 运算表达式并忽略其返回值,比如void (1+2),void (0) <html> <head> <meta http-equiv="conten ...