C++解析(17):操作符重载

0.目录

1.操作符重载

2.完善的复数类

3.小结

1.操作符重载

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

示例1——原有的解决方案:

#include <stdio.h>

class Complex
{
    int a;
    int b;
public:
    Complex(int a = 0, int b = 0)
    {
        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(1, 2);
    Complex c2(3, 4);
    Complex c3 = Add(c1, c2); // c1 + c2

    printf("c3.a = %d, c3.b = %d\n", c3.getA(), c3.getB());

    return 0;
}

运行结果为:

[[email protected] Desktop]# g++ test.cpp
[[email protected] Desktop]# ./a.out
c3.a = 4, c3.b = 6

操作符重载:

  • C++中的重载能够扩展操作符的功能
  • 操作符的重载以函数的方式进行
  • 本质——用特殊形式的函数扩展操作符的功能

通过operator关键字可以定义特殊的函数

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

语法:

示例2——使用操作符重载函数代替原有函数:

#include <stdio.h>

class Complex
{
    int a;
    int b;
public:
    Complex(int a = 0, int b = 0)
    {
        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(1, 2);
    Complex c2(3, 4);
    Complex c3 = operator + (c1, c2); // c1 + c2

    printf("c3.a = %d, c3.b = %d\n", c3.getA(), c3.getB());

    return 0;
}

运行结果为:

[[email protected] Desktop]# g++ test.cpp
[[email protected] Desktop]# ./a.out
c3.a = 4, c3.b = 6

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

  • 比全局操作符重载函数少一个参数(左操作符)
  • 不需要依赖友元就可以完成操作符重载
  • 编译器优先在成员函数中寻找操作符重载函数

示例3——不使用友元,而是使用类的成员函数(编译器优先在成员函数中寻找操作符重载函数):

#include <stdio.h>

class Complex
{
    int a;
    int b;
public:
    Complex(int a = 0, int b = 0)
    {
        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(1, 2);
    Complex c2(3, 4);
    Complex c3 = c1 + c2; // c1.operator + (c2)

    printf("c3.a = %d, c3.b = %d\n", c3.getA(), c3.getB());

    return 0;
}

运行结果为:

[[email protected] Desktop]# g++ test.cpp
[[email protected] Desktop]# ./a.out
Complex operator + (const Complex& p)
c3.a = 4, c3.b = 6

2.完善的复数类

复数类应该具有的操作:

利用操作符重载:

  • 统一复数与实数的运算方式
  • 统一复数与实数的比较方式

先实现Complex.h头文件:

// Complex.h
#ifndef _COMPLEX_H_
#define _COMPLEX_H_

class Complex
{
    double a;
    double b;
public:
    Complex(double a = 0, double b = 0);
    double getA();
    double getB();
    double getModulus();

    Complex operator + (const Complex& c);
    Complex operator - (const Complex& c);
    Complex operator * (const Complex& c);
    Complex operator / (const Complex& c);

    bool operator == (const Complex& c);
    bool operator != (const Complex& c);

    Complex& operator = (const Complex& c);
};

#endif

再实现Complex.cpp具体操作:

// Complex.cpp
#include "Complex.h"
#include "math.h"

Complex::Complex(double a, double b)
{
    this->a = a;
    this->b = b;
}

double Complex::getA()
{
    return a;
}

double Complex::getB()
{
    return b;
}

double Complex::getModulus()
{
    return sqrt(a * a + b * b);
}

Complex Complex::operator + (const Complex& c)
{
    double na = a + c.a;
    double nb = b + c.b;
    Complex ret(na, nb);

    return ret;
}

Complex Complex::operator - (const Complex& c)
{
    double na = a - c.a;
    double nb = b - c.b;
    Complex ret(na, nb);

    return ret;
}

Complex Complex::operator * (const Complex& c)
{
    double na = a * c.a - b * c.b;
    double nb = a * c.b + b * c.a;
    Complex ret(na, nb);

    return ret;
}

Complex Complex::operator / (const Complex& c)
{
    double cm = c.a * c.a + c.b * c.b;
    double na = (a * c.a + b * c.b) / cm;
    double nb = (b * c.a - a * c.b) / cm;
    Complex ret(na, nb);

    return ret;
}

bool Complex::operator == (const Complex& c)
{
    return (a == c.a) && (b == c.b);
}

bool Complex::operator != (const Complex& c)
{
    return !(*this == c);
}

Complex& Complex::operator = (const Complex& c)
{
    if( this != &c )
    {
        a = c.a;
        b = c.b;
    }

    return *this;
}

最后实现main函数:

// test.cpp
#include <stdio.h>
#include "Complex.h"

int main()
{
    Complex c1(1, 2);
    Complex c2(3, 6);
    Complex c3 = c2 - c1;
    Complex c4 = c1 * c3;
    Complex c5 = c2 / c1;

    printf("c3.a = %f, c3.b = %f\n", c3.getA(), c3.getB());
    printf("c4.a = %f, c4.b = %f\n", c4.getA(), c4.getB());
    printf("c5.a = %f, c5.b = %f\n", c5.getA(), c5.getB());

    Complex c6(2, 4);

    printf("c3 == c6 : %d\n", c3 == c6);
    printf("c3 != c4 : %d\n", c3 != c4);

    (c3 = c2) = c1;

    printf("c1.a = %f, c1.b = %f\n", c1.getA(), c1.getB());
    printf("c2.a = %f, c2.b = %f\n", c2.getA(), c2.getB());
    printf("c3.a = %f, c3.b = %f\n", c3.getA(), c3.getB());

    return 0;
}

运行结果为:

[[email protected] Desktop]# g++ test.cpp Complex.cpp
[[email protected] Desktop]# ./a.out
c3.a = 2.000000, c3.b = 4.000000
c4.a = -6.000000, c4.b = 8.000000
c5.a = 3.000000, c5.b = 0.000000
c3 == c6 : 1
c3 != c4 : 1
c1.a = 1.000000, c1.b = 2.000000
c2.a = 3.000000, c2.b = 6.000000
c3.a = 1.000000, c3.b = 2.000000

注意事项:

  • C++规定赋值操作符(=)只能重载为成员函数
  • 操作符重载不能改变原操作符的优先级
  • 操作符重载不能改变操作符的个数
  • 操作符重载不应改变操作符的原有语义

3.小结

  • 操作符重载是C++的强大特性之一
  • 操作符重载的本质是通过函数扩展操作符的功能
  • operator关键字是实现操作符重载的关键
  • 操作符重载遵循相同的函数重载规则
  • 全局函数和成员函数都可以实现对操作符的重载
  • 复数的概念可以通过自定义类实现
  • 复数中的运算操作可以通过操作符重载实现
  • 赋值操作符只能通过成员函数实现
  • 操作符重载的本质为函数定义

原文地址:https://www.cnblogs.com/PyLearn/p/10084115.html

时间: 2024-08-05 08:42:57

C++解析(17):操作符重载的相关文章

Kotlin操作符重载:把标准操作加入到任何类中(KAD 17)

作者:Antonio Leiva 时间:Mar 21, 2017 原文链接:https://antonioleiva.com/operator-overload-kotlin/ 就像其他每种语言一样,在Kotlin中,已经预定义了一些操作符执行一定的操作. 最典型的是加(+),减(-),乘(*),除(/),而且还有很多. 类似Java这样的一些语言,这些操作符被限制在一些特定的数量类型上,且没有方法让其他类型数据使用这些操作符. 还有在像Scala这样的其他语言中,由于函数名称可接受任何符号,我

类和对象(17)—— 操作符重载

1.操作符重载的基本语法 所谓重载,就是重新赋予新的含义.函数重载就是对一个已有的函数赋予新的含义,使之实现新功能,因此,一个函数名就可以用来代表不同功能的函数,也就是"一名多用". 运算符也可以重载.实际上,我们已经在不知不觉之中使用了运算符重载.例如,大家都已习惯于用加法运算符"+"对整数.单精度数和双精度数进行加法运算,如5+8,5.8+3.67等,其实计算机对整数.单精度数和双精度数的加法操作过程是很不相同的,但由于C++已经对运算符"+"

lua 14 metatable (类似操作符重载)

转自:http://www.runoob.com/lua/lua-metatables.html 感性认识: "Lua中Metatable这个概念, 国内将他翻译为元表. 元表为重定义Lua中任意一个对象(值)的默认行为提供了一种公开入口. 如同许多OO语言的操作符重载或方法重载. Metatable能够为我们带来非常灵活的编程方式. 具体的说, Lua中每种类型的值都有都有他的默认操作方式, 如, 数字可以做加减乘除等操作, 字符串可以做连接操作, 函数可以做调用操作, 表可以做表项的取值赋值

第二十篇:类操作符重载的相关规定与具体实现示例

前言 有书这么说过,C++设计的最大目的在于允许程序员定义自己的类型,并使它们用起来跟内置类型一样容易和直观.就目前看来,要实现这一点,最核心的莫过于操作符的重载.科学的重载可以让类的使用最大程度地接近内置类型.本文将讨论类操作符重载涉及到的一些原则和具体做法. 实现类操作符重载的两种思路 1. 友元函数法: 将待重载操作符视作非成员函数( 它声明为操作数类型的友元函数 ) 应当采用这种机制重载的运算符有:IO操作符,算数操作符,关系操作符. 2. 成员函数法: 将待重载操作符视作特殊的成员函数

【C/C++】操作符重载

常见问题 Q1. 下列运算符,在C++语言中不能重载的是( ). A. * B. ?: C. :: D. delete Q2. 编写类String的构造函数.析构函数和赋值函数. Q3. 复制构造函数与赋值运算符的区别是什么? Q4. 下述代码的输出结果是什么? 1 #include <iostream> 2 using namespace std; 3 class X 4 { 5 public: 6 X() { cout << "constructor" &l

C++学习笔记-操作符重载

操作符重载(operator overloading)是一种形式的C++多态,C++将操作符重载扩展到用户自定义的类型,如允许使用+将两个自定义的对象相加,编译器将根据操作数的数目和类型决定使用那种加法定义. 要重载操作符,需使用操作符函数,格式如下: operator op (argument-list) op:要重载的操作符 argument-list: 操作数 操作符函数可以是类的成员函数,也可以是友元函数,如果是类成员函数,则第一个操作数是调用对象,它不在argument-list中.

等号操作符重载为什么不能用友元函数大揭秘,以及函数没有等到重载的时候赋值会出现什么现象(盲点)

先看下面程序结果输出什么? 1 #include <iostream> 2 using namespace std; 3 4 class A 5 { 6 private: 7 int x; 8 public: 9 A(){ 10 x=99; 11 cout<<"看看这里是否会被调用"<<endl; 12 } 13 }; 14 15 int main() 16 { 17 A a; 18 a = 7; 19 } 这里面,会报错,显示没有等号匹配现象.只有

C++语言学习(八)——操作符重载

C++语言学习(八)--操作符重载 一.操作符重载基础 1.操作符重载的语法 通过operator关键字可以定义特殊的函数,operator本质是通过函数重载操作符. Type operator operatorname(const Type p1, const Type p2) { Type ret; return ret; } 2.友元函数重载操作符 可以将操作符重载函数声明为友元函数. #include <iostream> using namespace std; class Comp

【学习】操作符重载

2018/8/21 15:16:56 何为操作符重载? 1. 操作符重载就是为了让原有操作符能够作用于用户定义的类型,例如'+'操作符原来只能用于两侧均为数字或字符串的情况,通过操作符重载可以实现如两个时刻相加求和(类似11:20:00 + 1:25:20). 1 class Time(object): 2 def __init__(self,hour=0,minute=0,second=0): 3 self.hour = hour 4 self.minute = minute 5 self.h