简介:

  重载的运算符是具有特殊名字的函数:它们的名字由关键字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. Interesting Punch-Bowl(优先队列)

    /** http://acm.nyist.net/JudgeOnline/problem.php?pid=547 题意: 有一个正方形的区域 区域上面有高度不同的1*1的立方体自然有凸有凹 凹的地方可 ...

  2. MyBatis(6):MyBatis集成Spring事务管理(下)

    前一篇文章复习了MyBatis的基本使用以及使用Spring管理MyBatis的事务的做法,本文的目的是在这个的基础上稍微做一点点的进阶:多数据的事务处理.文章内容主要包含两方面: 1.单表多数据的事 ...

  3. git上解决代码冲突

    1.切换到master: git co master 2.拉最新代码:git pull origin master 3.删掉多余符号 4.切换到提交的分支:git br Txxxx 5.合并:git  ...

  4. uva12118

    一开始以为直接算联通块个数就行了 后来发现还得分联通块里的奇点... 还要注意m = 0的情况... #include<iostream> #include<algorithm> ...

  5. html contenteditable

    contenteditable 是html中的一個屬性,在HTML中,某些元素設置 contenteditable='true'  屬性時可以開啟該元素的編輯模式,contenteditable 可以 ...

  6. Maven Build Life Cycle--reference

    What is Build Lifecycle? A Build Lifecycle is a well defined sequence of phases which define the ord ...

  7. [转] [翻译]图解boost::bind

    http://kelvinh.github.io/blog/2013/12/03/boost-bind-illustrated/ 其实这是很久之前留的一个坑了,一直没有填.. 记得在刚开始看到 boo ...

  8. IDL实现主成分变化(PCA)

    IDL只能通过调用envi的二次接口做图像的变换,但是对于普通的数据没有提供函数.根据主成分变换的原理,用IDL写出来了,这样就不用每次再去用matlab的princomp去做了.主成分变化的基本过程 ...

  9. Day6 - Python基础6 面向对象编程

    Python之路,Day6 - 面向对象学习   本节内容:   面向对象编程介绍 为什么要用面向对象进行开发? 面向对象的特性:封装.继承.多态 类.方法.     引子 你现在是一家游戏公司的开发 ...

  10. RxJava 教程-1 简介 原理 线程控制 变换

    简介 RxJava 是什么? RxJava 在 GitHub 主页上的自我介绍是 RxJava is a Java VM implementation of ReactiveX: a library ...