首页 > 编程语言 >C++ 友元函数实现运算符重载、成员函数实现运算符重载

C++ 友元函数实现运算符重载、成员函数实现运算符重载

时间:2022-11-30 21:34:45浏览次数:41  
标签:real 函数 show int image 运算符 Complex 重载 c2

1.友元函数实现运算符重载(复数的加减法)

Complex.h:

#pragma once
#include <string>
using namespace std;
class Complex
{
public:
    Complex();
    Complex(int r, int i);
    void show();
    //运算符重载的实质就是函数重载
    //友元函数实现运算符重载
    friend Complex operator + (const Complex& x, const Complex& y);
    friend bool operator == (const Complex & c1, const Complex & c2);
    Complex Add(const Complex& x)//成员函数
    {
        Complex temp;
        temp.real = this->real + x.real;
        temp.image = this->image + x.image;
        return temp;
    }
private:
    int real;
    int image;
};

Complex.cpp:

#include "Complex.h"
#include <iostream>
using namespace std;

Complex::Complex()
{
    real = 0;
    image = 0;
}

Complex::Complex(int r, int i)
{
    this->real = r;
    this->image = i;
}

void Complex::show()
{
    if (image > 0) {
        if (image == 1) {
            cout << real << "+i" << endl;
        }
        else
        {
            cout << real << "+" << image << "i" << endl;
        }
    }
    else if (image < 0) {
        if (image == -1) {
            cout << real  << "-i" << endl;
        }
        else
        {
            cout << real << image << "i" << endl;
        }
    }
    else {
        cout << real << endl;
    }
}

Complex operator + (const Complex& x, const Complex& y) { //赋值运算符
    Complex temp;
    temp.real = x.real + y.real;
    temp.image = x.image + y.image;
    return temp;
}
bool operator == (const Complex& c1, const Complex& c2) {
    if (c1.real == c2.real && c1.image == c2.image) {
        return true;
    }
    else {
        return false;
    }
}

main.cpp:

#include <iostream>
#include "Complex.h"

using namespace std;

int main() {
    Complex c1(1, -1), c2(1, -1);
    c1.show();
    c2.show();
    //Complex c3 = c1.Add(c2);
    //c3.show();

    //Complex c3 = c1 + c2; //隐式的调用
    //c3.show();

    Complex c3 = operator+(c1,c2);//显式的调用
    c3.show(); 

    if (c1 == c2) {
        cout << "两个复数相等!" << endl;
    }
    else {
        cout << "两个复数不相等!" << endl;
    }
    system("pause");
    return 0;
}

 2.成员函数实现运算符重载

Complex.h:

#pragma once
#include <string>
using namespace std;
class Complex
{
public:
    Complex();
    Complex(int r, int i);
    void show();
    //运算符重载的实质就是函数重载
    //友元函数实现运算符重载
    //friend Complex operator + (const Complex& x, const Complex& y);
    //friend bool operator == (const Complex & c1, const Complex & c2);

    //成员函数实现运算符的重载
    Complex operator + (const Complex& x);
    bool operator ==(const Complex & x);

    Complex Add(const Complex& x)//成员函数
    {
        Complex temp;
        temp.real = this->real + x.real;
        temp.image = this->image + x.image;
        return temp;
    }
private:
    int real;
    int image;
};

Complex.cpp:

#include "Complex.h"
#include <iostream>
using namespace std;

Complex::Complex()
{
    real = 0;
    image = 0;
}

Complex::Complex(int r, int i)
{
    this->real = r;
    this->image = i;
}

void Complex::show()
{
    if (image > 0) {
        if (image == 1) {
            cout << real << "+i" << endl;
        }
        else
        {
            cout << real << "+" << image << "i" << endl;
        }
    }
    else if (image < 0) {
        if (image == -1) {
            cout << real  << "-i" << endl;
        }
        else
        {
            cout << real << image << "i" << endl;
        }
    }
    else {
        cout << real << endl;
    }
}

Complex Complex::operator+(const Complex& x)
{
    Complex temp;
    temp.real = x.real + this->real;
    temp.image = x.image + this->image;
    return temp;
}

bool Complex::operator==(const Complex& x)
{
    if (this->real == x.real && this->image == x.image) {
        return true;
    }
    return false;
}

main.cpp:

#include <iostream>
#include "Complex.h"

using namespace std;

int main() {
    Complex c1(1, -1), c2(1, -1);
    c1.show();
    c2.show();

    //Complex c3 = c1.operator+(c2); //显式的调用
    //c3.show();

    Complex c3 = c1 + c2; //隐式的调用
    c3.show();

    if (c1 == c2) {
        cout << "两个复数相等!" << endl;
    }
    else {
        cout << "两个复数不相等!" << endl;
    }
    system("pause");
    return 0;
}

标签:real,函数,show,int,image,运算符,Complex,重载,c2
From: https://www.cnblogs.com/smartlearn/p/16939193.html

相关文章

  • MySQL常用函数-24课-2022-11-30
    --====================常用函数==============================--数学运算SELECTABS(-80)--绝对值SELECTCEILING(9.4)--向上取整SELECTFLOOR(9.4)--向下......
  • 分享一个PHP的远程图片抓取函数
    ​​function​​​​​​grabImage(​​​​​​$url​​​​​​,​​​​​​$filename​​​​​​=​​​​​​''​​​​​......
  • Flink-使用flink处理函数以及状态编程实现TopN案例
    7.5应用案例-TopN7.5.1使用ProcessAllWindowFunction场景例如,需要统计最近10秒内最热门的两个url链接,并且每5秒思路使用全窗口函数ProcessAllWindowFunction开......
  • kotlin 函数格式大赏
    funmain(){//一个有引用的lambda表达式valf11:(Int,Int)->Unit={n1,n2->println("f11is${n1*n2}")}//普通函数funf......
  • 高阶函数 内联函数
    参考:(23条消息)“Kotlin“系列:一、Kotlin入门_sweetying520的博客-CSDN博客......
  • iTOP2k1000开发板Makefile基本语法-wildcard函数
    格式:$(wildcardPATTENR)功能:展开指定的目录举例:在/home/topeet/test目录有一个“a.c”的c文件和一个test的文件夹,在/home/topeet/test/test文件夹下有一个......
  • oracle函数、包、触发器
    1、函数:必须有返回值,函数必须用基本数据类型CREATEFUNCTION函数的名字( 参数列表)RETURNTYPENAMEIS      变量的声明BEGIN    过程语句ENDSEL......
  • c语言文件操作函数应用(2) ——学习整理
    表头文件#include<stdio.h>字符读写函数1.      fgetc()(由文件中读取一个字符)【定义函数】intfgetc(FILE*stream);【函数说明】fgetc()从参数stream所指的文件中......
  • c语言文件操作函数应用(3) ——学习整理
    表头文件#include<stdio.h>1.读字符串函数fgets格式:fgets(字符数组名,n,文件指针);功能:从指定的文件中读一个字符串到字符数组中。说明:n表示从文件中读出的字符串不超过n-1......
  • javascript函数的理解
    参考:https://www.liaoxuefeng.com/wiki/1022910821149312/1023021087191360在js里,函数是一等公民。函数可以分配给变量函数可以作为参数传递给其他函数函数可以从其他......