4.17:在4.2的基础上,增加两个判断复数是否相等的函数,一个为成员函数,一个为友元函数
来源:互联网 发布:简述单片机的发展趋势 编辑:程序博客网 时间:2024/05/29 15:52
#include<iostream>using namespace std;class Complex { //复数类 private: //定义私有成员变量 double real; //定义实数部 double image; //定义虚数部 public: Complex(void):real(0),image(0) {} //定义参数为空的构造函数 Complex(double rp):real(rp),image(0) {} //定义只有实数部的构造函数 Complex(double rp,double ip):real(rp),image(ip) {} //定义参数同时制定实数和虚数的构造函数 ~Complex() {} //定义析构函数(无特定操作可不写) Complex operator+(const Complex &x) const; //声明重载运算符+ Complex operator-(const Complex &x) const; //声明重载运算符- Complex operator*(const Complex &x) const; //声明重载运算符* Complex operator/(const Complex &x) const; //声明重载运算符/ bool operator==(const Complex &x) const; //声明重载运算符== Complex& operator+=(const Complex &x); //声明重载运算符+= Complex& operator-=(const Complex &x); //声明重载运算符-= Complex& operator*=(const Complex &x); //声明重载运算符*= bool Charge_member(Complex &x); //声明判断复数相等的成员函数 friend bool Charge_friend(Complex &x, Complex &y); //声明类外的一个判断相等的函数为友元函数(这个函数不是类的成员函数,是将类外的某个函数定义为自己的友元函数,只有定义为友元函数之后才能访问类的私有变量) void Print(void) const; //定义类成员输出函数};inline Complex Complex::operator+(const Complex &x) const { //重载运算符的实际函数体 return Complex(real + x.real, image + x.image);}inline Complex Complex::operator-(const Complex &x) const { return Complex(real - x.real, image - x.image);}inline Complex Complex::operator*(const Complex &x) const { return Complex(real * x.real - image * x.image, real * x.image + image * x.real);}Complex Complex::operator/(const Complex &x) const { double m; m = x.real * x.real + x.image * x.image; return Complex((real * x.real + image * x.image) / m, (image * x.real - real * x.image) / m);}inline bool Complex::operator==(const Complex &x) const { //运算符==判断是否相等,应返回bool类型的值 return bool(real == x.real && image == x.image);}Complex& Complex::operator+=(const Complex &x) { //因为+=的结果是将被加数加在自己的成员函数上,所以返回自身的指针 real += x.real; image += x.image; return *this;}Complex& Complex::operator-=(const Complex &x) { real -= x.real; image -= x.image; return *this;}Complex& Complex::operator*=(const Complex &x) { real = real * x.real - image * x.image; image = real * x.image + image * x.real; return *this;}inline bool Complex::Charge_member(Complex &x) { //直接判断 return bool(real == x.real && image == x.image);}bool Charge_friend(Complex &x, Complex &y) { //需声明两个复数类的参数 return bool(y.real == x.real && y.image == x.image);}void Complex::Print(void) const { //输出函数 cout<<"("<<real<<","<<image<<"i)"<<endl;}int main(void) { //测试函数 Complex a(3, 5), b(2, 3), c; c = a + b * a / b - b; c.Print(); if(a.Charge_member(b)) cout<<"这两个复数是相等的(成员函数)"<<endl; else cout<<"这两个复数是不相等的(成员函数)"<<endl; if(Charge_friend(a,b)) cout<<"这两个复数是相等的(友元函数)"<<endl; else cout<<"这两个复数是不相等的(友元函数)"<<endl; return 0;}
运行结果
0 0
- 4.17:在4.2的基础上,增加两个判断复数是否相等的函数,一个为成员函数,一个为友元函数
- 一个类的成员函数声明为另一个类的友元函数
- 多态:如何判断一个成员函数是否为虚函数(即函数的重写或覆盖)
- 判断一个数是否为素数的函数
- 类成员函数声明为另外一个类的友元
- 判断一个矩阵是否为函数
- 判断一个数是否为素数(函数)
- 判断一个js变量是否为函数
- 一个判断文件夹为空的函数
- C#判断是否为数字的两个函数
- 判断两个对象是否相等的js函数
- 判断两个对象是否相等的js函数
- 写一个函数,判断一个字符串是否为回文联
- t-sql判断一个字符串是否为bigint的函数(全角数字需要判断为不合格)
- 在ORalce 中,怎么判断两个时间段之间是否有交集, 编写的一个函数.
- 判断输入是否为中文的函数
- 判断输入是否为中文的函数
- 判断输入是否为中文的函数
- S3增加防盗链功能和备份策略
- Linux-sed学习笔记
- Linux系统的组成部分及发行版本
- iOS 8 Auto Layout界面自动布局系列4-使用VFL添加布局约束
- Qt数据库(sqlite) — 总结
- 4.17:在4.2的基础上,增加两个判断复数是否相等的函数,一个为成员函数,一个为友元函数
- 深入了解vray材质的属性
- iOS 8 Auto Layout界面自动布局系列5-自身内容尺寸约束、修改约束、布局动画
- 移动web开发笔记(1)
- 数据结构-树-学习笔记
- 软考——计算机组成原理
- Xcode6之后增加pch文件并且常用设置属性
- 函数调用导致堆栈不对称。原因可能是托管的 PInvoke 签名与非托管的目标签名不匹配。
- golang 数组长度与定义长度(容量)