最近在学习iphone开发,上着培训班,要从C,C++学起。虽然之前学校里已经学习过这两种语言。但是最近在学习的过程中,我发现自己已经忘记了蛮多知识点了。记得当时学C++ 的时候,觉得运算符重载比较恶心,没好好看它,今天在做C++的作业时,碰到它了,我就头疼。果然一开始写得很不顺手,于是拿着“谭哥”的书,仔仔细细地把运算符重载那章看了一遍。于是写下下面这些东西。(这个是个作业噢~学习C++的童鞋都要写的噢~)如果有不对的地方,希望指出。
---------------------------------------------------complex.h文件---------------------------------------------------
#ifndef COMPLEX_HEADER
#define COMPLEX_HEADER
#include<iostream>
using namespace std;
class Complex
{
public:
Complex();
Complex(double r);
Complex(double r,double i);
friend const Complex operator +(const Complex &c1,const Complex &c2);
friend const Complex operator -(const Complex &c1,const Complex &c2);
friend const Complex operator *(const Complex &c1,const Complex &c2);
friend const Complex operator /(const Complex &c1,const Complex &c2);
friend Complex operator -=(Complex &c1,Complex &c2);
friend Complex operator --(Complex &c1);
friend Complex operator *=(Complex &c1,Complex &c2);
friend Complex operator /=(Complex &c1,Complex &c2);
friend bool operator ==(Complex &c1,Complex &c2);
friend bool operator !=(Complex &c1,Complex &c2);
friend istream& operator >>(istream &in,Complex &c);
void show();
private:
double real;
double imag;
};
#endif
--------------------------------------------------complex.cpp文件--------------------------------------------------
#include"complex.h"
//无参数的构造函数
Complex::Complex()
{
real = 0;
imag = 0;
}
//带一个参数的构造函数
Complex::Complex(double r)
{
real = r;
imag = 0;
}
//带两个参数的构造函数
Complex::Complex(double r,double i)
{
real = r;
imag = i;
}
//“+”运算符重载函数
const Complex operator +(const Complex &c1,const Complex &c2)
{
return Complex(c1.real+c2.real,c1.imag+c2.imag);
}
//“-”运算符重载函数
const Complex operator - (const Complex &c1,const Complex &c2)
{
return Complex(c1.real-c2.real,c1.imag-c2.imag);
}
//“*”运算符重载函数
const Complex operator * (const Complex &c1,const Complex &c2)
{
return Complex(c1.real*c2.real-c1.imag*c2.imag,c1.real*c2.imag+c1.imag*c2.real);
}
//“/”运算符重载函数
const Complex operator / (const Complex &c1,const Complex &c2)
{
if((c2.real == 0) && (c2.imag == 0))
{
exit(1);
}
double den = c2.real*c2.real+c2.imag*c2.imag;
return Complex((c1.real * c2.real + c1.imag * c2.imag) /den ,(c1.imag * c2.real -c1.real * c2.imag) /den);
}
//“>>”运算符重载函数
istream& operator >>(istream &in,Complex &c)
{
cout<<"请输入一个复数:";
in>>c.real>>c.imag;
if(!in)
{
Complex(0,0);
}
return in;
}
//“--”运算符重载函数
Complex operator --(Complex &c1)
{
return c1=Complex(c1.real-1,c1.imag-1);
}
//“-=”运算符重载函数
Complex operator -=(Complex &c1,Complex &c2)
{
return c1=Complex(c1.real-c2.real,c1.imag-c2.imag);
}
//在“-”重载的基础上,“--”运算符重载函数
Complex operator --(Complex &c1)
{
return c1=c1-Complex(1,1);
}
//在“-”重载的基础上,“-=”运算符重载函数
Complex operator -=(Complex &c1,Complex &c2)
{
return c1=c1-c2;
}
//“*=”运算符重载函数
Complex operator *=(Complex &c1,Complex &c2)
{
return c1=Complex(c1.real*c2.real-c1.imag*c2.imag,c1.real*c2.imag+c1.imag*c2.real);
}
//“/=”运算符重载函数
Complex operator /=(Complex &c1,Complex &c2)
{
if((c2.real == 0) && (c2.imag == 0))
{
exit(1);
}
double den = c2.real*c2.real+c2.imag*c2.imag;
return c1=Complex((c1.real * c2.real + c1.imag * c2.imag) /den ,(c1.imag * c2.real -c1.real * c2.imag) /den);
}
//“==”运算符重载函数,复数相等
bool operator ==(Complex &c1,Complex &c2)
{
if(c1.real==c2.real && c1.imag==c2.imag)
{
return true;
}
return false;
}
//“!=”运算符重载函数,复数相等
bool operator !=(Complex &c1,Complex &c2)
{
if(c1.real!=c2.real && c1.imag!=c2.imag)
{
return true;
}
return false;
}
//复数显示函数
void Complex::show()
{
cout<< real << (imag>=0?"+":"") << imag << "i"<<endl;
}
------------------------------------------------complexTest.cpp文件------------------------------------------------
#include"complex.h"
int main(void)
{
int i;
Complex c1(4),c2(2),c3(1,3),c4(4,6),c5(8,3),c6(7,2),c7(8,9),c8(3,3),c9(3,3);
cout<<"-----------------------------"<<endl;
cout<<"c1:";
c1.show();
cout<<"c2:";
c2.show();
c1-=c2;
cout<<"c1-=c2: ";
c1.show();
cout<<"-----------------------------"<<endl;
cout<<"c3:";
c3.show();
cout<<"c4:";
c4.show();
c3*=c4;
cout<<"c3*=c4: ";
c3.show();
cout<<"-----------------------------"<<endl;
cout<<"c5:";
c5.show();
cout<<"c6:";
c6.show();
c5/=c6;
cout<<"c5/=c6: ";
c5.show();
cout<<"-----------------------------"<<endl;
cout<<"c5:";
c5.show();
cout<<"c6:";
c6.show();
if(c5==c6)
{
cout<<"c5与c6相等!"<<endl;
}
else
{
cout<<"c5与c6不相等!"<<endl;
}
cout<<"-----------------------------"<<endl;
cout<<"c7:";
c7.show();
c7--;
cout<<"c7--: ";
c7.show();
cout<<"-----------------------------"<<endl;
cout<<"c8:";
c8.show();
cout<<"c9:";
c9.show();
if(c8!=c9)
{
cout<<"c8与c9不相等!"<<endl;
}
else
{
cout<<"c8与c9相等!"<<endl;
}
while(1);
return 0;
}