栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c++week4设计并实现一个矩形类Rectangle

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c++week4设计并实现一个矩形类Rectangle

 

 

 

#include 
#include 

// 矩形类Rectangle的定义和实现
// 补足代码
// ×××
class Rectangle
{
public:
    Rectangle();
    Rectangle(int l, int w);
    Rectangle(const Rectangle &x);

    double len();
    double wide();
    double area();
    double circumference();
    void resize(double times);
    void resize(double l_times, double l_);

    ~Rectangle() = default;

private:
    double lenth, width;
};

Rectangle::Rectangle()
{
    lenth = 2.0;
    width = 1.0;
}
Rectangle::Rectangle(int l, int w)
{
    lenth = l;
    width = w;
}
Rectangle::Rectangle(const Rectangle &x)
{
    lenth = x.lenth;
    width = x.width;
}
double Rectangle::len()
{
    return lenth;
}

double Rectangle::wide()
{
    return width;
}
double Rectangle::area()
{
    return lenth*width;
}
double Rectangle::circumference()
{
    return (lenth + width) * 2;
}
void Rectangle::resize(double times)
{
    lenth *= times;
    width *= times;
}
void Rectangle::resize(double l_times, double w_times)
{
    lenth *= l_times;
    width *= w_times;
}



// 普通函数, 用于输出矩形信息
void output(const Rectangle &rect) {
    using namespace std;
    Rectangle a = rect;
    cout << "矩形信息: n";
              // 控制输出格式:以浮点数形式输出、小数部分保留两位
    // 补足代码:分行输出矩形长、宽、面积、周长
    cout << fixed << setprecision(2);
    cout << "长:    " << left << a.len() << endl;
    cout << "宽:    " << left << a.wide() << endl;
    cout << "面积:  " << left << a.area() << endl;
    cout << "周长:  " << left << a.circumference() << endl;
    cout << endl;
 }


// 主函数,测试Rectangle类
int main()
{
    Rectangle rect1;        // 默认构造函数被调用
    output(rect1);

    Rectangle rect2(15, 5);     // 带有两个参数的构造函数被调用
    output(rect2);

    Rectangle rect3(rect1);     // 复制构造函数被调用
    rect3.resize(3);            // 矩形rect3的长和宽同时缩放2倍
    output(rect3);

    rect3.resize(10, 2);         // 矩形rect3的长缩放5倍, 宽缩放2倍
    output(rect3);

    system("pause");
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/290467.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号