C++运算符重载基础性练习
来源:互联网 发布:勇士vs雷霆第七场数据 编辑:程序博客网 时间:2024/05/15 13:41
综合练习
定义Coordinate类
数据成员:m_iX, m_iY
成员函数:构造函数
重载“–”运算符(分为前置和后置、一元),重载“+”运算符(二元)
#include <iostream>using namespace std;/** * 定义Coordinate类 * 数据成员:m_iX,m_iY * 成员函数:构造函数 * 重载--运算符,重载+运算符 */class Coordinate{public: Coordinate(int x, int y) { m_iX = x; m_iY = y; } // 前置--运算符重载 Coordinate& operator--() { m_iX--; m_iY--; return *this; } // 后置--运算符重载 Coordinate operator --(int) { Coordinate old(*this); this->m_iX--; this->m_iY--; return old; } // +号运算符重载 Coordinate operator +(Coordinate& coor) { Coordinate old(0,0); old.m_iX=this->m_iX+coor.m_iX; old.m_iY=this->m_iY+coor.m_iY; return old; }public: int m_iX; int m_iY;};int main(void){ Coordinate coor1(1, 3); Coordinate coor2(2, 4); Coordinate coor3(0, 0); coor1--; --coor2; coor3 = coor1 + coor2; cout << coor3.m_iX << endl; cout << coor3.m_iY << endl; return 0;}
1 0
- C++运算符重载基础性练习
- 【c++】运算符重载练习
- 运算符重载练习
- 重载运算符练习
- 运算符重载练习(一)
- 运算符的重载练习
- c++运算符重载练习
- C++运算符重载练习
- C++-运算符重载
- [C++]重载运算符
- C#:运算符重载
- c++-++运算符重载
- C++:重载运算符
- 运算符重载(C++)
- [c++]运算符重载
- 【C++】运算符重载
- C++--------------------------------------------运算符重载
- C#:运算符重载
- 最简单的一种栈 固定大小 [数组实现]
- github安装及使用图文详解
- PAT乙级1047
- 技术员必须的品质
- 强制系统横屏竖屏
- C++运算符重载基础性练习
- openURL 不能使用iOS 10
- Java的final笔记
- 数据采集方案初探
- codeblocks 对‘cv::waitKey(int)’未定义的引用
- 10 Java并发编程1总结
- MySql批量插入多条数据的一种具体实现方法
- 使用Eclipse将Android项目打Library包
- 深度学习之卷积神经网络学习摘录(一)