C++ Code 
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
 
/*
    名称:C++运算符重载
    作者:Michael Joessy
    日期:2017-06-05
    知识:给原有的运算符赋予新的功能
    C++的一大特性就是重载(overload),通过重载可以把功能相似的几个函数合为一个,使得程序更加简洁、高效。
    在C++中不仅函数可以重载,而且运算符也可以重载。
    运算符重载的本质:函数重载
    关键字:operator
    分类:成员函数重载   友元函数重载
*/

#include <iostream>
#include <string>
using namespace std;

// class Coordinate
class Coordinate
{
    //friend Coordinate& operator-(Coordinate& coor);
    //friend Coordinate operator+(const Coordinate& coor1, const Coordinate& coor2);
    friend ostream& operator<<(ostream& out, const Coordinate& coor);

public:
    Coordinate(int nX, int nY);
    int getX();
    int getY();
    Coordinate& operator-();
    Coordinate& operator++();   //前置++
    Coordinate operator++(int); //后置++

Coordinate operator+(const Coordinate& coor);

int operator[](int nIndex);
protected:
private:
    int m_nX;
    int m_nY;
};

Coordinate::Coordinate( int nX, int nY )
{
    m_nX = nX;
    m_nY = nY;
}

Coordinate& Coordinate::operator-()
{
    m_nX = -m_nX;
    m_nY = -m_nY;
    return *this;
}

Coordinate& Coordinate::operator++()
{
    m_nX++;
    m_nY++;
    return *this;
}

Coordinate Coordinate::operator++( int )
{
    Coordinate old(*this);
    m_nX++;
    m_nY++;
    return old;
}

int Coordinate::getX()
{
    return m_nX;
}

int Coordinate::getY()
{
    return m_nY;
}

Coordinate Coordinate::operator+( const Coordinate& coor )
{
    Coordinate tmp();
    tmp.m_nX = this->m_nX + coor.m_nX;
    tmp.m_nY = this->m_nY + coor.m_nY;
    return tmp;
}

int Coordinate::operator[]( int nIndex )
{
     == nIndex)
    {
        return m_nX;
    }
     == nIndex)
    {
        return m_nY;
    }
    else
    {
        ;
    }
}

//Coordinate& Coordinate::operator-( Coordinate& coor )
 //{
 // coor.m_nX = -coor.m_nX;
 // coor.m_nY = -coor.m_nY;
 // return *this;
 //}

//Coordinate Coordinate::operator+( const Coordinate& coor1, const Coordinate& coor2 )
 //{
 // Coordinate tmp(0, 0);
 // tmp.m_nX = coor1.m_nX + coor2.m_nX;
 // tmp.m_nY = coor1.m_nY + coor2.m_nY;
 // return tmp;
 //}

ostream& operator<<(ostream& out, const Coordinate& coor)
{
    out << coor.m_nX << "," << coor.m_nY;
    return out;
}

int main(void)
{
    /************************************************************************/
    /* string  operator = + <<  
    /************************************************************************/
    string str1("Michael");
    string str2("Joessy");
    string str3 = str1 + " " + str2;
    cout << str3 << endl;

/************************************************************************/
    /* 一元运算符重载 
    /************************************************************************/
    // -(负号)
);
    -coor1;                 //coor1.operator-();    
                            //operator-(coor1);    friend   
    cout << coor1.getX() << "," << coor1.getY() << endl;
    // ++(前置)   
    ++coor1;                //coor1.operator++();
    cout << coor1.getX() << "," << coor1.getY() << endl;
    // ++(后置)   
    coor1++;                //coor1.operator++(0);
    cout << coor1.getX() << "," << coor1.getY() << endl;

/************************************************************************/
    /* 二元运算符重载 
    /************************************************************************/
    Coordinate coorAdd1();
    Coordinate coorAdd2();
    Coordinate coorAddRes3();
    coorAddRes3 = coorAdd1 + coorAdd2;   //coorAdd1.operator+(coorAdd2)
                                         //operator+(coorAdd1, coorAdd2) friend
    cout << coorAddRes3.getX() << "," << coorAddRes3.getY() << endl;

/************************************************************************/
    /* 输出运算符重载  
    /* 必须使用友元运算符重载
    /************************************************************************/
    Coordinate coorTest();
    cout << coorTest << endl;            //operator<<(cout, coorTest)

/************************************************************************/
    /* []索引运算符重载  
    /* 必须使用成员运算符重载
    /************************************************************************/
    Coordinate coorIndex();
    cout << coorIndex[] << endl;        //coorIndex.operator[](0)
] << endl;        //coorIndex.operator[](1)

cin.get();
    ;
}

C++之运算符重载的更多相关文章

  1. C++ 运算符重载时,将运算符两边对象交换问题.

    在C++进行运算符重载时, 一般来讲,运算符两边的对象的顺序是不能交换的. 比如下面的例子: #include <iostream> using namespace std; class ...

  2. C#高级编程笔记2016年10月12日 运算符重载

    1.运算符重载:运算符重重载的关键是在对象上不能总是只调用方法或属性,有时还需要做一些其他工作,例如,对数值进行相加.相乘或逻辑操作等.例如,语句if(a==b).对于类,这个语句在默认状态下会比较引 ...

  3. C++运算符重载

    C++运算符重载 基本知识 重载的运算符是具有特殊名字的函数,他们的名字由关键字operator和其后要定义的运算符号共同组成. 运算符可以重载为成员函数和非成员函数.当一个重载的运算符是成员函数时, ...

  4. 标准C++之运算符重载和虚表指针

    1 -> *运算符重载 //autoptr.cpp     #include<iostream> #include<string> using namespace std ...

  5. python运算符重载

    python运算符重载就是在解释器使用对象内置操作前,拦截该操作,使用自己写的重载方法. 重载方法:__init__为构造函数,__sub__为减法表达式 class Number: def __in ...

  6. PoEduo - C++阶段班【Po学校】-Lesson03-5_运算符重载- 第7天

    PoEduo - Lesson03-5_运算符重载- 第7天 复习前面的知识点 空类会自动生成哪些默认函数 6个默认函数    1  构造  2  析构   3  赋值  4 拷贝构造  5 oper ...

  7. 不可或缺 Windows Native (24) - C++: 运算符重载, 自定义类型转换

    [源码下载] 不可或缺 Windows Native (24) - C++: 运算符重载, 自定义类型转换 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 运算符重载 自 ...

  8. 我的c++学习(8)运算符重载和友元

    运算符的重载,实际是一种特殊的函数重载,必须定义一个函数,并告诉C++编译器,当遇到该运算符时就调用此函数来行使运算符功能.这个函数叫做运算符重载函数(常为类的成员函数). 方法与解释 ◆ 1.定义运 ...

  9. c/c++面试题(6)运算符重载详解

    1.操作符函数: 在特定条件下,编译器有能力把一个由操作数和操作符共同组成的表达式,解释为对 一个全局或成员函数的调用,该全局或成员函数被称为操作符函数.该全局或成员函数 被称为操作符函数.通过定义操 ...

  10. 实验12:Problem H: 整型数组运算符重载

    Home Web Board ProblemSet Standing Status Statistics   Problem H: 整型数组运算符重载 Problem H: 整型数组运算符重载 Tim ...

随机推荐

  1. jQuery Event.which 属性详解

    jQuery Event.which 属性详解 which属性用于返回触发当前事件时按下的键盘按键或鼠标按钮. 对于键盘和鼠标事件,该属性用于确定你按下的是哪一个键盘按键或鼠标按钮. which属性对 ...

  2. jQuery knowledge

    I have used jquery for many years, but didn't list the problem I ever meeting, so here is a list of ...

  3. C#设置文件夹用户权限

    var security = new DirectorySecurity();   string path=@"C:\temp" //设置权限的应用为文件夹本身.子文件夹及文件,所 ...

  4. Python @ddt.file_data() 为.yml 文件实例

    一,创建login.yml 文件(以登录接口为例) 1,创建 login.yml 文件,内容如下图: 打印login.yml 文件,代码及显示效果如下: 代码: import yaml,jsonf = ...

  5. C++中常函数内部的this指针也是const类型的

    代码中碰到一个奇怪的现象,在同样的函数中调用this指针,结果却有一个无法通过编译 // 读取连接信息 void ThirdWizardPage::ReadConnection() { QFile f ...

  6. Eclipse “cannot be resolved to a type” error

    引言:     eclipse新导入的项目经常可以看到"XX cannot be resolved to a type"的报错信息.本文将做以简单总结. 正文:     (1)jd ...

  7. python学习笔记(10)--爬虫下载煎蛋图片

    说明: 1. 有很多细节需要注意! 2. str是保留字,不要作为变量名 3. 保存为txt报错,encoding=utf-8 4. 403错误,添加headers的方法 5. 正则match只能从开 ...

  8. CSS学习笔记(12)--Flex 布局教程:实例篇

    原文--阮一峰博客 作者: 阮一峰 日期: 2015年7月14日 上一篇文章介绍了Flex布局的语法,今天介绍常见布局的Flex写法. 你会看到,不管是什么布局,Flex往往都可以几行命令搞定. 我只 ...

  9. 一款基于css3鼠标经过圆形旋转特效

    今天给大家分享一款基于css3鼠标经过圆形旋转特效.当鼠标经过的时候图片边框颜色旋转,图片显示详情.该实例适用浏览器:IE8.360.FireFox.Chrome.Safari.Opera.傲游.搜狗 ...

  10. js中的target与currentTarget的区别<转>

    关于js中target与currentTarget的区别的关键在于他们所处在的事件流的阶段是不一样的,target处于事件流的目标阶段,currentTarget处理事件流的捕获.处于目标阶段和冒泡阶 ...