#include <iostream>
#include<cmath>
using namespace std;//蓝多多作业
class Complex
{
private:
double real;
double imag;
public:
Complex()//无参构造函数
{real = 0; imag = 0;}
Complex(double a, double b)//有参构造函数
{ real = a; imag = b; }
~Complex()//析构函数
{}
Complex(const Complex &p)//深拷贝构造函数
{
real = p.real; imag = p.imag;
}
void output();//声明一个输出函数
double size();//声明一个求模函数
Complex addition(Complex &c1);//声明一个加法函数
};
double Complex::size()
{
double c;
c = sqrt(real*real + imag * imag);
return c;
}
void Complex::output()
{
cout << real << "+" << "(" << imag << ")" << "i" << endl;
}
Complex Complex::addition(Complex &c1)
{
real = c1.real + real;
imag = c1.imag + imag;
return *this;
}
int main()
{
Complex c1(3, 4), c2(5, -10), c3;
c3 = c1.addition(c2);//求c1和c2的和并赋给c3
cout << "c3=";
c3.output();
cout << "c3的模是" << c3.size() << endl;
system("pause");
return 0;
}
定义一个复数类(面向对象程序设计)
猜你喜欢
转载自blog.csdn.net/qq_43554335/article/details/105583511
今日推荐
周排行