#include<iostream>
using namespace std;
class CComplex
{
public:
CComplex(double,double);
CComplex(CComplex &c);
CComplex add(CComplex &x);
void print();
private:
double real;
double image;
};
CComplex::CComplex(double r=0.0,double i=0.0)
{
real=r;
image=i;
cout<<"调用两个参数的构造函数"<<endl;
}
CComplex::CComplex(CComplex &c)
{
real=c.real;
image=c.image;
cout<<"调用拷贝构造函数"<<endl;
}
void CComplex::print()
{
cout<<"("<<real<<","<<image<<")"<<endl;
}
void f(CComplex n)
{
cout<<"n=";
n.print();
}
CComplex CComplex::add(CComplex &x) (这一句 不懂为何 类名 类名::对象)
{
CComplex y(real+x.real,image+x.image);
return y;
}
int main()
{
CComplex a(3.0,4.0),b(5.6,7.9);
CComplex c(a);
cout<<"a=";
a.print();
cout<<"c=";
c.print();
f(b);
c=a.add(b);
c.print();
system("pause");
}
交互式爱情
weixin_慕斯卡5464189
互换的青春
相关分类