实验三
#include <iostream>
using namespace std;
class Space
{
public:
Space();
Space(double x0, double y0);
Space(Space &s0);
~Space();
double area() {return l*w ; }
private:
double l;
double w;
};
Space::Space()
{
cout<<"Constructor(default) is called!"<<endl;
l=0;
w=0;
}
Space::Space(double x0, double y0)
{
cout<<"Constructor is called!"<<endl;
l=x0;
w=y0;
}
Space::Space(Space &s0)
{
cout<<"Copy constructor is called!"<<endl;
l=s0.l;
w=s0.w;
}
Space::~Space()
{
cout<<"Destructor is called!"<<endl;
}
int main()
{
Space s1;
cout<<"S="<<s1.area()<<endl;
double x, y;
cout<<"请输入长和宽:";
cin>>x>>y;
Space s2(x, y);
cout<<"S="<<s2.area()<<endl;
Space s3(s2);
cout<<"S="<<s3.area()<<endl;
return 0;
}

#include<iostream>
using namespace std;
class Complex
{
public:
Complex(double r0,double i0);
Complex(double r0);
void add(Complex &c0);
void show(){cout<<i<<'+'<<r<<'i'<<endl;}
private:
double r;//实部 real
double i;//虚部 imaginary
};
Complex::Complex(double r0,double i0)
{
r=r0;
i=i0;
}
Complex::Complex(double r0)
{
r=r0;
i=0;
}
void Complex::add(Complex &c0)
{
r+=c0.r;
i+=c0.i;
}
int main()
{
Complex c1(3,5);
Complex c2=4.5;
c1.add(c2);
c1.show();
return 0;
}

浙公网安备 33010602011771号