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);
}
};