【C++】设计一个Rectangle类,计算周长,面积

设计一个Rectangle类。要求:

//(1)包含两个成员变量m_length和m_width,其默认值为1。
//(2)包含成员函数Perimeter()计算长方形的周长,Area()计算长方形面积。
//(3)包含成员函数SetWidth()和GetWidth()用来设置和得到m_width的值,SetLength()和GetLength()用来设置和得到m_length的值。Set…()函数应验证m_length和m_width均为0.0到20.0之间的浮点数。
//(4)编写主函数,测试Rectangle类。

#include<iostream>
using namespace std;

class Rectangle {
public:
    //Perimeter()计算长方形的周长
    double Perimeter() { return 2 * (m_length + m_width); }
    //Area()计算长方形面积
    double Area() { return m_length * m_width; }

    //SetWidth()和GetWidth()用来设置和得到m_width的值
    void SetWidth(double w) { m_width = w; }
    double GetWidth() { return m_width; }

    //SetLength()和GetLength()用来设置和得到m_length的值
    void SetLength(double l) { m_length = l; }
    double GetLength() { return m_length; }

    //Set_()函数应验证m_length和m_width均为0.0到20.0之间的浮点数。
    bool Set_() {
        bool n = 1;
        if (0 > m_length || 20 < m_length) {
            cout << "length is not in 0.0 - 20.0" << endl;
            n = 0;
        }
        if (0 > m_width || 20 < m_width) {
            cout << "width is not in 0.0 - 20.0" << endl;
            n = 0;
        }
        if (n)
            cout << "length and width are both in 0.0 - 20.0" << endl;
        return n;
    }
private:
    //两个成员变量m_length和m_width,其默认值为1
    double m_length = 1;
    double m_width = 1;
};

int main() {
    Rectangle rec;
    cout << "Length : " << rec.GetLength() << "    " << "Width : " << rec.GetWidth() << endl;
    rec.Set_();
    cout << endl << endl;

    rec.SetLength(5.2);
    rec.SetWidth(4);
    cout << "Length : " << rec.GetLength() << "    " << "Width : " << rec.GetWidth() << endl;
    rec.Set_();
    cout << endl << endl;

    rec.SetWidth(25);
    cout << "Length : " << rec.GetLength() << "    " << "Width : " << rec.GetWidth() << endl;
    rec.Set_();
    cout << endl << endl;

    return 1;
}

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
#include <iostream> #include <string> using namespace std; struct CPoint { int x ; int y ; }; class CRectangle { private: const int id;//常量数据成员 static int total;//静态数据成员 const static string sclass; const static int f=1.0f; CPoint lefttop ; CPoint rightdown ; public: CRectangle( ); CRectangle( CPoint& lt, CPoint& rd ); CPoint GetLefttop() const { return lefttop; } CPoint GetRightdown() const { return rightdown; } void SetLefttop(CPoint & pt) { lefttop=pt; } void SetRightdown(CPoint & pt) { rightdown=pt; } int Getid() const { return id; } static int Gettotal() { return total; } int Area( ) const; int Perimeter( ) const; }; int CRectangle::total=0;//静态数据成员必须在的外部定义(正好一次)。 const string CRectangle::sclass="CRectangle"; CRectangle::CRectangle( ):id(++total) { lefttop.x=0; lefttop.y=0; rightdown.x=1; rightdown.y=1; } CRectangle::CRectangle( CPoint& lt, CPoint& rd ):id(++total) { lefttop = lt ; rightdown = rd ; } int CRectangle::Area( ) const { int wd= rightdown.x - lefttop.x ; int ht= rightdown.y - lefttop.y ; return wd * ht ; } int CRectangle::Perimeter( ) const { int wd= rightdown.x - lefttop.x ; int ht= rightdown.y - lefttop.y ; return 2 * ( wd + ht ) ; } int main() { CPoint lt, rd; cin >> lt.x >> lt.y; cin >> rd.x >> rd.y; CRectangle crt(lt,rd);//调用有参构造函数 CRectangle crt2;//调用默认构造函数 //创建常量对象 const CRectangle crt3(lt,rd); cout<<"当前创建的矩形个数为:"; cout<<CRectangle::Gettotal()<<endl; //返回矩形的左上和右下点 CPoint lt1=crt.GetLefttop(); CPoint lt2=crt.GetRightdown(); //显示矩形的坐标 cout<<crt.Getid()<<"号矩形的坐标是:"<<"("<<lt1.x<<","<<lt1.y<<"), "; cout<<"("<<lt2.x<<","<<lt2.y<<")"<<endl; //显示矩形的面积周长 cout << "Area:"<<crt.Area( )<<endl; cout <<"Perimeter:"<<crt.Perimeter( )<<endl; //修改矩形的左上角点 cout<<"请输入矩形新的左上点坐标:"; cin>> lt.x>>lt.y; crt.SetLefttop(lt); lt1=crt.GetLefttop(); //显示修改后矩形的坐标 cout<<"矩形的坐标是:"<<"("<<lt1.x<<","<<lt1.y<<"), "; cout<<"("<<lt2.x<<","<<lt2.y<<")"<<endl; //显示修改后矩形的面积周长 cout << "Area:"<<crt.Area( )<<endl; cout <<"Perimeter:"<<crt.Perimeter( )<<endl; }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值