1354 [BA1003] The Rectangle class

Description

Design a class named Rectangle to represent a rectangle. The class contains:
Two double data field named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height.
A no-arg constructor that creates a default rectangle.
A constructor that creates a rectangle with the specified width and height.
The accessor and mutator functions for all the data fields. (getWidth, getHeight, setWidth, setHeight)
A function name getArea() that returns the area of this rectangle.
A function named getPerimeter() that returns the perimeter.

class Rectangle
{
  private:
    double width, height;
  public:
    Rectangle() ;
    Rectangle(double width, double height);
    double getWidth();
    double getHeight();
    void setWidth(double width);
    void setHeight(double height);
    double getArea();
    double getPerimeter();  
};

Provided Codes

framework.cpp

// =====================
// framework source code
// =====================
#include <iostream>
#include <iomanip>
#include "source.h"
using namespace std;
void print(Rectangle &obj)
{
  cout << fixed << setprecision(2) 
  << obj.getWidth() << " " << obj.getHeight() << " " << obj.getArea() << " " << obj.getPerimeter() << endl;
}

int main() {
    Rectangle obj1;
    print(obj1);
    obj1.setWidth(4);
    obj1.setHeight(40);
    print(obj1);
    Rectangle obj2(3.5, 35.9);
    print(obj2);

    return 0;
} 

Submission

source.h

class Rectangle
{
  private:
    double width, height;
  public:
    Rectangle();
    Rectangle(double width, double height);
    double getWidth();
    double getHeight();
    void setWidth(double width);
    void setHeight(double height);
    double getArea();
    double getPerimeter();  
};

double Rectangle::getWidth(){
    return width;
}

double Rectangle::getHeight(){
    return height;
}

void Rectangle::setWidth(double width){
    Rectangle::width=width;
}

void Rectangle::setHeight(double height){
    Rectangle::height=height;
}

Rectangle::Rectangle(){
    Rectangle::setWidth(1);
    Rectangle::setHeight(1);
}

Rectangle::Rectangle(double width, double height){
    Rectangle::setWidth(width);
    Rectangle::setHeight(height);
}

double Rectangle::getArea(){
    return width*height;
}

double Rectangle::getPerimeter(){
    return (width+height)*2;
}

Standard Answer

source.h

// Problem#: 17646
// Submission#: 4642249
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include<iostream>
#include<string>
using namespace std;

class Rectangle
{
private:
    double width, height;
public:
    Rectangle(double Width = 1., double Height = 1.) {
        width = Width;
        height = Height;
    }
    double getWidth() {
        return width;
    }
    double getHeight() {
        return height;
    }
    void setWidth(double Width){
        width = Width;
    }
    void setHeight(double Height){
        height = Height;
    }
    double getArea() {
        return width * height;
    }
    double getPerimeter() {
        return 2 * (width + height);
    }
}; 
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值