如何在 class 名称中创建一个带有变量名称的 class?
How would I create a class with a variable name in the class name?
所以,这是我的代码:
#include <iostream>
#include <conio.h>
using namespace std;
class rectangle {
public:
double width;
double height;
rectangle(double, double);
double area() { return (width*height);}
};
rectangle::rectangle(double a, double b) {
width = a;
height = b;
}
int main() {
cout << "How many rectangles would you like to create? ";
int rectNum;
cin >> rectNum;
for (int counter = 0; counter < rectNum; counter++) {
int rectCount = 1;
rectCount = counter + 1;
double rectWidth, rectHeight;
cout << "\nEnter width of rectangle " << rectCount << ": ";
cin >> rectWidth;
cout << "\nEnter height of rectangle " << rectCount << ": ";
cin >> rectHeight;
rectangle rect/*integer value of rectCount at current time*/(rectWidth, rectHeight);
}
return 0;
}
正如评论区所说,我想创建一个名为rect的矩形,其后缀是整数rectCount的当前值。我该怎么做?提前致谢!
变量名必须在编译时定义,这样你试图做的就不会起作用。
这个习语在 C++ 中从来没有用过。正确的方法是将矩形存储在像 std::vector
这样的容器中,它会自动增长和收缩以满足您的需要。然后,您可以循环遍历向量,而不必担心其中实际有多少元素。
std::vector<rectangle> rects(rectNum);
for (int counter = 0; counter < rectNum; counter++) {
/* .. */
rects.emplace_back(rectWidth, rectHeight);
}
实际上不需要循环。 std::vector
的构造函数会为您处理。
std::vector<rectangle> rects(rectNum, {rectWidth, rectHeight});
您需要包含 <vector>
才能使用 std::vector
。
动态设置 class 名称在 C++ 中是不可能的。但是,静态变量可用于存储 运行 个矩形:
#include <iostream>
#include <conio.h>
using namespace std;
class rectangle
{
public:
double width;
double height;
static int rectCount; //declare static variable
rectangle(double, double);
double area() { return (width*height);}
};
int rectangle::rectCount=0; //initialize static variable
rectangle::rectangle(double a, double b)
{
width = a;
height = b;
rectCount++;
}
int main()
{
cout << "How many rectangles would you like to create? ";
int rectNum;
cin >> rectNum;
for (int counter = 0; counter < rectNum; counter++)
{
int rectCount = 1;
rectCount = counter + 1;
double rectWidth, rectHeight;
cout << "\nEnter width of rectangle " << rectCount << ": ";
cin >> rectWidth;
cout << "\nEnter height of rectangle " << rectCount << ": ";
cin >> rectHeight;
rectangle rect=rectangle(rectWidth, rectHeight);
cout<<"rectCount value: "<<rectCount;
}
return 0;
}
所以,这是我的代码:
#include <iostream>
#include <conio.h>
using namespace std;
class rectangle {
public:
double width;
double height;
rectangle(double, double);
double area() { return (width*height);}
};
rectangle::rectangle(double a, double b) {
width = a;
height = b;
}
int main() {
cout << "How many rectangles would you like to create? ";
int rectNum;
cin >> rectNum;
for (int counter = 0; counter < rectNum; counter++) {
int rectCount = 1;
rectCount = counter + 1;
double rectWidth, rectHeight;
cout << "\nEnter width of rectangle " << rectCount << ": ";
cin >> rectWidth;
cout << "\nEnter height of rectangle " << rectCount << ": ";
cin >> rectHeight;
rectangle rect/*integer value of rectCount at current time*/(rectWidth, rectHeight);
}
return 0;
}
正如评论区所说,我想创建一个名为rect的矩形,其后缀是整数rectCount的当前值。我该怎么做?提前致谢!
变量名必须在编译时定义,这样你试图做的就不会起作用。
这个习语在 C++ 中从来没有用过。正确的方法是将矩形存储在像 std::vector
这样的容器中,它会自动增长和收缩以满足您的需要。然后,您可以循环遍历向量,而不必担心其中实际有多少元素。
std::vector<rectangle> rects(rectNum);
for (int counter = 0; counter < rectNum; counter++) {
/* .. */
rects.emplace_back(rectWidth, rectHeight);
}
实际上不需要循环。 std::vector
的构造函数会为您处理。
std::vector<rectangle> rects(rectNum, {rectWidth, rectHeight});
您需要包含 <vector>
才能使用 std::vector
。
动态设置 class 名称在 C++ 中是不可能的。但是,静态变量可用于存储 运行 个矩形:
#include <iostream>
#include <conio.h>
using namespace std;
class rectangle
{
public:
double width;
double height;
static int rectCount; //declare static variable
rectangle(double, double);
double area() { return (width*height);}
};
int rectangle::rectCount=0; //initialize static variable
rectangle::rectangle(double a, double b)
{
width = a;
height = b;
rectCount++;
}
int main()
{
cout << "How many rectangles would you like to create? ";
int rectNum;
cin >> rectNum;
for (int counter = 0; counter < rectNum; counter++)
{
int rectCount = 1;
rectCount = counter + 1;
double rectWidth, rectHeight;
cout << "\nEnter width of rectangle " << rectCount << ": ";
cin >> rectWidth;
cout << "\nEnter height of rectangle " << rectCount << ": ";
cin >> rectHeight;
rectangle rect=rectangle(rectWidth, rectHeight);
cout<<"rectCount value: "<<rectCount;
}
return 0;
}