Arrays 需要使用类显示随机数生成器选项并输出到文件的帮助吗 我正在使用类为C++类分配任务。我一直坚持使用随机生成器显示每个类的正确输出。我得到的只是立方体和0。赋值的目的是为圆、正方形和立方体创建一个类,并在带有循环的数组中使用随机数生成器来显示选项
最后,我必须要求用户选择1。打印结果或2。输入文件名并保存到输出文件中。即使经过研究,我也不知道该怎么做 这是我的代码和输出:Arrays 需要使用类显示随机数生成器选项并输出到文件的帮助吗 我正在使用类为C++类分配任务。我一直坚持使用随机生成器显示每个类的正确输出。我得到的只是立方体和0。赋值的目的是为圆、正方形和立方体创建一个类,并在带有循环的数组中使用随机数生成器来显示选项,arrays,class,inheritance,output,Arrays,Class,Inheritance,Output,最后,我必须要求用户选择1。打印结果或2。输入文件名并保存到输出文件中。即使经过研究,我也不知道该怎么做 这是我的代码和输出: const double PI = 2.0 * asin(1.0); class Geometry { public: Geometry(); void printColor(); //void color(string); virtual void calArea(); // virtual string calAreaPrint(); double
const double PI = 2.0 * asin(1.0);
class Geometry {
public:
Geometry();
void printColor();
//void color(string);
virtual void calArea();
// virtual string calAreaPrint();
double area, length;
string color, shape;
};
Geometry::Geometry() {
color = "blue"; //Default values set
area = 0;
length = 0;
shape = "unknown";
}
void Geometry::printColor() { // Created function printColor with color display.
cout << "The color is: " << color << endl;
}
void Geometry::calArea() { // Virtual void prints area 0 and shape unknown.
cout << "Area: " << area << endl;
cout << "The shape is: " << shape << endl;
}
class Circle : public Geometry {
private:
double radius;
string shape;
public:
Circle();// constructor and default set to 5
void calcArea();
Circle::Circle() {
radius = 5.0;
shape = "circle";
}
void Circle::calcArea() {
area= (radius * radius *PI);
cout << "The area of the circle is: " << area <<
"with a radius of: " << radius << endl;
}
class Square : public Geometry {
private:
double length;
string shape;
public:
Square();
void calcArea();
};
Square::Square() {
length = 5.3;
shape = "Square";
area = 0;
}
void Square::calcArea() {
area= (length * length);
cout << "The area of the square is: " << area <<
"with a length and width of: " << length << endl;
}
class Cube : public Geometry {
private:
double length, area;
public:
Cube();
void calcArea();
};
Cube::Cube(){ // Set the length of my choice.
length = 2;
area = 0;
shape = "Cube";
}
void Cube::calcArea() {
area=length * length * 6;
cout << "The surface area of the " << shape << " is " << area <<
"with side size of: " << length << endl;
}
int main()
{
Geometry* array[10];
for (int i = 0; i < 10; i++) {
int choice = 1 + rand() % 3;
if (choice == 1) {
array[i] = new Circle();
}
else if (choice == 2) {
array[i] = new Square();
}
else if (choice == 3) {
array[i] = new Cube();
}
}
for (int i = 0; i < 10; i++) {
array[i]->calArea();
}
return 0;
```
常数双PI=2.0*asin(1.0);
类几何{
公众:
几何();
void printColor();
//空颜色(字符串);
虚空卡拉雷亚();
//虚拟字符串calAreaPrint();
双面积,长度;
线的颜色、形状;
};
几何体::几何体(){
color=“blue”;//设置默认值
面积=0;
长度=0;
shape=“未知”;
}
void Geometry::printColor(){//创建了带有颜色显示的函数printColor。
库特