c++学习笔记_4
前言:本笔记所对应的课程为中国大学mooc中北京大学的程序设计与算法(三)C++面向对象程序设计,主要供自己复习使用,且本笔记建立在会使用c和java的基础上,只针对与c和java的不同来写
运算符重载
运算符重载的基本概念
目的:希望使对象也能用基本的运算符进行运算。本质上是函数重载。
可以将运算符重载为普通函数,也可以重载为成员函数。
一个运算符可以被多次重载,根据实参的具体类型决定调用哪个重载函数。
格式:
返回值类型 operator运算符符号(参数表){
}
实例:
class Complex{
public:
double real, imag;
Complex(double r = 0.0, double i = 0.0):real(r), imag(i){
}
Complex operator-(const Complex & c);
};
Complex operator+(const Complex & c1, const Complex & c2){
return Complex(c1.real+c2.real, c1.imag+c2.imag);
} //当重载为普通函数时,参数个数为运算符目数
Complex Complex::operator-(const Complex & c){
return Complex(real-c.real, imag-c.imag);
} //当重载为成员函数时,参数个数为运算符目数减一
int main(){
Complex a(4, 4), b(1, 1), c, d;
c = a + b; //等价于 c = operator+(a, b);
d = a - b; //等价于 d = a.operator-(b);
}
赋值运算符的重载
赋值运算符只能重载为成员函数。
实例:
# include <iostream>
# include <cstring>
using namespace std;
class String{
private:
char * str;
public:
String():str(new char[1]) { str = 0; }
String(String & s){
str = new char[strlen(s.str)+1];
strcpy(str, s.str);
}
long long adderss() {
return (long long)(&str);
}
const char * c_str() { return str; }
String & operator=(const char * s);
String & operator=(const String & s);
~String() { delete [] str; }
};
String & String::operator=(const char * s){
//该函数的作用是可以使“obj = "hello"”这样的语句成立(和类型转换构造函数是有区别的)。
delete [] str;
str = new char[strlen(s)+1];
strcpy(str, s);
return * this;
}
String & String::operator=(const String & s){
//这一句是防止出现 s = s 的情况。
if(this == &s)
return * this;
delete [] str;
str = new char[strlen(s.str)+1];
strcpy(str, s.str);
return * this;
}
int main(){
String s;
s = "good luck";
cout << s.c_str() << endl;
//等价于s.operator=("good luck");
//String s2 = "hello";
//这一句是错的,原因:这里的等号不是赋值号,而是“类型转换构造函数”的赋初值法,由于没有写“类型转换构造函数”,因此不会通过。
String s3, s4;
s3 = "first";
s4 = "second";
cout << s3.adderss() << endl;
cout << s4.adderss() << endl;
s3 = s4;
cout << s3.adderss() << endl;
cout << s4.adderss() << endl;
cout << s3.c_str() << endl;
cout << s4.c_str() << endl;
//直接让同一类的两对象相等时,会让s3中每一成员变量的值去等于s4中对应的每一成员变量的值。由于String的成员变量str是指针,因此直接让s3 = s4的话会导致指向同一内存区域,后续进行其他操作时有很大隐患。因此要额外增加一赋值运算符重载函数。
//这里的address()函数用以检查str的地址是否也被赋值
String s5 = s;
cout << s5.c_str() << endl;
//注意这里的等号不是赋值号,而是调用“复制构造函数”赋初值。等价于"String s5(s); "。同理由于String的成员变量str是指针,如果使用默认的“复制构造函数”,会产生上述同样的问题,因此要额外写一个“复制构造函数”。
return 0;
}
注意,对于operator=的返回值类型,应该是String &,而不是void或是String
运算符重载为友元
目的:我们一般将运算符重载为成员函数,但有时会出现不够用的情况,因此需要将运算符重载为友元。
实例:
class Complex{
public:
double real, imag;
Complex(double r = 0.0, double i = 0.0):real(r), imag(i){ }
Complex operator+(double r){
return Complex(real + r, imag);
} //(1)
friend Complex operator+(double r, Complex & c); //(2)
};
Complex operator+(double r, Complex & c){
return Complex(c.real + r, c.imag);
}
int main(){
Complex c;
c = c + 5; //调用(1)
c = 5 + c; //调用(2)
}
可变长数组类的实现
实现方法:重载[]运算符。具体代码暂略。
流插入运算符和流提取运算符的重载
- cout是在iostream中定义的,ostream类的对象;cin是istream类的对象。
- 一般将重载函数设为全局函数,如果需要用到某对象中的private变量,则可以声明为friend
实例:
# include <iostream>
using namespace std;
class Student{
private:
int age;
friend ostream & operator << (ostream & o, const Student & s);
public:
Student(int n):age(n) {};
};
ostream & operator << (ostream & o, const Student & s){
o << s.age;
return o;
}
int main(){
Student s(5);
cout << s << " years old" << endl;
return 0;
}
类型转换运算符的重载
- 注意强制类型转换函数的书写形式,不用写返回类型
- 在存在某强制类型转换函数的情况下,如果某些地方使用了该强制类型转换函数可以使程序正确运行,那么会发生隐式的强制类型转换。
实例:
# include <iostream>
using namespace std;
class Complex{
private:
double real, imag;
public:
Complex(double r, double i):real(r), imag(i) {}
operator double (){
return real;
}
//注意强制类型转换函数的书写形式
};
int main(){
Complex c(5, 4);
cout << (double)c << endl;
double n = 2 + c; //这里会发生自动的强制类型转换,等价于n = 2 + c.operator double();
cout << n << endl;
return 0;
}
自增自减运算符的重载
- 由于自增自减运算符有前置与后置之分,因此两者写法不同:前置运算符需要作为一元运算符重载,后置运算符需要作为二元运算符重载(增加一个无用的int参数)具体用法如下面实例所示。
- 在只写了前置的重载函数,而没有写后置的重载函数时,如果调用后置运算(例如obj++),在vs中会调用前置重载,在dev中会编译出错。
实例:
# include <iostream>
using namespace std;
class Complex{
private:
double real, imag;
public:
Complex(double r, double i):real(r), imag(i){ };
//前置成员函数写法:返回值为对象的引用
Complex & operator ++ (){
++real;
return *this;
}
//后置成员函数写法:多了一个无用的int参数,返回值为新的对象
Complex operator ++ (int){
Complex temp(*this);
++real;
return temp;
}
friend Complex & operator -- (Complex & c);
friend Complex operator -- (Complex & c, int);
friend ostream & operator << (ostream & o, const Complex & c);
};
//前置全局函数写法
Complex & operator -- (Complex & c){
--c.real;
return c;
}
//后置全局函数写法
Complex operator -- (Complex & c, int){
Complex temp(c);
--c.real;
return temp;
}
ostream & operator << (ostream & o, const Complex & c){
o << c.real;
return o;
}
int main(){
Complex d(5, 4);
cout << (d++) << ",";
cout << d << ",";
cout << (++d) << ",";
cout << d << endl;
cout << (d--) << ",";
cout << d << ",";
cout << (--d) << ",";
cout << d << endl;
//输出结果应为:
//5,6,7,7
//7,6,5,5
return 0;
}
其他注意事项:
- c++不允许定义新的运算符。
- 重载后的运算符含义应符合日常习惯。
- 运算符重载不改变运算符的优先级。
- 一下运算符不可被重载:
..*::?:sizeof - 重载运算符
()[]->=时,运算符重载函数必须声明为类的成员函数。
c++学习笔记_4的更多相关文章
- servlet学习笔记_4
一.response.1.response.characterEncoding和response.setContentType("text/html;charset=UTF-8") ...
- Java编程思想学习笔记_4(异常机制,容器)
一.finally语句注意的细节: 当涉及到break和continue语句的时候,finally字句也会得到执行. public class Test7 { public static void m ...
- PythonI/O进阶学习笔记_4.自定义序列类(序列基类继承关系/可切片对象/推导式)
前言: 本文代码基于python3 Content: 1.python中的序列类分类 2. python序列中abc基类继承关系 3. 由list的extend等方法来看序列类的一些特定方法 4. l ...
- Flink学习笔记:Flink API 通用基本概念
本文为<Flink大数据项目实战>学习笔记,想通过视频系统学习Flink这个最火爆的大数据计算框架的同学,推荐学习课程: Flink大数据项目实战:http://t.cn/EJtKhaz ...
- flink学习笔记-各种Time
说明:本文为<Flink大数据项目实战>学习笔记,想通过视频系统学习Flink这个最火爆的大数据计算框架的同学,推荐学习课程: Flink大数据项目实战:http://t.cn/EJtKh ...
- matlab学习笔记8 基本绘图命令-LineSpec线条设定
一起来学matlab-matlab学习笔记8 基本绘图命令_4 LineSpec线条设定 觉得有用的话,欢迎一起讨论相互学习~Follow Me 绘图函数接受线条设定作为参数并相应地修改生成的图形.您 ...
- matlab学习笔记4--导入和导出Internet数据
一起来学matlab-matlab学习笔记4 数据导入和导出_4 导入和导出Internet数据 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考书籍 <matlab 程序设计与综合 ...
- Learning hard 学习笔记
第一章 你真的了解C#吗 1.什么是C#, 微软公司,面向对象,运行于.NET Framework之上, 2.C#能编写哪些应用程序, Windows应用桌面程序,Web应用程序,Web服务, 3.什 ...
- js学习笔记:webpack基础入门(一)
之前听说过webpack,今天想正式的接触一下,先跟着webpack的官方用户指南走: 在这里有: 如何安装webpack 如何使用webpack 如何使用loader 如何使用webpack的开发者 ...
随机推荐
- HTTP_1_Web及网络基础
Web使用一种HTTP(HyperText TransFer Protocol,超文本协议)的协议作为规范,完成从客户端到服务器等一系列运作流程.可见web是建立在HTTP协议上通信的. 通常我们使用 ...
- Tomcat发布War包或者Maven项目
在tomcat的conf目录下面的server.xml中修改如下: Host name="localhost" appBase="webapps" unpac ...
- 0R电阻在PCB布线中对布线畅通的一个小妙用
在PCB布线中,我们都会尽量节约板子空间,将元器件排布的紧密一些,难免会遇到布线不通的时候. 博主下面就来说一个关于0R电阻在PCB布线使之畅通的一个小妙用. 使用0R电阻前 假设我们这个TXD的线周 ...
- spring cloud stream 经验总结
---恢复内容开始--- 基本概念 spring: cloud: stream: kafka: binder: brokers: cloudTest:19092 zk-nodes: cloudTest ...
- leetcode 29 两数相除
问题描述 给定两个整数,被除数 dividend 和除数 divisor.将两数相除,要求不使用乘法.除法和 mod 运算符. 返回被除数 dividend 除以除数 divisor 得到的商. 示例 ...
- 【C++】string::substr函数
形式:s.substr(p, n) 返回一个string,包含字符串s中从p开始的n个字符的拷贝(p的默认值是0,n的默认值是s.size() - p,即不加参数会默认拷贝整个s) int main( ...
- 全球十大OTA 谁能有一席之地?
全球十大OTA 谁能有一席之地? http://www.traveldaily.cn/article/78381/1 2014-03-05 来源:i黑马 随着旅游行业日新月异的发展,在线旅游网站的出现 ...
- 《深入理解Java虚拟机》-(实战)练习修改class文件
这是一篇修改class文件的文章.注释并不完全,要抓住这次练习的目的: boolean在虚拟机中是以何种方式解读的 好的,开始我的表演 1.安装asmtools.jar 2.编写一个java文件,并编 ...
- powerdesign进军(一)--安装破解
目录 资源下载地址 安装powerdesign 破解powerdesign 汉化 总结 IT行业不管是web开发还是客户端开发都需要数据库,因为现在是数据时代能够拥有强大的数据就是行业的王者.目前一些 ...
- 峰回路转:去掉 DbContextPool 后 Windows 上的 .NET Core 版博客表现出色
今天早上,我们修改了博客程序中的1行代码,将 services.AddDbContextPool 改为 services.AddDbContext ,去掉 DbContextPool . 然后奇迹出现 ...