pfwvan666

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  11 随笔 :: 0 文章 :: 0 评论 :: 1847 阅读

设计一个名为Rectangle的矩形类,这个类包括:两个名为width和height的double数据域,它们分别表示矩形的宽和高。width和height的默认值都为1.该类包括矩形类的无参构造函数(默认构造函数);一个width和height为指定值的矩形构造函数;一个名为getArea( )的函数返回矩形的面积;一个名为getPerimeter( )的函数返回矩形的周长。请实现这个类。编写一个测试程序,创建一个Rectangle对象,从键盘输入矩形的宽和高,然后输出矩形的面积和周长。

输入格式:

3.5 35.9(第一个数表示矩形的宽,第二个数表示矩形的高,中间是空间分隔。)

输出格式:

125.65 (第一行输出矩形的面积)
78.8 (第二行输出矩形的周长)

输入样例:

3.5 35.9
 

输出样例:

125.65
78.8
 
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
 
 
 
 
#include <iostream>
using namespace std;

class Rectangle
{
    double Width;
    double Height;//矩形的宽和高

public:

    void SetWidth(double width);
    void SetHeight(double height);
    double getArea(double width,double height );
    double getPerimeter(double width,double height );

};


void Rectangle::SetWidth(double width)//
{
    Width = width;
}

void Rectangle::SetHeight(double height)
{
    Height = height;
}

//返回矩形的面积
double Rectangle::getArea( double width,double height )
{
        double Area=width*height;
        cout<<Area<<endl;
}
//返回矩形的周长
double Rectangle::getPerimeter(double width,double height )
{
        double Perimeter=2*(width+height);
        cout<<Perimeter<<endl;
}


int main()
{

    Rectangle c;
    double width;
    double height;
    cin >> width >> height;

    c.SetWidth(width);
    c.SetHeight(height);

    c.getArea( width,height );
    c.getPerimeter(width,height );

//可以写个check,因为没有负数的长宽
    return 0;
}

posted on   澎湖湾  阅读(812)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示