定义一个描述矩形的类Rectangle,包括的数据成员有宽(width)和长(length),并实现如下功能函数;
定义一个描述矩形的类Rectangle,包括的数据成员有宽(width)和长(length),并实现如下功能函数;
(1)矩形对象初始化;
(2)计算矩形周长;
(3)计算矩形面积;
(4)改变矩形大小。 展开
代码如下:
class Recangle{
private:
int width;
int length;
public:
Recangle()
{width = 0; length = 0;};
Recangle(int w,int h)
{width = w; height = h;};
int circumference()
{return 2*(width+height)};
int Area()
{return width * height};
void changeRec(int w, int h)
{width = w; height = h;};
};
C++编程如下
#include <iostream.h>
#include <std.h>
#include <math.h>
int main()
{
int a1=5,a2=6;
int b1=4,b2=5;
int s1,s2;
s1=a1*a2;
s2=b1*b2;
printf("矩形1的面积为:%d;",s1);
printf("矩形2的面积为:%d;",s2);
if(s1>s2)
{
printf("s1>s2");
}
else
{
if(s1<s2)
{
printf("s1<s2");
}
else
{
printf("s1=s2");
}
}
2023-08-15 广告
private:
int width;
int length;
public:
Recangle()
{width = 0; length = 0;};
Recangle(int w,int h)
{width = w; height = h;};
int circumference()
{return 2*(width+height)};
int Area()
{return width * height};
void changeRec(int w, int h)
{width = w; height = h;};
};
广告 您可能关注的内容 |