Fourth practice 1

Fourth practice 1

任务描述

定义一个Rectangle类,有长itsWidth、宽itsLength等属性,重载其构造函数Rectangle()和Rectangle(int width, int length)。

测试输入:1020

预期输出:

Rect1 width: 5
Rect1 length: 10
Enter a width: 10
Enter a length: 20
Rect2 width: 10
Rect2 length: 20

源代码

#include <iostream>
using namespace std;
class Rectangle
{
public:
    Rectangle();
    Rectangle(int width, int length);
    ~Rectangle() {}
    int GetWidth() const { return itsWidth; }
    int GetLength() const { return itsLength; }
private:
    int itsWidth;
    int itsLength;
};
Rectangle::Rectangle()
{
    itsWidth = 5;
    itsLength = 10;
}

Rectangle::Rectangle(int width, int length)
{
    this->itsWidth = width;
    this->itsLength = length;
}

int main()
{
    int width,length;
    cin>>width>>length;

    //输出初始构造函数的值
    Rectangle rc1;
    cout<<"Rect1 width: "<<rc1.GetWidth()<<endl;
    cout<<"Rect1 length: "<<rc1.GetLength()<<endl;
    //输入长和宽
    cout<<"Enter a width: "<<width<<endl;
    cout<<"Enter a length: "<<length<<endl;
    //输出重载后的构造函数的值
    Rectangle rc2(width,length);
    cout<<"Rect2 width: "<<rc2.GetWidth()<<endl;
    cout<<"Rect2 length: "<<rc2.GetLength()<<endl;
    return 0;
}

原文链接: https://www.cnblogs.com/lightice/p/12911025.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    Fourth practice 1

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/349415

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年3月2日 上午5:40
下一篇 2023年3月2日 上午5:41

相关推荐