C++ 的 +,加号重载示例
#include <iostream> // overloading "operator + "
// 要考虑加法的形式
// a+1
// a+a
// 1+a ////////////////////////////////////////////////////////// class Rectangle
{
public:
Rectangle(const int w, const int h)
: width(w), height(h)
{}; ~Rectangle() {};
Rectangle operator+ (const int& i) const; // a+1,这里不能返回引用
Rectangle operator+ (const Rectangle& i) const; // a+a
// 1+a,定义在class之外 public:
int width;
int height;
}; ////////////////////////////////////////////////////////// Rectangle
Rectangle::operator+(const int& i) const // a+1
{
Rectangle r(*this);
r.width += i;
r.height += i; return r;
} Rectangle
Rectangle::operator+(const Rectangle& rec) const // a+a
{
Rectangle r(*this);
r.width += rec.width;
r.height += rec.height; return r;
} ////////////////////////////////////////////////////////// std::ostream&
operator<< (std::ostream& os, const Rectangle& rec)
{
os << rec.width << ", " << rec.height;
return os; } Rectangle
operator+(const int i, const Rectangle& rec) // 1+a
{
Rectangle r(rec);
r.width += i;
r.height += i; return r;
} ////////////////////////////////////////////////////////// int main()
{
Rectangle a(40, 10); std::cout << "a+1 = " << a + 1 << std::endl;
std::cout << "a+a = " << a + a << std::endl;
std::cout << "1+a = " << 1 + a << std::endl; // 这种形式,先计算1+a,然后a+a,最后a+1。
std::cout
<< "a+1 = " << a + 1 << std::endl
<< "a+a = " << a + a << std::endl
<< "1+a = " << 1 + a << std::endl
; return 0;
}
C++ 的 +,加号重载示例的更多相关文章
- 计算时间:一个C++运算符重载示例
Time类是一个用于计算时间的类,其原型如下:程序清单11.1 mytime0.h // mytime0.h -- Time class before operator overloading #if ...
- C++ class外的 >> 重载,输入流,重载示例。不应该定义类内的>>重载
#include <iostream> // overloading "operator >> " outside class // >> 应该 ...
- C++ class 内的 () 重载示例
#include <iostream> // overloading "operator () " outside class //////////////////// ...
- C++ class 内的 [] 重载示例。
#include <iostream> // overloading "operator [] " inside class ///////////////////// ...
- C++ class 外的 ++ 重载,左++,右++,重载示例。
#include <iostream> // overloading "operator ++ " outside class // ++ 是一元操作符 /////// ...
- C++ class内的 ++ 重载,左++,右++,重载示例。
#include <iostream> // overloading "operator ++ " inside class // ++ 是一元操作符 //////// ...
- C++ class外的 << 重载,输出流,重载示例。不应该定义类内的<<重载
#include <iostream> // overloading "operator << " outside class // << 应该 ...
- C++ class内的 < 和 > 重载,大于号,小于号,重载示例。
#include <iostream> // overloading "operator = " outside class // < 和 > 是二元操作符 ...
- C++ class内的=重载,拷贝赋值函数copy op=,重载示例。必须是class内
#include <iostream> // overloading "operator = " inside class // = 是一元操作符.不写,编译器会提供 ...
随机推荐
- FCC---CSS Flexbox: Add Flex Superpowers to the Tweet Embed
To the right is the tweet embed that will be used as the practical example. Some of the elements wou ...
- Android五大布局详解——LinearLayout(线性布局)
Android五大布局 本篇开始介绍Android的五大布局的知识,一个丰富的界面显示总是要有众多的控件来组成的,那么怎样才能让这些控件能够按你的想法进行摆放,从而自定义你所想要的用户界面呢?这就牵涉 ...
- [Go] 实现websocket服务端
直接使用官方子包可以实现websocket协议, golang.org/x/net/websocket 如果在这个目录没有这个包就去github下载net库,放进这个目录$GOPATH/src/gol ...
- pytorch 中的Variable一般常用的使用方法
Variable一般的初始化方法,默认是不求梯度的 import torch from torch.autograd import Variable x_tensor = torch.randn(2, ...
- JS去重的几种常见方法
JS数组去重的几种常见方法 一.简单的去重方法 // 最简单数组去重法 /* * 新建一新数组,遍历传入数组,值不在新数组就push进该新数组中 * IE8以下不支持数组的indexOf方法 * */ ...
- C++ std::vector 基本用法2
#include <iostream> #include <vector> using namespace std; int main() { int ar[10] = { 1 ...
- 2019年跨越速递Java工程师笔试题
1.下面哪个选项可以用于JSP页面之间传递对象(A C) A application B page C session D error E response 评语:这道题考察的是对JSP内置对象的了 ...
- java8-新的日期API
背景 java的日期和时间API设计不理想,java8引入新的时间和日期API就是为了解决这个问题. 老的日期API的核心类 缺点 Date 月从0开始,年最小从1900年开始,没有时区的概念 Cal ...
- SVM算法核函数的选择
SVM支持向量机,一般用于二分类模型,支持线性可分和非线性划分.SVM中用到的核函数有线性核'linear'.多项式核函数pkf以及高斯核函数rbf. 当训练数据线性可分时,一般用线性核函数,直接实现 ...
- ubuntu18.04 安装 QQ
参照大佬文章https://www.lulinux.com/archives/1319 我将安装过程需要的命令行总结出来,便于直接快速安装. # 安装 wine git clone https://g ...