1.友元函数实现运算符重载(复数的加减法)
Complex.h:
#pragma once #include <string> using namespace std; class Complex { public: Complex(); Complex(int r, int i); void show(); //运算符重载的实质就是函数重载 //友元函数实现运算符重载 friend Complex operator + (const Complex& x, const Complex& y); friend bool operator == (const Complex & c1, const Complex & c2); Complex Add(const Complex& x)//成员函数 { Complex temp; temp.real = this->real + x.real; temp.image = this->image + x.image; return temp; } private: int real; int image; };
Complex.cpp:
#include "Complex.h" #include <iostream> using namespace std; Complex::Complex() { real = 0; image = 0; } Complex::Complex(int r, int i) { this->real = r; this->image = i; } void Complex::show() { if (image > 0) { if (image == 1) { cout << real << "+i" << endl; } else { cout << real << "+" << image << "i" << endl; } } else if (image < 0) { if (image == -1) { cout << real << "-i" << endl; } else { cout << real << image << "i" << endl; } } else { cout << real << endl; } } Complex operator + (const Complex& x, const Complex& y) { //赋值运算符 Complex temp; temp.real = x.real + y.real; temp.image = x.image + y.image; return temp; } bool operator == (const Complex& c1, const Complex& c2) { if (c1.real == c2.real && c1.image == c2.image) { return true; } else { return false; } }
main.cpp:
#include <iostream> #include "Complex.h" using namespace std; int main() { Complex c1(1, -1), c2(1, -1); c1.show(); c2.show(); //Complex c3 = c1.Add(c2); //c3.show(); //Complex c3 = c1 + c2; //隐式的调用 //c3.show(); Complex c3 = operator+(c1,c2);//显式的调用 c3.show(); if (c1 == c2) { cout << "两个复数相等!" << endl; } else { cout << "两个复数不相等!" << endl; } system("pause"); return 0; }
2.成员函数实现运算符重载
Complex.h:
#pragma once #include <string> using namespace std; class Complex { public: Complex(); Complex(int r, int i); void show(); //运算符重载的实质就是函数重载 //友元函数实现运算符重载 //friend Complex operator + (const Complex& x, const Complex& y); //friend bool operator == (const Complex & c1, const Complex & c2); //成员函数实现运算符的重载 Complex operator + (const Complex& x); bool operator ==(const Complex & x); Complex Add(const Complex& x)//成员函数 { Complex temp; temp.real = this->real + x.real; temp.image = this->image + x.image; return temp; } private: int real; int image; };
Complex.cpp:
#include "Complex.h" #include <iostream> using namespace std; Complex::Complex() { real = 0; image = 0; } Complex::Complex(int r, int i) { this->real = r; this->image = i; } void Complex::show() { if (image > 0) { if (image == 1) { cout << real << "+i" << endl; } else { cout << real << "+" << image << "i" << endl; } } else if (image < 0) { if (image == -1) { cout << real << "-i" << endl; } else { cout << real << image << "i" << endl; } } else { cout << real << endl; } } Complex Complex::operator+(const Complex& x) { Complex temp; temp.real = x.real + this->real; temp.image = x.image + this->image; return temp; } bool Complex::operator==(const Complex& x) { if (this->real == x.real && this->image == x.image) { return true; } return false; }
main.cpp:
#include <iostream> #include "Complex.h" using namespace std; int main() { Complex c1(1, -1), c2(1, -1); c1.show(); c2.show(); //Complex c3 = c1.operator+(c2); //显式的调用 //c3.show(); Complex c3 = c1 + c2; //隐式的调用 c3.show(); if (c1 == c2) { cout << "两个复数相等!" << endl; } else { cout << "两个复数不相等!" << endl; } system("pause"); return 0; }