1. 非运算符重载实现复数的加法运算 //非运算符重载实现复数的加法运算 //complex.h #include #ifndef COMPLEX_H #define COMPLEX_H class complex { public: complex(); doublegetreal(); doublegetimag(); voidsetreal(double); voidsetimag(double); void display(); private: double real; double image; }; #endif //complex.cpp #include #include"complex.h" complex::complex() { real = 0; image = 0; } void complex::setreal(double r) { real = r; }
void complex::setimag(double i) { image = i; }
double complex::getreal() { return real; } double complex::getimag() { return image; } void complex::display() { cout<<"the result is\n"<} //test.cpp #include #include"complex.h" complex add(complex comp1,complex comp2); complex sub(complex comp1,complex comp2); void main() { complex c1,c2,c3,c4; c1.setreal(2.0); c1.setimag(3.0); c2.setreal(1.0); c2.setimag(4.0); c3 = add(c1,c2); cout<<"c3 = c1 + c2 , "; c3.display(); c4 = sub(c1,c2); cout<<"c4 = c1 - c2 ,"; c4.display(); } complex add(complex comp1,complex comp2) { complex temp; temp.setreal(comp1.getreal() + comp2.getreal()); temp.setimag(comp1.getimag() + comp2.getimag()); return temp; } complex sub(complex comp1,complex comp2) { complex temp; temp.setreal(comp1.getreal() - comp2.getreal()); temp.setimag(comp1.getimag() - comp2.getimag()); return temp; }
2. 运算符重载作为类的成员函数
//运算符重载作为类的成员函数 //complex.h #include #ifndef COMPLEX_H #define COMPLEX_H class complex { public: complex(double = 0.0,double = 0.0); complex operator+ (const complex &); complex operator- (const complex &); complex& complex::operator= (const complex &); void display(); private: double real; double image; }; #endif //complex.cpp #include #include"complex.h" complex::complex(double r,doublei) { real = r; image = i; } complex complex::operator+(const complex &c2)//定义重载运算符函数 { complex c; c.real = real + c2.real; c.image = image + c2.image; return c; } complex complex::operator-(const complex &c2)//定义重载运算符函数 { complex c; c.real = real - c2.real; c.image = image - c2.image; return c; } complex& complex::operator= (const complex &right) { real = right.real; image = right.image; return *this; } void complex::display() { cout<<"the result is\n"<} //test.cpp #include #include"complex.h" double main( ) { complex c3,c4,c1(3.0,4.0),c2(5.0,1.0); cout<<"c1="; c1.display(); cout<<"c2="; c2.display(); c3=c1+c2; //运算符+用于复数运算 cout<<"c1+c2=" 3. 运算符重载作为类的友元函数 //运算符重载作为类的友元函数 //complex.h #include #ifndef COMPLEX_H #define COMPLEX_H class complex { friend complex operator +(complex &c1,complex &c2);//重载函数作为类的友元函数 friend complex operator -(complex &c1,complex &c2);//重载函数作为类的友元函数 public: complex(double r= 0.0 ,double i = 0.0); void display(); private: double real; double image; }; #endif //complex.cpp #include #include"complex.h" complex::complex(double r,doublei) { real=r; image=i; } void complex::display() { cout<<"the result is "<} complex operator +(complex &c1,complex &c2) { complex c3; c3.real=c1.real + c2.real ; c3.image = c1.image +c2.image ; return c3; } complex operator -(complex &c1,complex &c2) { complex c3; c3.real=c1.real - c2.real ; c3.image = c1.image -c2.image ; return c3; } //test.cpp #include #include"complex.h" int main() { complex c1(2.0,1.0),c2(1.0,3.0),c3,c4; cout<<"c1="; c1.display(); cout<<"c2="; c2.display(); c3=c1+c2; c4=c1-c2; c3.display(); c4.display(); return 0; } 4.运算符重载作为类的非成员非友元函数 //运算符重载作为类的非成员非友元函数 //complex.h