|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: C++ 成员函数与友元函数 |
| 4 | +date: 2015-10-31 11:25 |
| 5 | +categories: C++ |
| 6 | +tags: [C++] |
| 7 | +--- |
| 8 | +**在[C++ 运算符重载]一文的基础上,增加两个判断复数是否相等的函数,一个为成员函数,一个为友元函数** |
| 9 | +```cpp |
| 10 | +#include<iostream> |
| 11 | +using namespace std; |
| 12 | +class Complex { //复数类 |
| 13 | + private: //定义私有成员变量 |
| 14 | + double real; //定义实数部 |
| 15 | + double image; //定义虚数部 |
| 16 | + public: |
| 17 | + Complex(void):real(0),image(0) {} //定义参数为空的构造函数 |
| 18 | + Complex(double rp):real(rp),image(0) {} //定义只有实数部的构造函数 |
| 19 | + Complex(double rp,double ip):real(rp),image(ip) {} //定义参数同时制定实数和虚数的构造函数 |
| 20 | + ~Complex() {} //定义析构函数(无特定操作可不写) |
| 21 | + |
| 22 | + Complex operator+(const Complex &x) const; //声明重载运算符+ |
| 23 | + Complex operator-(const Complex &x) const; //声明重载运算符- |
| 24 | + Complex operator*(const Complex &x) const; //声明重载运算符* |
| 25 | + Complex operator/(const Complex &x) const; //声明重载运算符/ |
| 26 | + bool operator==(const Complex &x) const; //声明重载运算符== |
| 27 | + Complex& operator+=(const Complex &x); //声明重载运算符+= |
| 28 | + |
| 29 | + Complex& operator-=(const Complex &x); //声明重载运算符-= |
| 30 | + Complex& operator*=(const Complex &x); //声明重载运算符*= |
| 31 | + |
| 32 | + bool Charge_member(Complex &x); //声明判断复数相等的成员函数 |
| 33 | + friend bool Charge_friend(Complex &x, Complex &y); //声明类外的一个判断相等的函数为友元函数(这个函数不是类的成员函数,是将类外的某个函数定义为自己的友元函数,只有定义为友元函数之后才能访问类的私有变量) |
| 34 | + |
| 35 | + void Print(void) const; //定义类成员输出函数 |
| 36 | +}; |
| 37 | +inline Complex Complex::operator+(const Complex &x) const { //重载运算符的实际函数体 |
| 38 | + return Complex(real + x.real, image + x.image); |
| 39 | +} |
| 40 | +inline Complex Complex::operator-(const Complex &x) const { |
| 41 | + return Complex(real - x.real, image - x.image); |
| 42 | +} |
| 43 | +inline Complex Complex::operator*(const Complex &x) const { |
| 44 | + return Complex(real * x.real - image * x.image, real * x.image + image * x.real); |
| 45 | +} |
| 46 | +Complex Complex::operator/(const Complex &x) const { |
| 47 | + double m; |
| 48 | + m = x.real * x.real + x.image * x.image; |
| 49 | + return Complex((real * x.real + image * x.image) / m, (image * x.real - real * x.image) / m); |
| 50 | +} |
| 51 | +inline bool Complex::operator==(const Complex &x) const { //运算符==判断是否相等,应返回bool类型的值 |
| 52 | + return bool(real == x.real && image == x.image); |
| 53 | +} |
| 54 | +Complex& Complex::operator+=(const Complex &x) { //因为+=的结果是将被加数加在自己的成员函数上,所以返回自身的指针 |
| 55 | + real += x.real; |
| 56 | + image += x.image; |
| 57 | + return *this; |
| 58 | +} |
| 59 | +Complex& Complex::operator-=(const Complex &x) { |
| 60 | + real -= x.real; |
| 61 | + image -= x.image; |
| 62 | + return *this; |
| 63 | +} |
| 64 | +Complex& Complex::operator*=(const Complex &x) { |
| 65 | + real = real * x.real - image * x.image; |
| 66 | + image = real * x.image + image * x.real; |
| 67 | + return *this; |
| 68 | +} |
| 69 | + |
| 70 | +inline bool Complex::Charge_member(Complex &x) { //直接判断 |
| 71 | + return bool(real == x.real && image == x.image); |
| 72 | +} |
| 73 | + |
| 74 | +bool Charge_friend(Complex &x, Complex &y) { //需声明两个复数类的参数 |
| 75 | + return bool(y.real == x.real && y.image == x.image); |
| 76 | +} |
| 77 | + |
| 78 | +void Complex::Print(void) const { //输出函数 |
| 79 | + cout<<"("<<real<<","<<image<<"i)"<<endl; |
| 80 | +} |
| 81 | + |
| 82 | +int main(void) { //测试函数 |
| 83 | + Complex a(3, 5), b(2, 3), c; |
| 84 | + c = a + b * a / b - b; |
| 85 | + c.Print(); |
| 86 | + |
| 87 | + if(a.Charge_member(b)) cout<<"这两个复数是相等的(成员函数)"<<endl; |
| 88 | + else cout<<"这两个复数是不相等的(成员函数)"<<endl; |
| 89 | + |
| 90 | + if(Charge_friend(a,b)) cout<<"这两个复数是相等的(友元函数)"<<endl; |
| 91 | + else cout<<"这两个复数是不相等的(友元函数)"<<endl; |
| 92 | + |
| 93 | + return 0; |
| 94 | +} |
| 95 | +``` |
0 commit comments