|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <iostream>
#include <string>
class Complex
{
public:
Complex(); //构造器
Complex(double a,double b); //函数重载
virtual ~Complex();
Complex operator + (Complex &d);
void print();
private:
double real; //实部
double imag; //虚部
};
Complex::Complex()
{
real = 0;
imag = 0;
std::cout << "Complex 构造器++\n";
}
Complex::Complex(double r,double i)
{
real = r;
imag = i;
}
Complex::~Complex()
{
std::cout << "Complex 析构器--\n";
}
void Complex::print()
{
std::cout << "real = " << real << ",imag =" << imag << "\n";
}
Complex Complex::operator+(Complex &d)
{
Complex c;
c.real = real + d.real;
c.imag = imag + d.imag;
return c;
}
int main()
{
Complex a(1,2), b(1,4), c;
c = a + b;
std::cout << "a:";
a.print();
std::cout << "b:";
b.print();
std::cout << "c:";
c.print();
return 0;
} |
|