其實就是定義一個類,類裡面包含2個元素,一個實數,一個虛數
然後運算子過載 相加的時候 實數與實數相加,虛數與虛數相加
#include<iostream.h>
#include<string.h>
class Complex
{
public:
Complex();
Complex(double a,double b);
friend Complex operator +(Complex a,Complex b);
friend Complex operator -(Complex a,Complex b);
void show();
private:
double realp; //實部
double imagp; //虛部
};
Complex::Complex() //不帶引數的建構函式
realp=0.00;
imagp=0.00;
}
Complex::Complex(double a,double b) //帶引數的建構函式
realp=a;
imagp=b;
Complex operator +(Complex a,Complex b) //加法運算子過載實現
Complex temp;
temp.realp=a.realp+b.realp;
temp.imagp=a.imagp+b.imagp;
return temp;
Complex operator -(Complex a,Complex b) //減法運算子過載實現
temp.realp=a.realp-b.realp;
temp.imagp=a.imagp-b.imagp;
其實就是定義一個類,類裡面包含2個元素,一個實數,一個虛數
然後運算子過載 相加的時候 實數與實數相加,虛數與虛數相加
#include<iostream.h>
#include<string.h>
class Complex
{
public:
Complex();
Complex(double a,double b);
friend Complex operator +(Complex a,Complex b);
friend Complex operator -(Complex a,Complex b);
void show();
private:
double realp; //實部
double imagp; //虛部
};
Complex::Complex() //不帶引數的建構函式
{
realp=0.00;
imagp=0.00;
}
Complex::Complex(double a,double b) //帶引數的建構函式
{
realp=a;
imagp=b;
}
Complex operator +(Complex a,Complex b) //加法運算子過載實現
{
Complex temp;
temp.realp=a.realp+b.realp;
temp.imagp=a.imagp+b.imagp;
return temp;
}
Complex operator -(Complex a,Complex b) //減法運算子過載實現
{
Complex temp;
temp.realp=a.realp-b.realp;
temp.imagp=a.imagp-b.imagp;
return temp;
}