简介:

  重载的运算符是具有特殊名字的函数:它们的名字由关键字operator和其后要定义的运算符号共同组成。其中一元运算符有一个参数,二元运算符有两个参数。

可以被重载的运算符  
+ - * / % ^
& | ~ ! , =
< > <= >= ++ --
<< >> == != && ||
+= -= /= %= ^= &=
|= *= <<= >>= [] ()
-> ->* new new[] delete delete[]
不能被重载的运算符
:: .* . ? :    

PS:

  &&和||运算符无法实现短路保护。

类型:

  定义运算符重载时可定义为成员函数或者普通的非成员函数。

判断依据:

  1、赋值(=)、下标([])、调用(())和成员访问箭头(->)运算符必须是成员。

  2、复合赋值运算符一般来说是成员,但并非必须。

  3、改变对象状态的运算符或者与给定类型密切相关的运算符,如递增、递减和解引用运算符通常应该是成员。

  4、具有对称性的运算符可能转换任意一端的运算对象,例如算数、相等性、关系和位运算符等,通常应该是普通非成员函数。

  5、输入和输出运算符必须是非成员函数。

Demo:

#pragma once
#ifndef _COMPLEXNUMBER_
#define _COMPLEXNUMBER_ #include <iostream> class complexNum
{
friend std::ostream& operator<<(std::ostream &out, complexNum &rhs);
friend std::istream& operator>>(std::istream &in, complexNum &rhs); public:
complexNum(int real = , int image = );
complexNum(const complexNum &obj); public:
complexNum& operator=(const complexNum &rhs); complexNum operator+(const complexNum &rhs); complexNum& operator++(void); //前置++
complexNum operator++(int); //后置++,需要一个int的占位符
complexNum& operator+=(const complexNum &rhs);
bool operator>(const complexNum &rhs); private:
int real;
int image;
}; #endif
#include "complexNumber.h"

complexNum::complexNum(int real, int image) :real(real), image(image){}

complexNum::complexNum(const complexNum &obj) : real(obj.real), image(obj.image){}

std::ostream& operator<<(std::ostream &out, complexNum &rhs)
{
out << rhs.real; if (rhs.image >= )
out << "+"; out << rhs.image << "i" << std::endl; return out;
} std::istream& operator>>(std::istream &in, complexNum &rhs)
{
return in >> rhs.real >> rhs.image;
} complexNum& complexNum::operator=(const complexNum &rhs)
{
this->real = rhs.real;
this->image = rhs.image; return *this;
} complexNum complexNum::operator+(const complexNum &rhs)
{
complexNum tmp; tmp.real = this->real + rhs.real;
tmp.image = this->image + rhs.image; return tmp;
} complexNum& complexNum::operator++(void)
{
this->real++;
this->image++; return *this;
} complexNum complexNum::operator++(int)
{
complexNum tmp = *this; this->real++;
this->image++; return tmp;
} complexNum& complexNum::operator+=(const complexNum &rhs)
{
this->operator+(rhs); return *this;
} bool complexNum::operator>(const complexNum &rhs)
{
if (this->real > rhs.real)
return true;
else if (this->real < rhs.real)
return false;
else
{
if (this->image > rhs.image)
return true;
else
return false;
}
}

C++学习笔记4——类的封装(2)的更多相关文章

  1. C++学习笔记3——类的封装(1)

    封装: 1.把属性和方法进行封装. 2.对属性和方法进行访问控制. class和struct的区别: class和struct的唯一区别是默认的访问权限不一样.struct的默认访问权限是public ...

  2. 学习笔记 Java类的封装、继承和多态 2014.7.10

    1.问题:toString()没搞懂? int a = 1; Integer aa = new Integer(a); //这是实现的过程 System.out.println("Hello ...

  3. python学习笔记4_类和更抽象

    python学习笔记4_类和更抽象 一.对象 class 对象主要有三个特性,继承.封装.多态.python的核心. 1.多态.封装.继承 多态,就算不知道变量所引用的类型,还是可以操作对象,根据类型 ...

  4. Java学习笔记——File类之文件管理和读写操作、下载图片

    Java学习笔记——File类之文件管理和读写操作.下载图片 File类的总结: 1.文件和文件夹的创建 2.文件的读取 3.文件的写入 4.文件的复制(字符流.字节流.处理流) 5.以图片地址下载图 ...

  5. Java学习笔记之---类和对象

    Java学习笔记之---类和对象 (一)类 类是一个模板,它描述一类对象的行为和状态  例如:动物类是一个类,动物们都有属性:颜色,动物们都有行为:吃饭 public class Dog { Stri ...

  6. UML学习笔记:类图

    UML学习笔记:类图 有些问题,不去解决,就永远都是问题! 类图 类图(Class Diagrame)是描述类.接口以及它们之间关系的图,用来显示系统中各个类的静态结构. 类图包含2种元素:类.接口, ...

  7. swift学习笔记3——类、结构体、枚举

    之前学习swift时的个人笔记,根据github:the-swift-programming-language-in-chinese学习.总结,将重要的内容提取,加以理解后整理为学习笔记,方便以后查询 ...

  8. .net学习笔记---HttpRuntime类

    HttpRuntime在ASP.NET处理请求中负责的是创建HttpContext对象以及调用HttpApplicationFactory创建HttpApplication. 其定义如下: publi ...

  9. C++ Primer 学习笔记_57_类和数据抽象 --管理指针成员

    复印控制 --管理指针成员 引言: 包括指针的类须要特别注意复制控制.原因是复制指针时.一个带指针成员的指针类 class HasPtr { public: HasPtr(int *p,int i): ...

随机推荐

  1. Java接口,抽象类

    1.接口(interface),接口被用来建立类与类之间关联的标准. 2.抽象类(abstract class),只要类中有一个抽象方法,此类就被标记为抽象类.实际上抽象类除了被继承之外没有任何意义. ...

  2. hdu4549之矩阵快速幂

    M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Su ...

  3. 来自投资银行的20个Java面试题

    问题一:在多线程环境中使用HashMap会有什么问题?在什么情况下使用get()方法会产生无限循环? HashMap本身没有什么问题,有没有问题取决于你是如何使用它的.比如,你在一个线程里初始化了一个 ...

  4. 分布式系统状态下redis存储asp.net session使用第三方Providers驱动

    https://github.com/ServiceStack/ServiceStack.Redis (redis客户端组件) 注:redis服务端在windows不太稳定,一般部署在Linux下. ...

  5. Meth | phpstorm invalid descendent file name

     Failed to collect files: Invalid descendent file name "codelog_ddz.\"(]))\",\').txt& ...

  6. [转] STL源码学习----lower_bound和upper_bound算法

    http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html PS: lower_bound of value 就是最后一个 < ...

  7. 模板-->中国剩余定理[互质版本]

    如果有相应的OJ题目,欢迎同学们提供相应的链接 相关链接 所有模板的快速链接 扩展欧几里得extend_gcd模板 poj_1006_Biorhythms,my_ac_code 简单的测试 None ...

  8. python实现登录函数,比较简单

    一个简单的python实现登录以及修改密码的函数 #密码错误3次,锁定登录: password_list = ['] def account_login(): Tries = 3 while Trie ...

  9. eclipse开发servlet应用,Tomcat无法访问jpg图片 ===第二版===

    之前版本中,设置完后,确实可以访问图片了,但是问题接着来了,那就是,无法访问servlet的服务了. 后来想了下,原因也挺好理解的,设置到了Tomcat目录,而项目没有部署,所以没能访问. 但是怎么两 ...

  10. COGS 渡轮问题 (LIS规定字典序输出方案数)

    /* 下标字典序最小 */ #include<iostream> #include<cstdio> #include<cstring> #define maxn 1 ...