C++第30课--操作符重载的概念

本文学习自 狄泰软件学院 唐佐林老师的 C++课程


实验1:通过实现 Add() 实现复数相加
实验2: 操作符重载实验
实验3:将操作符重载定义为类的成员函数,编译器优先在成员函数中寻找操作符重载函数


C++第30课--操作符重载的概念

实验1:通过实现 Add() 实现复数相加

#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;
}


mhr@ubuntu:~/work/c++$ 
mhr@ubuntu:~/work/c++$ g++ 30-1.cpp
mhr@ubuntu:~/work/c++$ ./a.out 
c3.a = 4, c3.b = 6
mhr@ubuntu:~/work/c++$ 

C++第30课--操作符重载的概念

C++第30课--操作符重载的概念

C++第30课--操作符重载的概念

实验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 = c1 + c2; // operator + (c1, c2)
    
    printf("c3.a = %d, c3.b = %d\n", c3.getA(), c3.getB());
    
    return 0;
}

mhr@ubuntu:~/work/c++$ 
mhr@ubuntu:~/work/c++$ g++ 30-2.cpp
mhr@ubuntu:~/work/c++$ 
mhr@ubuntu:~/work/c++$ ./a.out 
c3.a = 4, c3.b = 6
mhr@ubuntu:~/work/c++$ 
mhr@ubuntu:~/work/c++$ 

C++第30课--操作符重载的概念

实验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;
    }
    
    /*
    参数是右操作数
    左操作数为 this 参数,当前对象
	*/
    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;
}

mhr@ubuntu:~/work/c++$ 
mhr@ubuntu:~/work/c++$ g++ 30-3.cpp
mhr@ubuntu:~/work/c++$ ./a.out 
Complex operator + (const Complex& p)
c3.a = 4, c3.b = 6
mhr@ubuntu:~/work/c++$ 

C++第30课--操作符重载的概念

C++第30课--操作符重载的概念C++第30课--操作符重载的概念 奶牛养殖场小马 发布了211 篇原创文章 · 获赞 100 · 访问量 8万+ 私信 关注
上一篇:【Excel】实现多列依次排序


下一篇:《个人金融信息保护技术规范》解读