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 // = 是一元操作符.不写,编译器会提供 ...
随机推荐
- 部署vue-element-admin流程
1.修改环境变量: 在以下两个文件: .env_staging [如果修改这个,需要以npm run build:stage启动] .env_production [如果修改这个,需要以npm run ...
- 不能绑定到端口号:9194原因:Cannot assign requested address: JVM_Bind
晚上将老服务器程序从win2008部署在新的云服务器win2012上,其实就是复制过去改改配置,启动时突然报不能绑定到端口号:9194原因:Cannot assign requested addres ...
- PC端视频播放器
视频播放器:Potplayer 它是一款纯净的.无广告.极速
- 如何用css实现太极图
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>太 ...
- flex布局使用
什么是flex布局 flex是flexible Box的缩写,意味"弹性盒子",用来为盒子状模型提供最大的灵活性 任何一个盒子都可以指定为flex布局 .box{ display: ...
- Linux下shell脚本实现mongodb定时自动备份
MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案. MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功 ...
- 关于实现序列化接口Serializable
关于javabean,是否需要实现序列化接口这个问题,只有当这些javabean需要通过分布式网络传输,磁盘持久化等情况下才有必要,其他情况并非必须.
- Web安全测试学习笔记-DVWA-SQL注入-1
SQL注入的定义网上很多,作为一个初学者,我对SQL注入的理解是这样的:网站应用一般都有后台数据库(不论是关系型还是非关系型),用户在网站上的绝大部分操作,最终都会跟数据库交互(也就是执行一串SQL语 ...
- 查看Linux服务器CPU总核数
下面介绍查看Linux服务器CPU总核数的方法. 通过/proc/cpuinfo可查看CPU个数及总核数. [root@kevin ~]# grep processor /proc/cpuinfo | ...
- JS 实现动态轮播图
JavaScript实现轮播图思路 + html/css + js源码 整个轮播图的效果是通过js代码,操作dom, 拿到html我们需要的元素,控制整个ul的距离浏览器左边的位置,让排好的图片依次出 ...