#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++ 的 +,加号重载示例的更多相关文章

  1. 计算时间:一个C++运算符重载示例

    Time类是一个用于计算时间的类,其原型如下:程序清单11.1 mytime0.h // mytime0.h -- Time class before operator overloading #if ...

  2. C++ class外的 >> 重载,输入流,重载示例。不应该定义类内的>>重载

    #include <iostream> // overloading "operator >> " outside class // >> 应该 ...

  3. C++ class 内的 () 重载示例

    #include <iostream> // overloading "operator () " outside class //////////////////// ...

  4. C++ class 内的 [] 重载示例。

    #include <iostream> // overloading "operator [] " inside class ///////////////////// ...

  5. C++ class 外的 ++ 重载,左++,右++,重载示例。

    #include <iostream> // overloading "operator ++ " outside class // ++ 是一元操作符 /////// ...

  6. C++ class内的 ++ 重载,左++,右++,重载示例。

    #include <iostream> // overloading "operator ++ " inside class // ++ 是一元操作符 //////// ...

  7. C++ class外的 << 重载,输出流,重载示例。不应该定义类内的<<重载

    #include <iostream> // overloading "operator << " outside class // << 应该 ...

  8. C++ class内的 < 和 > 重载,大于号,小于号,重载示例。

    #include <iostream> // overloading "operator = " outside class // < 和 > 是二元操作符 ...

  9. C++ class内的=重载,拷贝赋值函数copy op=,重载示例。必须是class内

    #include <iostream> // overloading "operator = " inside class // = 是一元操作符.不写,编译器会提供 ...

随机推荐

  1. 简单的权限管理php

    转发自https://www.cnblogs.com/shenzikun1314/p/6604867.html#4262295 首先,要明白的基础理论是用户,角色,权限之间的关系是一对多,还是多对多. ...

  2. vue-cli3配置webpack-bundle-analyzer插件

    为优化vue项目性能,需要使用webpack-bundle-analyzer分析报文件,找出最占用空间的插件有哪些,对应做出优化 网上看了一些网站,有的写的太麻烦了,现将最简单的一种写出来供大家参考 ...

  3. 属性文件——Java&Spring

    属性文件 什么是属性文件 ? 定义:一个扩展名为properties文件,属性文件都是以key-value(键值对)来保存文件的内容,如:log4j.properties,db.properties等 ...

  4. 有抱负的 DevOps 和 SRE 工程师必读好书清单 | 文末有福利!

    原文地址:https://medium.com/faun/10-great-books-for-aspiring-devops-sre-engineers-76536c7c4909 原文作者:Ayme ...

  5. C#后台架构师成长之路-Orm篇体系

    成为了高工,只是完成体系的熟练,这个时候就要学会啃一些框架了... 常用Orm底层框架的熟悉: 1.轻量泛型的DBHelper,一般高工都自己写的出来的 2.EF-基于Linq的,好好用 3.Keel ...

  6. Python导入运行的当前模块报错

    引言 今天遇到了一个奇怪的现象,简单举个栗子: 文件结构如下:

  7. 电位器控制两个 LED 灯交替闪烁

    电路图: 布局:

  8. CF1062F Upgrading Cities

    题意 由于这是个\(DAG\),我们考虑拓朴排序,求某个点能到的和能到它的点,这是两个问题,我们可以正反两边拓朴排序,这样就只用考虑它能到的点了 设\(f[x]\)表示\(x\)能到的点数\(+\)能 ...

  9. nginx 反向代理之 proxy_pass

    格式很简单: proxy_pass URL; 其中URL包含:传输协议(http://, https://等).主机名(域名或者IP:PORT).uri. 示例如下: proxy_pass http: ...

  10. [译]Vulkan教程(23)暂存buffer

    [译]Vulkan教程(23)暂存buffer Staging buffer 暂存buffer Introduction 入门 The vertex buffer we have right now ...