C++学习笔记4——类的封装(2)
简介:
重载的运算符是具有特殊名字的函数:它们的名字由关键字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)的更多相关文章
- C++学习笔记3——类的封装(1)
封装: 1.把属性和方法进行封装. 2.对属性和方法进行访问控制. class和struct的区别: class和struct的唯一区别是默认的访问权限不一样.struct的默认访问权限是public ...
- 学习笔记 Java类的封装、继承和多态 2014.7.10
1.问题:toString()没搞懂? int a = 1; Integer aa = new Integer(a); //这是实现的过程 System.out.println("Hello ...
- python学习笔记4_类和更抽象
python学习笔记4_类和更抽象 一.对象 class 对象主要有三个特性,继承.封装.多态.python的核心. 1.多态.封装.继承 多态,就算不知道变量所引用的类型,还是可以操作对象,根据类型 ...
- Java学习笔记——File类之文件管理和读写操作、下载图片
Java学习笔记——File类之文件管理和读写操作.下载图片 File类的总结: 1.文件和文件夹的创建 2.文件的读取 3.文件的写入 4.文件的复制(字符流.字节流.处理流) 5.以图片地址下载图 ...
- Java学习笔记之---类和对象
Java学习笔记之---类和对象 (一)类 类是一个模板,它描述一类对象的行为和状态 例如:动物类是一个类,动物们都有属性:颜色,动物们都有行为:吃饭 public class Dog { Stri ...
- UML学习笔记:类图
UML学习笔记:类图 有些问题,不去解决,就永远都是问题! 类图 类图(Class Diagrame)是描述类.接口以及它们之间关系的图,用来显示系统中各个类的静态结构. 类图包含2种元素:类.接口, ...
- swift学习笔记3——类、结构体、枚举
之前学习swift时的个人笔记,根据github:the-swift-programming-language-in-chinese学习.总结,将重要的内容提取,加以理解后整理为学习笔记,方便以后查询 ...
- .net学习笔记---HttpRuntime类
HttpRuntime在ASP.NET处理请求中负责的是创建HttpContext对象以及调用HttpApplicationFactory创建HttpApplication. 其定义如下: publi ...
- C++ Primer 学习笔记_57_类和数据抽象 --管理指针成员
复印控制 --管理指针成员 引言: 包括指针的类须要特别注意复制控制.原因是复制指针时.一个带指针成员的指针类 class HasPtr { public: HasPtr(int *p,int i): ...
随机推荐
- 软工UML学习札记
UML模型由:事物.关系和图组成 (1)类(class)── 类用带有类名.属性和操作的矩形框来表示. (2)主动类(active class)── 主动类的实例应具有一个或多个进程或线程,能够启动控 ...
- Threading Module源码概述(一)
Python的Threading模块是建立在thread module基础上的一个模块,在threading模块中,暴露着许多thread模块的属性.比如threading._get_ident实际上 ...
- 第一节:Scrapy开源框架初探
Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 可以应用在包括数据挖掘,信息处理或存储历史数据等一系列的程序中. 具体开发流程如下: 一.确定待抓取网站 当您需要从某 ...
- storyBoard方式ScrollView的AutoLayout
在使用storyboard和xib时,我们经常要用到ScrollView,还有自动 布局AutoLayout,但是ScrollView和AutoLayout 结合使用,相对来说有点复杂.根据实践,我说 ...
- 九度OnlineJudge之1001:A+B for Matrices
题目描述: This time, you are supposed to find A+B where A and B are two matrices, and then count the num ...
- SVN server的搭建
当做大的项目是,svn是代码管理的好工具,假设是用自己的server,那么须要搭建SVNserver. Subversion是一款很优秀的svnserver工具,笔者採用VisualSVN serve ...
- C\C++代码优化的27个建议
1. 记住阿姆达尔定律: funccost是函数func运行时间百分比,funcspeedup是你优化函数的运行的系数. 所以,如果你优化了函数TriangleIntersect执行40%的运行时间, ...
- Linux如何关闭防火墙和查看防火墙的具体情况
1.Linux下关闭和开启防火墙 1) 重启后生效 开启: chkconfig iptables on 关闭: chkconfig iptables off 2) 即时生效,重启后失效 开启: ser ...
- C#中的转义字符
一些常用的转义字符: \n 换行 \b backspace,删除光标前面的一个字符 \t tab键 由多个空格组成的一个字符,具有行与行之间的对齐功能 \\ \ 如果在字符串前面加@的话: 1 ...
- python函数的使用和返回值
#coding=utf-8 def a(): i=1a() #函数的返回值,用return语句实现 #一个返回值的情况def test(): i=7 return iprint test() #多个返 ...