这段代码中总共有三个错误。
-
在非参数化构造函数中,您没有将black用作字符串
-
-
创建名为rectangularcube1的新对象时,使用()来调用函数。
C++:
#include<iostream>
using namespace std;
class RectangularCube { //Defined the class Rectangular Cube
private: //Access specifier - Declare two sides as private data field
string color; //Private data field color that is string type
double width;`enter code here`
double height;
public:
double length; //Declare one side as a public data field
//Contructor with parameteres
RectangularCube(string newColor, double newWidth, double newHeight, double newLength) {
color = newColor;
width = newWidth;
height = newHeight;
length = newLength;
}
RectangularCube() { //No-arguments contructor with all three sides and color set to different values
color = "black"; // here you din't write black in string (with double quotes)
width = 5.145;
height = 4.894;
length = 10.123;
}
// Return Volume of this Rectangular Cube
double getVolume() {
return length * width * height;
};
//Return Surface Area of this Rectangular Cube
double getSurfaceArea() {
return 2*(length * width) + 2 * (length * height) + 2 * (width * height); // you din't use * operator before brackets
};
//get and set functions for the two private sides and color data field
double getColor();
double getWidth();
double getHeight();
void setColor();
void setWidth();
void setHeight();
};
int main() {
RectangularCube rectangularcube1; // you created new object with name rectangluarcube1 but you were using parenthesis like => rectangularcube1(); which is a function call
cout << "The Volume of the Rectangular Cube is: "
<< rectangularcube1.getVolume() << endl;
return 0;
}