实验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;
}
?
?
總結:
這次的類和對象相比之前難了一些,復制構造函數還是不太熟練
轉載于:https://www.cnblogs.com/sjcnb/p/8743546.html
總結
- 上一篇: 接口测试基础之入门篇(待续)
- 下一篇: html读取C#参数问题