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 // = 是一元操作符.不写,编译器会提供 ...
随机推荐
- SourceTree Mac安装跳过注册步骤
1.打开sourcetree2.关闭sourcetree3.命令终端输入defaults write com.torusknot.SourceTreeNotMAS completedWelcomeWi ...
- MyBatis框架之第三篇
8.Spring与Mybatis整合 框架的整合就是软件之间的集成,它很抽象,因此在做整合之前先想好思路.规划好思路然后按照思路一步一步的做就可以实现框架的整合. 8.1.SM整合思路 8.1.1.思 ...
- WestWild: 1.1: Vulnhub Walkthorugh
启动界面 主机层面扫描: ╰─ nmap -p1-65535 -sV -A 10.10.202.131 Starting Nmap 7.70 ( https://nmap.org ) at 2019- ...
- oracle 根据时间戳查询date类型sql
话不多说上sql: select to_char(1574837126879/(1000*60*60*24)+to_date('1970-01-01 08:00:00','YYYY-MM-DD HH2 ...
- OGG For Oracle To PostgreSQL
本文档描述OGG(Oracle goldengate)为Oracle同步到PostgreSQL数据库配置.在目前去“IOE”潮流.PostgreSQL确实是Oracle最好的替代品之一. 实验环境如下 ...
- js对象数组中的某属性值 拼接成字符串
js对象数组中的某属性值 拼接成字符串 var objs=[ {id:1,name:'张三'}, {id:2,name:'李四'}, {id:3,name:'王五'}, {id:4,name:'赵六' ...
- select同时获取value和label的值
Element ui 框架型 <el-select v-model="sketchID" autocomplete="off" @@change=&quo ...
- 用POI 3.17读取EXCEL数据
导入jar 包 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</a ...
- acwing 849 Dijkstra求最短路 I 模板
地址 https://www.acwing.com/problem/content/description/851/ 给定一个n个点m条边的有向图,图中可能存在重边和自环,所有边权均为正值. 请你求出 ...
- C# LINQ Join两个表连接,关联多个条件的写法
1.sql语句: select * from Users u join Teachers t on u.UserID==t.TeacherID and u.Name=t.Name 2.linq写法: ...