#include
using namespace std;
class cfx
{
public://私有成员不能在类外访问,而公有成员可以
float lenth,width;
public:
cfx(float a,float b){lenth=a;width=b;}
float len(){return 2*(lenth+width);}
float area();
cfx(cfx & p){lenth=p.lenth;width=p.width;}
};
inline float cfx::area(){return lenth*width;}
void main()
{
cfx A(3.0,1.5);
cfx B(A);
cout<<“对象A的周长为:”<<A.len()<<endl;
cout<<“对象A的面积为:”<<A.area()<<endl;
cout<<“对象B的周长为:”<<B.len()<<endl;
cout<<“对象B的面积为:”<<B.area()<<endl;
cout<<“长为:”<<A.lenth<<endl;
cout<<“宽为:”<<A.width<<endl;
}
运用类计算长方型的周长和面积
最新推荐文章于 2021-03-10 16:20:34 发布