简介:

  重载的运算符是具有特殊名字的函数:它们的名字由关键字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. haffman树c实现

    #include<stdio.h>#include<stdlib.h>#include<string.h>#define N 100#define M 2*N-1t ...

  2. poj1006

    题目大意:生物节律 一些人相信有三种循环在一个人生命中从他或者她出生的那天起,这个三个周期是,身体,情绪,智力, 并且他们有23,28,和33天的时间,在每一个周期里面都有一个周期,在一个周期的高峰期 ...

  3. 使用atomic一定是线程安全的吗

    这个问题很少遇到,但是答案当然不是.atomic在set方法里加了锁,防止了多线程一直去写这个property,造成难以预计的数值.但这也只是读写的锁定.跟线程安全其实还是差一些.看下面. @inte ...

  4. angularJs $injector

    一 angularJS中几种注入方式 Spring中使用构造注入或者设值注入的方式,还需要做一些额外的操作,但是angular中只需要在需要的地方声明一下即可,类似模块的引用,因此十分方便. angu ...

  5. boost库在工作(32)网络服务端之二

    在这个例子里,服务器对象主要使用boost::asio::io_service对象,这个对象主要用来构造异步接收数据使用,接着定义boost::asio::ip::tcp::acceptor对象,这个 ...

  6. PHP开发安全之近墨者浅谈(转)

    ==过滤输入/输出转义 过滤是Web应用安全的基础.它是你验证数据合法性的过程.通过在输入时确认对所有的数据进行过滤,你可以避免被污染(未过滤)数据在你的程序中被误信及误用.大多数流行的PHP应用的漏 ...

  7. rsyslog安装

    http://www.rsyslog.com/downloads/download-other/ http://www.rsyslog.com/download/stable-download/pag ...

  8. Python开发【第十三篇】:jQuery--无内容点击-不进去(一)

    Python开发[第十三篇]:jQuery--无内容点击-不进去(一)

  9. 洛谷 1373 小a和uim之大逃离

    /* 很容易想到f[i][j][k][l][01] 表示到ij点 两个人得分为kl 01表示这一步谁走的 因为起点不同 路径不同 所以要枚举起点.. 时间复杂度 O(nmk*nmk) 空间复杂度 O( ...

  10. C#中的IO流操作(FileStream)

    StreamReader和StreamWriter适用于对文本文件的操作,因为它是以字符为单位进行的操作 不用担心编码问题 using (Stream s = new FileStream(@&quo ...