【发布时间】:2019-04-04 15:31:12
【问题描述】:
我需要创建一个程序,使用一个类从用户输入创建三个三角形,包括以下规范:
私有数据应包含 4 个成员: 长度 宽度 区域 周长
有 1 个具有以下参数的构造函数:长度和宽度。
有 2 个突变体: setLength 设置/修改长度 和 setWidth 设置/修改宽度。
-
有 5 个访问器: getLength 返回长度
getWidth 返回宽度
getPerimeter 返回周长
getArea 返回区域
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) “未使用私有数据中的面积和周长” 每次修改
width和/或length时都计算area和perimeter,然后返回它们而不是每次都计算。 2)你需要void Rectangle::printObjet(),然后是cout << " Width = " << ....3)你已经使用了这个构造函数:Rectangle box1; -
@DimChtz 代码甚至没有编译
-
编译错误太多了。请检查您的代码,复制可以编译的代码,然后粘贴。