首页 > 其他分享 >友元运算符重载函数

友元运算符重载函数

时间:2024-09-28 21:47:35浏览次数:3  
标签:友元 real temp imag 运算符 Complex 重载

目录

1.定义友元运算符重载函数的语法形式

2.双目运算符重载

3.单目运算符重载


1.定义友元运算符重载函数的语法形式

        (1)在类的内部,定义友元运算符重载函数的格式如下:

friend 函数类型 operator 运算符(形参表)
{
    函数体
}

        (2) 在类中,声明友元运算符重载函数原型的格式如下:

class X
{
    ...
    firend 函数类型 operator 运算符(形参表);
    ...
};

        (3) 在类外,定义友元运算符重载函数的格式如下:

函数类型 operator 运算符(形参表)
{
    函数体
}

        若友元运算符重载函数重载的是双目运算符,则参数表中有两个操作数;若重载的是单目运算符,则参数表中只有一个操作数。下面予以介绍。

2.双目运算符重载

        双目运算符有两个操作数,通常在运算符的左右两侧,例如3 + 5,24 > 12 等。下面是用友元运算符重载函数进行复数运算的例子。

#include <iostream>

using namespace std;

class Complex
{
public:
    Complex(double r = 0, double i = 0);
    void print();
    friend Complex operator+(Complex& a, Complex& b); // 声明运算符 + 重载函数
    friend Complex operator-(Complex& a, Complex& b); // 声明运算符 - 重载函数
    friend Complex operator*(Complex& a, Complex& b); // 声明运算符 * 重载函数
    friend Complex operator/(Complex& a, Complex& b); // 声明运算符 / 重载函数
private:
    double real; // 复数实部
    double imag; // 复数虚部
};

Complex::Complex(double r, double i) // 构造函数
{
    real = r;
    imag = i;
}

Complex operator+(Complex& a, Complex& b)
{
    Complex temp;
    temp.real = a.real + b.real;
    temp.imag = a.imag + b.imag;
    return temp;
}

Complex operator-(Complex& a, Complex& b)
{
    Complex temp;
    temp.real = a.real - b.real;
    temp.imag = a.imag - b.imag;
    return temp;
}

Complex operator*(Complex& a, Complex& b)
{
    Complex temp;
    temp.real = a.real * b.real - a.imag * b.imag;
    temp.imag = a.real * b.imag + a.imag * b.real;
    return temp;
}

Complex operator/(Complex& a, Complex& b)
{
    Complex temp;
    double t;
    t = 1 / (b.real * b.real + b.imag * b.imag);
    temp.real = (a.real * b.real + a.imag * b.imag) * t;
    temp.imag = (b.real * a.imag - a.real * b.imag) * t;
    return temp;
}

void Complex::print()
{
    cout << real;
    if (imag > 0)
        cout << "+";
    if (imag != 0)
        cout << imag << 'i' << endl;
}

int main()
{
    Complex A1(2.3, 4.6), A2(3.6, 2.8), A3, A4, A5, A6; // 定义6个Complex类的对象
    A3 = A1 + A2; // 复数相加
    A4 = A1 - A2; // 复数相减
    A5 = A1 * A2; // 复数相乘
    A6 = A1 / A2; // 复数相除

    A1.print(); // 输出复数A1
    A2.print(); // 输出复数A2
    A3.print(); // 输出复数相加结果A3
    A4.print(); // 输出复数相减结果A4
    A5.print(); // 输出复数相乘结果A5
    A6.print(); // 输出复数相除结果A6

    return 0;
}

        程序运行结果如下:

3.单目运算符重载

        单目运算符只有一个操作数,如-a, &b, !c, ++p等。

        以下是用友元函数重载单目运算符“-”。

#include <iostream>

using namespace std;

class Coord
{
public:
    Coord(int x1 = 0, int y1 = 0)
    {
        x = x1;
        y = y1;
    }
    friend Coord operator-(Coord &obj); // 声明单目运算符 - 重载函数
    void print();
private:
    int x, y;
};

Coord operator-(Coord &obj) // 定义单目运算符 - 重载函数
{
    obj.x = -obj.x;
    obj.y = -obj.y;
    return obj;
}

void Coord::print()
{
    cout << "x = " << x << ", y = " << y << endl;
}

int main()
{
    Coord ob1(50, 60), ob2;
    ob1.print();
    ob2 = - ob1;
    ob2.print();
    return 0;
}

        程序结果如下:

        用友元函数重载单目运算符“++”。

#include <iostream>

using namespace std;

class Coord
{
public:
    Coord(int i = 0, int j = 0)
    {
        x = i;
        y = j;
    }
    friend Coord operator++(Coord &op) // 定义单目运算符 ++ 重载函数
    {                                  // 采用对象引用作为函数参数
        ++op.x;
        ++op.y;
        return op;
    }
    void print()
    {
        cout << "x = " << x << ", y = " << y << endl;
    }
private:
    int x, y;
};


int main()
{
    Coord ob(10, 20);
    ob.print();
    ++ob;
    ob.print();
    return 0;
}

        程序结果如下:

 

标签:友元,real,temp,imag,运算符,Complex,重载
From: https://blog.csdn.net/2301_80391227/article/details/142617275

相关文章

  • java字符串连接和运算符优先级
    源代码:publicclassEnumTest{publicstaticvoidmain(String[]args){intx=100;inty=200;System.out.println("x+y="+y+x+y);System.out.println(x+y+"=x+y");}}程序输出:x+y=200100200300=x......
  • C++友元和运算符重载
    目录一.友元friend1.1概念1.2友元函数1.3友元类1.4友元成员函数二.运算符重载2.1概念2.2成员函数运算符重载2.3成员函数运算符重载2.4特殊运算符重载2.4.1赋值运算符重载2.4.2类型转换运算符重载2.5注意事项三、std::string字符串类(熟悉)一.友元......
  • Java编程基础(基本语法==>运算符)
    文章目录一、基本语法①注释②标识符③关键字④常量二、变量①变量的定义②基本数据类型③基本数据类型的级别与数据转换三、运算符①算数运算符②赋值运算符(=)③关系运算符④逻辑运算符⑤三目运算符⑥位运算符⑦运算符优先级总结提示:以下是本篇文章正文内容,下面......
  • Day4 C++(运算符重载,模板与容器)(友元函数,运算符重载,赋值运算符,string字符串类,模板)
    1.友元friend1.1概念(掌握)定义:类实现了数据的隐藏与封装,类的数据成员一般定义为私有成员,仅能通过类的成员函数才能读写。如果数据成员定义为公共的,则又破坏了封装性。但是某些情况下,需要频繁读写类的成员,特别是在对某些成员函数多次调用时,由于参数传递、类型检查和安全......
  • C++ Practical-2 day2 运算符重载之时钟类++运算符
    系列文章目录点击直达——文章总目录文章目录系列文章目录C++Practical-2day2运算符重载之时钟类++运算符Overview1.时间类重载后缀`++`运算符来递增时间1.1.解释1.2.注意事项2.如何确保时间递增操作在多线程环境中是线程安全的?关于作者C++Practical-2day......
  • 运算符
    短路运算publicclassOperator02{publicstaticvoidmain(String[]args){//短路运算//c<4false,不执行后面半句(c++<4),c还是5没有自增intc=5;booleand=(c<4)&&(c++<4);System.out.println(d);Syst......
  • 运算符、分支语句
    位操作符:可以直接操作二进制数位的内容;~是一个单目位操作符,它可以根据一个数字计算另外一个数字,这两个数字所有二进制数位的内容都不同(按位取反),使用的时候这个符号应该写在数字前面双目位操作符:包括按位与(&),按位或(|)以及按位异或(^),他们都可以把两个数字对应二进制数位的内容做计算......
  • C++ 容器赋值运算符
    ▲《C++Primer》P302assignlist<string>names;vector<constchar*>old_c_str{"娃哈哈","孟菲斯","HelloWold!"};names.assign(old_c_str.cbegin(),old_c_str.cend());//这个可以,拷贝构造for(constautos:names){cout......
  • 实验1 C语言开发环境使用和数据类型、运算符、表达式
    任务1:1#include<stdio.h>23intmain(){4printf("OO\n");5printf("<H><H>\n");6printf("IIII\n");7return0;8} 1#include<stdio.h>23intmain(......
  • PHP中如何使用三元条件运算符
    三元条件运算符简介PHP中的三元条件运算符是一个简化的if-else语句,它允许你在一行代码中完成条件判断和赋值。其基本语法如下:条件?表达式1:表达式2;条件:是一个表达式,其结果将被评估为TRUE或FALSE。表达式1:如果条件为真(TRUE),则计算并返回这个表达式的值。表达式2:如果条件......