友元运算符重载函数

news/2024/9/29 3:21:31/

目录

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(); // 输出复数A1A2.print(); // 输出复数A2A3.print(); // 输出复数相加结果A3A4.print(); // 输出复数相减结果A4A5.print(); // 输出复数相乘结果A5A6.print(); // 输出复数相除结果A6return 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;
}

        程序结果如下:

 


http://www.ppmy.cn/news/1531740.html

相关文章

算力共享平台的控制流程,业务流程

目录 控制流程 业务流程 在提供的计算机网络系统结构示意图和描述中,我们可以区分出控制流程和业务流程的组成部分。 控制流程 控制流程主要涉及系统内部的管理、调度和监控操作,以确保系统能够按照预定的规则和策略运行。在这个例子中,控制流程可能包括但不限于以下部分…

等保2.0测评:安全管理体系建设思路

在实际项目中&#xff0c;很多单位都太不重视等保的安全管理方面&#xff0c;也有很多单位比较重视&#xff0c;但是又不知从何入手。因此本文从等保2.0三级基本要求方面的简单介绍下安全管理体系的建设思路。 一、安全管理建设的重要性 这个很多人可能很难理解&#xff0c;尤…

pcs集群表决盘故障导致主机reboot

建议重建fence设备并配置 PCSOracle HA实战安装配置参考 - 墨天轮

WPF入门教学十八 动画入门

WPF&#xff08;Windows Presentation Foundation&#xff09;是微软推出的一种用于创建Windows客户端应用程序的用户界面框架。WPF 提供了丰富的动画支持&#xff0c;可以通过XAML或者代码来实现各种动画效果。以下是一个简单的WPF动画入门教学&#xff0c;我们将使用XAML来创…

ubuntu 安裝 Poetry 示例

ubuntu 安裝 Poetry 示例 一、前言 poetry 是一个命令行工具&#xff0c;安装之后就可以使用 poetry 指令。可以将其安装全局环境或者是虚拟环境&#xff0c;我推荐安装在全局环境&#xff0c;这样在后面使用时不需要单独激活虚拟环境。 &#xff08;1&#xff09;安装 Poet…

[单master节点k8s部署]23.构建EFK日志收集平台(二)

部署elasticsearch集群 已经完成的工作&#xff1a;创建存储。首先配置了nfs存储提供商&#xff08;nfs-deployment.yaml&#xff09;&#xff0c;然后通过创建存储类&#xff08;storageclass.yaml&#xff09;来将nfs服务器与存储类绑定&#xff1a; [rootmaster 31efk]# c…

CorePress Pro 网站加载慢 WordPress

一般来说是你用了「CorePress天气模块」 解决方案&#xff1a;这个插件从你右侧边栏里删掉就可以了&#xff08;上方的图中已经是删掉后的效果了&#xff09; 寻找加载时间长的原因&#xff1a; 谷歌浏览器F12->网络->打开录制->ShiftF5 得出结论&#xff1a;和风天气…

【Linux】初始进程

目录 基本概念 PCB task_struct task_struct内容分类 组织进程 查看进程 查看正在运行的进程信息 获取pid和ppid 创建子进程 基本概念 一个已经加载到内存中的程序&#xff0c;叫做进程&#xff0c;正在运行的程序&#xff0c;叫做进程&#xff0c;进程是担当分配系统…