实验3 类和对象

4-11

#include<iostream>
using namespace std;
class Rectangle
{
public:
void set(double l,double w);
void show();
private:
double L,W;
};
void Rectangle::set(double l,double w)
{
L=l;
W=w;
}
inline void Rectangle::show()
{
cout<<"长为"<<L<<"宽为"<<W<<"面积为"<<L*W;
}
int main()
{
double x,y;
cin>>x>>y;
Rectangle arectangle;
arectangle.set(x,y);
arectangle.show();
return 0;
}

 

 

4-20

#include<iostream>
using namespace std;
class Complex
{
public:
Complex(float x,float y);
Complex(float x);
void add(Complex &c);
void show();
private:
float X,Y;
};
Complex::Complex(float x,float y)
{
X=x;
Y=y;
}
Complex::Complex(float x)
{
X=x;
}
void Complex::add(Complex &c)
{
X=X+c.X;
Y=Y+c.Y;
}
void Complex::show()
{
cout<<X<<"+"<<Y<<"i";
}
int main()
{
Complex c1(3,5);
Complex c2=4.5;
c1.add(c2);
c1.show();
return 0;
}

 

 

总结:

这次的类和对象相比之前难了一些,复制构造函数还是不太熟练

posted @ 2018-04-08 10:43  鸢一纯牛奶  阅读(151)  评论(1编辑  收藏  举报