如何使用类创建矩形
How to use classes to create rectangles
我需要创建一个程序,该程序使用类从用户输入中创建三个三角形,包括以下规范:
-
私人数据应由4名成员组成: 长度 宽度 面积 周边
-
具有 1 个具有以下参数的构造函数:长度和宽度。
-
有 2 个突变体: 设置长度以设置/修改长度 和设置宽度以设置/修改宽度。
-
有 5 个访问器: getLength 以返回长度
getWidth 返回宽度
获取周长以返回周长
获取区域以返回区域
printObjet 打印矩形的表示形式(使用相同的 前身的输出标准)
对于额外的学分,存储您在矢量中创建的 10 个矩形对象,并通过矢量进行排序以打印出 10 个矩形对象。一旦我学会了如何完成主要任务,我相信我可以自己完成,尽管额外的帮助不会出错。
使用 DimChtz 的更改进行编辑后,代码如下。
#include <iostream>
using namespace std;
// Rectangle class declaration.
class Rectangle
{
private:
double width;
double length;
double area;
double perimeter;
public:
void setWidth(double);
void setLength(double);
double getWidth();
double getLength();
double getArea();
double getPerimeter();
void printObjet();
void calc();
Rectangle();
};
//beginning class constructor
Rectangle::Rectangle() {
this->width = 0;
this->length = 0;
this->area = 0;
this->perimeter = 0;
}
// main class constructor
Rectangle::Rectangle(double width, double length)
{ this->width = width;
this->length = length;
this->calc();
}
//calc calculates the area and perimeter from width and length.
void Rectangle::calc() {
this->area = this->width * this->length;
this->perimeter = 2 * (this->width + this->length); }
// the class functions definition
// setWidth assigns its argument to the private member width.
void Rectangle::setWidth(double width)
{ this->width = width;
this->calc(); }
// setLength assigns its argument to the private member length.
void Rectangle::setLength(double length)
{ this->length = length;
this->calc(); }
// getLength returns the value in the private member length.
double Rectangle::getLength()
{ return length; }
// getWidth returns the value in the private member width.
double Rectangle::getWidth()
{ return width; }
// getPerimeter returns parameter of box.
double Rectangle::getPerimeter()
{ return this->perimeter; }
// getArea returns area of box.
double Rectangle::getArea()
{ return this->area; }
// printObjet prints the data of the rectangle
void Rectangle::printObjet()
{ cout << " Width = " << getWidth() << endl;
cout << " Length = " << getLength() << endl;
cout << " Perimeter = " << getPerimeter() << endl;
cout << " Area = " << getArea() << endl << endl; }
// the main program
int main()
{
double width; // Local variable for width.
double length; // Local variable for length.
// create object using constructor
Rectangle box1;
Rectangle box2;
Rectangle box3;
// Get the 1st rectangle's width and length from the user.
cout << "Enter the length of the 1st rectangle: ";
cin >> length;
cout << length << endl;
cout << "Enter the width of the 1st rectangle: ";
cin >> width;
cout << width << endl;
// Store the width and length of the rectangle in the box1 object.
box1.setWidth(width);
box1.setLength(length);
// Display box1 data.
cout << "You created a rectangle with the following characteristics:" << endl << endl;
box1.printObjet();
// Get the 2nd rectangle's width and length from the user.
cout << "Enter the length of the 2nd rectangle: ";
cin >> length;
cout << length << endl;
cout << "Enter the width of the 2nd rectangle: ";
cin >> width;
cout << width << endl;
// Store the width and length of the rectangle in the box1 object.
box2.setWidth(width);
box2.setLength(length);
// Display box2 data.
cout << "You created a rectangle with the following characteristics:" << endl << endl;
box2.printObjet();
// Get the 3rd rectangle's width and length from the user.
cout << "Enter the length of the 3rd rectangle: ";
cin >> length;
cout << length << endl;
cout << "Enter the width of the 3rd rectangle: ";
cin >> width;
cout << width << endl;
// Store the width and length of the rectangle in the box1 object.
box3.setWidth(width);
box3.setLength(length);
// Display box3 data.
cout << "You created a rectangle with the following characteristics:" << endl << endl;
box3.printObjet();
return 0;
}
编译错误现在是
34:1: error: prototype for 'Rectangle::Rectangle(double, double)' does not match any in class 'Rectangle'
Rectangle::Rectangle(double width, double length)
5:7: error: candidates are: constexpr Rectangle::Rectangle(Rectangle&&)
class Rectangle
5:7: error: constexpr Rectangle::Rectangle(const Rectangle&)
27:1: error: Rectangle::Rectangle()
1)事实上,你不使用area
和perimeter
。每次修改width
和/或length
时,都应同时计算area
和perimeter
:
Rectangle::Rectangle(double width, double length) {
this->width = width;
this->length = length;
this->calc();
}
void Rectangle::setWidth(double width) {
this->width = width;
this->calc();
}
void Rectangle::setLength(double length) {
this->length = length;
this->calc();
}
double Rectangle::getPerimeter() { return this->perimeter; }
double Rectangle::getArea() { return this->area; }
现在你需要一个新的私有函数:
void Rectangle::calc() {
this->area = this->width * this->length;
this->perimeter = 2 * (this->width + this->length);
}
2)你需要稍微改变一下printObjet
:
void printObjet();
// ...
void Rectangle::printObjet() {
cout << " Width = " << getWidth() << endl << " Length = " << getLength() << endl << " Perimeter = " << getPerimeter() << endl<< " Area = " << getArea() << endl << endl;
}
然后更改:
cout << box1.printObjet();
自:
box1.printObjet();
3)你需要这个:
Rectangle();
// ...
Rectangle::Rectangle() {
this->width = 0;
this->length = 0;
this->area = 0;
this->perimeter = 0;
}
或者简单地:
Rectangle::Rectangle() : width(0), length(0), area(0), perimeter(0) { }
甚至:
Rectangle::Rectangle() : Rectangle(0.0, 0.0) { }
相关文章:
- 如何使用MSVC 2019创建和使用Qt 5.14.0自定义小工具插件
- C++共享库:创建和使用
- 如何创建一个使用开源c项目的c++控制台应用程序
- 如何创建和使用存储为 c++ 对象的 python 对象?
- 我正在尝试创建一个使用 c++ 中的参数包构造函数的异常类
- 如何创建目录C (使用_mkdir)
- 如何在 C 中创建用于使用 C++ 代码的包装器?
- 创建进程使用路径时出现错误事件:类型 "char *" 的 E0167 参数与类型 "LPWSTR" 的参数不兼容
- 创建用于使用库的 cmake 模块
- 类创建和使用问题
- 如何在C 类中使用备用参数列表创建和使用finturePointer
- 如何使用 boost::p ython 创建和使用 Python 对象的实例
- 在 Solaris 上创建和使用动态共享库
- 是否可以创建和使用MatIterator数组
- 正在堆中创建和使用数组
- 如何创建和使用指向类中未知数量结构的数组的指针
- 在Xcode控制台项目中创建和使用C++库
- C++如何创建和使用指针来查找字符数组的长度
- 如何在C++中创建和使用共享库
- 在OpenSceneGraph中创建球体(使用osg::Geometry)