Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2349164
  • 博文数量: 816
  • 博客积分: 10000
  • 博客等级: 上将
  • 技术积分: 5010
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-17 17:57
文章分类

全部博文(816)

文章存档

2011年(1)

2008年(815)

分类:

2008-12-17 18:08:51

#include
using namespace std;
class complex
{
private:
      double real;
      double imag;
   public:
      complex(double r=0.0,double i=0.0){real=r;imag=i;}
      complex operator+ (complex c2);
      complex operator- (complex c2);
      complex operator+ ();
      complex operator- ();
      void display();  
   
};
complex complex::operator+(complex c2)
{
   return complex(real+c2.real,imag+c2.imag);
}
complex complex::operator-(complex c2)
{
   return complex(real-c2.real,imag-c2.imag);
}
complex complex::operator+()
{
   return complex(+real,+imag);
}
complex complex::operator-()
{
   return complex(-real,-imag);
}
void complex::display(){
   cout<<"("<}
int main(){
  complex c1(1,2),c2(3,4),c3;
   cout<<"c1=";c1.display();
   cout<<"c2=";c2.display();
   c3=c1+c2;
   cout<<"c3=c1+c2=";c3.display();
   c3=c1-c2;
   cout<<"c3=c1-c2=";c3.display();
   c1=-c1;
   cout<<"-c1=";c1.display();
   return 0;
}

--------------------next---------------------

阅读(1767) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~