1354 [BA1003] The Rectangle class
来源:互联网 发布:淘宝缩水女的名字 编辑:程序博客网 时间:2024/05/22 13:24
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); }};
阅读全文
0 0
- 1354 [BA1003] The Rectangle class
- (Chapter 9) 9.1 The Rectangle class
- HGE:Tutorials:Rectangle Class
- LeetCode_492. Construct the Rectangle
- LEETCODE--Construct the Rectangle
- Construct the Rectangle
- leetcode492: Construct the Rectangle
- 492. Construct the Rectangle
- 【leetcode】Construct the Rectangle
- 492. Construct the Rectangle
- LeetCode:Construct the Rectangle
- 492. Construct the Rectangle
- 492. Construct the Rectangle
- 492. Construct the Rectangle
- LeetCode Construct the Rectangle
- Construct the Rectangle
- 492. Construct the Rectangle
- Construct the Rectangle
- (151)无需切线空间应用凹凸贴图
- python爬虫2之简单模拟浏览器
- Java成绩判断优良中差
- bzoj 4305: 数列的GCD 数学
- NRF24L01的最简配置
- 1354 [BA1003] The Rectangle class
- 装饰模式
- java的几种对象(PO,VO,DAO,BO,POJO)解释
- unity下载
- UserDict、UserString、UserList存在的意义[转载]
- Android 8.0:支持全新emoji表情
- (牛客网)二叉树的深度
- Java中的水仙花
- POJ2686_Traveling by Stagecoach_状压DP