Complex Class Operator Overloading:
写在开篇:分文件编写的实现运算符的重载,以经典的Complex类为模板,如题~
正文开始@Assassin
目录:
1. 实现Complex类的运算符重载:
实现功能如下:
- 创建数个Complex对象,测试对象的所有成员函数(member functions)。
- 创建数个Complex对象的指针(pointer),以指针测试对象的所有成员函数(member functions)。
代码如下:
2. Complex.cpp:
//Complex.cpp
#include"Complex.h"
void Complex::setReal(double real)
{
Real = real;
}
double Complex:: getReal()
{
return Real;
}
void Complex::setImaginary(double imaginary)
{
Imaginary = imaginary;
}
double Complex::getImaginary()
{
return Imaginary;
}
//
//Complex::Complex()
//{
// Real=0.0;
// Imaginary = 0.0;
//}
//Complex::Complex(double real=0.0,double imaginary=0.0)
//{
// Real = real;
// Imaginary = imaginary;
//}
Complex::Complex(double real , double imaginary ) :Real(real), Imaginary(imaginary)
{
}
Complex& Complex::operator=(const Complex& right) //赋值运算符重载
{
if (*this != right)
{
cout << "Complex::operator=(const Complex & right) is called:" << endl;
Real = right.Real;
Imaginary = right.Imaginary;
}
return *this;
}
bool Complex::operator==(const Complex& right) const//重载==
{
if ((Real == right.Real) && (Imaginary == right.Imaginary))
{
cout << "Complex::operator==(const Complex & right) const is called:" << endl;
return true;
}
else
{
return false;
}
}
bool Complex:: operator!=(const Complex& right) const //重载!=
{
cout << "Complex::operator!=(const Complex & right) const is called:" << endl;
return!(*this == right);
}
Complex:: Complex(const Complex& right) //拷贝构造函数
{
Real = right.Real;
Imaginary = right.Imaginary;
}
Complex Complex::operator+(const Complex& right) const//重载+
{
cout << "Complex::operator+(const Complex & right) const is called:" << endl;
return Complex(Real + right.Real, Imaginary + right.Imaginary);
}
Complex Complex:: operator*(const Complex& right) const//重载*
{
cout << "Complex::operator*(const Complex & right) const is called:" << endl;
return Complex(((Real * right.Real) - (Imaginary * right.Imaginary)), ((Imaginary * right.Real) + (Real * right.Imaginary)));
}
Complex Complex:: operator-(const Complex& right) const//重载—
{
cout << "Complex::operator-(const Complex & right) const is called:" << endl;
return Complex(Real - right.Real, Imaginary - right.Imaginary);
}
Complex Complex:: operator/(const Complex& right) const //重载/
{
cout << "Complex::operator/(const Complex & right) const is called:" << endl;
Complex temp = *this;
double temp1 = right.Real * right.Real + right.Imaginary * right.Imaginary;
double temp2 = Real;
temp.Real = ((Real * right.Real) + (Imaginary * right.Imaginary)) / temp1;
temp.Imaginary = (-(temp2 * right.Imaginary) + (Imaginary * right.Real)) / temp1;
return temp;
}
Complex& Complex::operator+=(const Complex& right) //重载+=
{
cout << "Complex::operator+=(const Complex & right) is called:" << endl;
Real = Real + right.Real;
Imaginary = Imaginary + right.Imaginary;
return *this;
}
Complex& Complex:: operator-=(const Complex& right)//重载-=
{
cout << "Complex::operator-=(const Complex & right) is called:" << endl;
Real = Real - right.Real;
Imaginary = Imaginary - right.Imaginary;
return *this;
}
Complex& Complex:: operator*=(const Complex& right)//重载*=
{
cout << "Complex::operator*=(const Complex & right) is called:" << endl;
double temp = Real;
Real = (Real * right.Real) - (Imaginary * right.Imaginary);
Imaginary = (temp * right.Imaginary) + (Imaginary * right.Real);
return *this;
}
Complex& Complex:: operator/=(const Complex& right)//重载/=
{
cout << "Complex::operator/=(const Complex & right) is called:" << endl;
double temp1 = right.Real * right.Real + right.Imaginary * right.Imaginary;
double temp2 = Real;
Real = ((Real * right.Real) + (Imaginary * right.Imaginary)) / temp1;
Imaginary = (-(temp2 * right.Imaginary) + (Imaginary * right.Real)) / temp1;
return *this;
}
Complex Complex::operator++(int)//后置++
{
cout << "Complex::operator++(int) is called:" << endl;
Complex temp(*this);
Real += 1;
Imaginary += 1;
return temp;
}
Complex& Complex::operator++()//前置++
{
cout << "Complex::operator==() is called:" << endl;
Real += 1;
Imaginary += 1;
return *this;
}
Complex Complex::operator--(int)//后置--
{
cout << "Complex::operator--(int) is called:" << endl;
Complex temp(*this);
Real -= 1;
Imaginary -= 1;
return temp;
}
Complex& Complex::operator--()//前置--
{
cout << "Complex::operator--() is called:" << endl;
Real -= 1;
Imaginary -= 1;
return *this;
}
void Complex:: Print()
{
cout << Real << "+" << Imaginary << "i" << endl;
}
Complex::~Complex()
{
}
istream& operator>> (istream& input, Complex& right)
{
cout << "以两个对象相加(+)来测试重载>>,请依次输入实部,虚部:";
input >> right.Real >> right.Imaginary;
return input;
}
ostream& operator<<(ostream& output, const Complex& right) //输出运算符重载
{
output << right.Real << "+" << right.Imaginary << "i";
return output;
}
3. Complex.h:
//Complex.h
#ifndef _COMPLEX_ //防卫式声明
#define _COMPLEX_
#include <iostream>
using namespace std;
class Complex
{
friend istream &operator>>(istream &input, Complex &right); //输入运算符重载
friend ostream &operator<<(ostream &output, const Complex &right); //输出运算符重载
public:
void setReal(double real);
double getReal();
void setImaginary(double imaginary);
double getImaginary();
/* Complex();
Complex(double real, double imaginary);*/
Complex(double real = 0.0, double imaginary = 0.0);
//赋值操作符重载需要注意:1.自己给自己赋值;2.返回值是引用的形式:3.返回当前对象;4.参数,传引用
Complex &operator=(const Complex &right); //赋值运算符重载
bool operator==(const Complex &right) const; //重载==
bool operator!=(const Complex &right) const; //重载!=
Complex(const Complex &right); //拷贝构造函数
Complex operator+(const Complex &right) const; //重载+
Complex operator*(const Complex &right) const; //重载*
Complex operator-(const Complex &right) const; //重载—
Complex operator/(const Complex &right) const; //重载/
Complex &operator+=(const Complex &right); //重载+=
Complex &operator-=(const Complex &right); //重载-=
Complex &operator*=(const Complex &right); //重载*=
Complex &operator/=(const Complex &right); //重载/=
Complex operator++(int); //后置++
Complex &operator++(); //前置++
Complex operator--(int); //后置--
Complex &operator--(); //前置--
void Print();
~Complex();
private:
double Real;
double Imaginary;
};
istream &operator>>(istream &input, Complex &right);
ostream &operator<<(ostream &output, const Complex &right); //输出运算符重载
#endif
4. main.cpp:
//main.cpp
#include <iostream>
using namespace std;
#include "Complex.h"
void ret_obj()
{
cout << "对象调用成员函数:" << endl;
Complex c1;
c1.Print();
c1.setReal(6.6);
c1.getReal();
c1.setImaginary(9.9);
c1.getImaginary();
c1.Print();
Complex c2(3.3, 4.4);
c2.Print();
Complex c3(1.1, 2.2);
c3.Print();
cout << endl;
c3 = c2;
cout << c3 << endl;
cout << (c3 != c1) << endl;
cout << (c3 == c1) << endl;
cout << c1 + c2 << endl;
cout << c1 - c2 << endl;
cout << c1 * c2 << endl;
cout << c1 / c2 << endl;
cout << endl;
Complex c4(5.5, 6.6);
c4.Print();
cout << (c1 += c4) << endl;
cout << (c1 -= c4) << endl;
cout << (c1 *= c4) << endl;
cout << (c1 /= c4) << endl;
cout << endl;
cout << c1++ << endl;
cout << c2-- << endl;
cout << ++c3 << endl;
cout << --c4 << endl;
cout << endl;
Complex c5;
Complex c6;
cin >> c5 >> c6;
cout << c5 + c6 << endl;
cout << endl;
cout << "**************************************************************" << endl;
}
void ret_ptr()
{
cout << "对象指针调用成员函数:" << endl;
Complex *c1 = new Complex(10, 10);
c1->Print();
c1->setReal(6.6);
c1->getReal();
c1->setImaginary(9.9);
c1->getImaginary();
c1->Print();
Complex t1(6.6, 6.6);
Complex t2(3, 3);
Complex *c2 = new Complex(3.3, 2.2);
c2->Print();
Complex *c3 = new Complex(10.10, 10.10);
c3->Print();
cout << endl;
c3->operator=(t1);
//c3->Print();
cout << *c3 << endl;
cout << (*c3 != *c2) << endl;
cout << (*c3 == *c2) << endl;
cout << *c3 + *c2 << endl;
cout << *c3 - *c2 << endl;
cout << *c3 * *c2 << endl;
cout << *c3 / *c2 << endl;
cout << endl;
Complex c4(5.5, 6.6);
c4.Print();
cout << (*c1 += c4) << endl;
cout << (*c1 -= c4) << endl;
cout << (*c1 *= c4) << endl;
cout << (*c1 /= c4) << endl;
cout << endl;
cout << *c1++ << endl;
cout << *c2-- << endl;
cout << ++(*c3) << endl;
cout << --(*c3) << endl;
cout << endl;
Complex *c5 = new Complex(10, 10);
Complex *c6 = new Complex(10, 10);
cin >> *c5 >> *c6;
cout << *c5 + *c6 << endl;
delete (c1 - 1);
c1 = nullptr;
delete (c2 + 1);
c2 = nullptr;
delete c3;
c3 = nullptr;
delete c5;
c5 = nullptr;
delete c6;
c6 = nullptr;
}
// void info()
// {
// cout << "\n “欢迎使用The Complex Class Operator Overloading";
// cout << "\n\n ---------------------------------------------\n";
// cout << " | \n";
// cout << " | 课程:面向对象程序设计实验课题 \n";
// cout << " | \n";
// cout << " | 题目:Complex类与运算符重载 \n";
// cout << " | \n";
// cout << " | 作者:宁海 \n";
// cout << " | \n";
// cout << " | 学号:???????????? \n";
// cout << " | \n";
// cout << " ---------------------------------------------\n";
// cout << "The followings are my test examples:" << endl
// << endl;
// cout << "************************************************************" << endl;
// }
int main()
{
//info();
ret_obj();
ret_ptr();
system("pause");
return 0;
}
5. 结果展示:
1. 编译代码:
2. 运行结果:
对象调用:
对象指针调用:
写在结尾:
有问题请issue我~~谢谢- - -