当前位置: 首页 > news >正文

友元运算符重载函数

目录

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.lryc.cn/news/448849.html

相关文章:

  • 从0开始实现es6 promise类
  • XML 编码
  • AI周报(9.22-9.28)
  • 基于RealSense D435相机实现手部姿态重定向
  • js中防抖 debounce 节流 throttle 原理 从0手动实现
  • AIGC: 10 AI转文服务器的搭建过程记录
  • 性能测试1初步使用Jmeter
  • OpenGL ES 绘制一个三角形(2)
  • QT----Creater14.0,qt5.15无法启动调试,Launching GDB Debugger报红
  • 初试React前端框架
  • 华为OD机试真题---手机App防沉迷系统
  • 物流货运托运发货单二联三联打印软件定制 佳易王物流单管理系统操作教程
  • 代码随想录算法训练营| 找树左下角的值 、 路径总和 、 从中序与后序遍历序列构造二叉树
  • 【开源免费】基于SpringBoot+Vue.JS服装销售平台(JAVA毕业设计)
  • 人工智能与自然语言处理发展史
  • 0基础跟德姆(dom)一起学AI 机器学习01-机器学习概述
  • yakit使用教程(一,下载并进行基础配置)
  • 计算机毕业设计电影票购买网站 在线选票选座 场次订票统计 新闻留言搜索/springboot/javaWEB/J2EE/MYSQL数据库/vue前后分离小程序
  • DES、3DES 算法及其应用与安全性分析
  • TypeScript介绍和安装
  • NetworkPolicy访问控制
  • C++面向对象基础
  • 遥感图像变换检测实践上手(TensorRT+UNet)
  • Transformers 引擎,vLLM 引擎,Llama.cpp 引擎,SGLang 引擎,MLX 引擎
  • 牛顿迭代法求解x 的平方根
  • 端口隔离配置的实验
  • 洛谷 P10456 The Pilots Brothers‘ refrigerator
  • windows+vscode+arm-gcc+openocd+daplink开发arm单片机程序
  • Mysql梳理10——使用SQL99实现7中JOIN操作
  • 24.9.27学习笔记