1,下面的复数解决方案是否可行?

1,代码示例:

 class Comples
{
public:
int a;
int b;
}; int main()
{
Complex c1 = {, };
Complex c2 = {, };
Complex c3 = c1 + c2; // error: no match for 'operator' in 'c1 + c2' return ;
}

2,重载最深层次的意义在于通过重载可以扩展系统已有的功能;

3,成员变量为公有且没有自定义构造函数的时候,可以通过大括号来分别初始     化成员变量;

2,复数的加法操作编程实验:

1,main.cpp 文件:

 #include <stdio.h>

 class Complex
{
int a;
int b;
public:
Complex(int a = , int b = )
{
this->a = a;
this->b = b;
} int getA()
{
return a;
} int getB()
{
return b;
} friend Complex Add(const Complex& p1, const Complex& p2);
}; Complex Add(const Complex& p1, const Complex& p2)
{
Complex ret; ret.a = p1.a + p2.a;
ret.b = p1.b + p2.b; return ret;
} int main()
{ Complex c1(, );
Complex c2(, );
Complex c3 = Add(c1, c2); // c1 + c2 printf("c3.a = %d, c3.b = %d\n", c3.getA(), c3.getB()); return ;
}

2,输出结果:

  c3.a = 4,  c3.b = 6

3,思考:

1,Add 函数可以解决 Complex 对象相加的问题,但是 Complex 是现实世界中确切存在的复数,并且复数在数学中的地位和普通的实数相同;

2,为什么不能让 + 操作符也支持复数相加呢?

1,因为复数是我们自定义的类,编译器不知道这个类;

2,C++ 中扩展已有的功能可以通过重载来实现;

4,操作符重载:

1,C++ 中的重载能够扩展操作符的功能;

2,操作符的重载以函数的方式进行;

3,本质:

1,用特殊形式的函数扩展操作符的功能;

4,通过 operator 关键字可以定义特殊形式的函数;

5,operator 的本质是通过函数重载操作符;

6,语法:

1,代码示例:

 Type operator Sign(const Type& p1, const Type& p2) // operator 关键字作为特殊形式函数的前缀,它直接告诉编译器,如果有两个 Type 对象做 Sign 运算,则直接调用本函数;
{
Type ret; return ret;
}

2,Sign 为系统中预定义的操作符,如:+,-,*,/等;

3,仅在定义时函数名特殊,调用时和普通函数相同;

5,操作符重载初探编程实验:

1,main.cpp 文件:

 #include <stdio.h>

 class Complex
{
int a;
int b;
public:
Complex(int a = , int b = )
{
this->a = a;
this->b = b;
} int getA()
{
return a;
} int getB()
{
return b;
} friend Complex operator + (const Complex& p1, const Complex& p2);
}; Complex operator + (const Complex& p1, const Complex& p2)
{
Complex ret; ret.a = p1.a + p2.a;
ret.b = p1.b + p2.b; return ret;
} int main()
{ Complex c1(, );
Complex c2(, );
Complex c3 = c1 + c2; // c1 + c2 ==> operator + (c1, c2);用 “+” 来操作两个类,编译器没能力,然后编译器查找有没有用重载的概念扩展操作符的功能,于是找到了 operator +() 这个函数,如果编译器成功匹配相应的参数,则成功调用 operator +() 函数; printf("c3.a = %d, c3.b = %d\n", c3.getA(), c3.getB()); return ;
}

 2,输出结果:

  1,c3.a = 4, c3.b = 6

 3,问题:

  1,这里的解决方案用了友元,现代软件开发不允许;

  2,是否可以提供成员函数代替全局函数,以抛弃友元?

6,可以将操作符重载函数定义为类的成员函数:

1,比全局操作符重载函数少一个参数(左操作数);

1,和全局函数最大的差异;

2,成员函数中隐藏的 this 参数可以充当左操作数的角色;

2,不需要依赖友元就可以完成操作符重载;

3,编译器优先在成员函数中寻找操作符重载函数;

1,一旦在成员函数中找到,就不会去全局找;

4,代码示例:

 class Type
{
public:
Type operator Sign(const Type& p) // 左操作数用 this;
{
Type ret; return ret;
}
};

7,成员函数重载操作符编程实验:

1,main.cpp 文件:

 #include <stdio.h>

 class Complex
{
int a;
int b;
public:
Complex(int a = , int b = )
{
this->a = a;
this->b = b;
} int getA()
{
return a;
} int getB()
{
return b;
} Complex operator + (const Complex& p)
{
Complex ret;
printf("Complex operator + (const Complex& p)\n");
ret.a = this->a + p.a;
ret.b = this->b + p.b; return ret;
} friend Complex operator + (const Complex& p1, const Complex& p2);
}; Complex operator + (const Complex& p1, const Complex& p2)
{
Complex ret;
printf("Complex operator + (const Complex& p1, const Complex& p2)\n");
ret.a = p1.a + p2.a;
ret.b = p1.b + p2.b; return ret;
} int main()
{ Complex c1(, );
Complex c2(, );
Complex c3 = c1 + c2; // c1 + c2 ==> c1.operator + (c2) printf("c3.a = %d, c3.b = %d\n", c3.getA(), c3.getB()); return ;
}

 2,输出结果:

  Complex operator + (const Complex& p)

  c3.a = 4, c3.b = 6

8,小结:

1,操作符重载是 C++ 的强大特性之一;

2,操作符重载的本质是通过函数扩展操作符的功能;

3,operator 关键字是实现操作符重载的关键;

4,操作符重载遵循相同的函数重载规则;

5,全局函数和成员函数都可以实现对操作符的重载;

C++中操作符重载的概念的更多相关文章

  1. 关于C++中操作符重载的疑问 :四个运算符=, -&gt;, [], ()不可以重载为全局函数(友员函数)

    转载自:http://blog.csdn.net/u014610226/article/details/47679323     以下是对C++中不能重载为友元函数的四个运算符进行了详细的分析介绍,需 ...

  2. C++基础学习笔记----第十三课(操作符重载-下)

    本节主要讲使用成员函数重载操作符,包括[],=,(),->四种操作符的重载以及&&和||的问题. 类的成员函数进行操作符重载 基本概念 类的成员函数也可以进行操作符的重载.类的普 ...

  3. C++基础 (4) 第四天 this指针 全局函数和成员函数 友元 操作符重载

    1static强化练习-仓库进货和出货 #define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; c ...

  4. C++中采用操作符重载完善复数类

    1,复数类应该具有的操作: 1,运算:+,-,*,/: 2,比较:==,!=: 3,赋值:=: 4,求模:modulus: (5),完善的复数类操作符重载必不可少: 2,利用操作符重载: 1,统一复数 ...

  5. C#中如何利用操作符重载和转换操作符

    操作符重载 有的编程语言允许一个类型定义操作符应该如何操作类型的实例,比如string类型和int类型都重载了(==)和(+)等操作符,当编译器发现两个int类型的实例使用+操作符的时候,编译器会生成 ...

  6. C++中的操作符重载

    一.什么是操作符重载 操作符重载可以分为两部分:“操作符”和“重载”.说到重载想必都不陌生了吧,这是一种编译时多态,重载实际上可以分为函数重载和操作符重载.运算符重载和函数重载的不同之处在于操作符重载 ...

  7. delphi 中record 的类操作符重载简介

    今天简单介绍一下 delphi 中record 的类操作符重载使用,就是如何 实现 record 之间的简单操作. 关于类操作符重载 ,大家可以看官方的文档. Delphi allows certai ...

  8. Python中的操作符重载

    运算符重载是指在方法中拦截内置的操作----当类的实例出现在内置操作中,Python会自动调用自定义的办法,并且返回自定义方法的操作结果.     类可以重载python的操作符 操作符重载使我们的对 ...

  9. 【C++】C++中的操作符重载

    C++中的操作符重载使得对于类对象的操作更加方便和直观,但是对于各种操作符重载的规则以及语法形式,一直以来都是用到哪一个上stackoverflow上查找,在查找了四五次之后,觉得每次麻烦小总结一下. ...

随机推荐

  1. 8.0.17 MySQL Community Server 二进制手工安装

    8.0.17 MySQL Community Server 二进制手工安装 环境简介 操作系统:Centos 6.10 64位目前版本:8.0.17 MySQL Community Server 二进 ...

  2. vue的transition的name作用

    记录一下今天在vue的transition中遇到的坑 <!DOCTYPE html> <html> <head> <title>Vue中CSS动画原理& ...

  3. SpringBoot配置文件优先级加载顺序

  4. php中文件断点上传怎么实现?

    1.使用PHP的创始人 Rasmus Lerdorf 写的APC扩展模块来实现(http://pecl.php.net/package/apc) APC实现方法: 安装APC,参照官方文档安装,可以使 ...

  5. BZOJ 4245: [ONTAK2015]OR-XOR 贪心 + 位运算

    Description 给定一个长度为n的序列a[1],a[2],...,a[n],请将它划分为m段连续的区间,设第i段的费用c[i]为该段内所有数字的异或和,则总费用为c[1] or c[2] or ...

  6. 【Leetcode】国王挖金矿

    参考该文章 https://www.cnblogs.com/henuliulei/p/10041737.html #include <iostream> #include <cstr ...

  7. HDU 2612 Find a way(双向bfs)

    题目代号:HDU 2612 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2612 Find a way Time Limit: 3000/1000 M ...

  8. prometheus-pushgateway安装

    背景 当prometheus的server与target不在同一网段网络不通,无法直接拉取target数据,需要使用pushgateway作为数据中转点. 弊端 将多个节点数据汇总到 pushgate ...

  9. input 输入框效验

    input 输入框效验 1:只能输入正整数: <el-input v-model.number="formData.projectNum" type='number' min ...

  10. ES6字符串的拓展

    字符串的遍历接口 for...of循环遍历. for (let codePoint of 'foo') { console.log(codePoint) } // "f" // & ...