C++编程实现:定义矩形类,矩形类具有私有的长和宽属性,具有公有的求长、宽和面积的操作函数,输出长和宽分别为100、50的矩形对象实例的长、宽和面积。要求使用构造函数为属性赋值。
代码如下,如有帮助,请采纳一下,谢谢。
#include <stdio.h>
class Rectangle
{
private:
int mLength; //长
int mWidth; //宽
public:
Rectangle();
Rectangle(int l,int w){mLength = l;mWidth = w;}
int getLength(){return mLength;}
int getWidth(){return mWidth;}
int getMianji(){return mLength * mWidth;}
};
int main()
{
Rectangle rec(100,50);
int mj = rec.getMianji();
printf("面积=%d\n",mj);
getchar();
getchar();
return 0;
}