c++定义一个学生类student数组
时间: 2023-09-09 08:06:24 浏览: 54
以下是C++代码示例,定义了一个名为Student的类和一个名为studentArray的数组:
```c++
#include <iostream>
using namespace std;
class Student {
public:
string name;
int age;
string major;
float gpa;
};
int main() {
const int SIZE = 5;
Student studentArray[SIZE];
// 初始化数组
studentArray[0] = {"Alice", 20, "Computer Science", 3.5};
studentArray[1] = {"Bob", 21, "Mathematics", 3.2};
studentArray[2] = {"Charlie", 19, "Physics", 3.8};
studentArray[3] = {"David", 22, "Engineering", 3.4};
studentArray[4] = {"Emily", 18, "Biology", 3.6};
// 打印每个学生信息
for (int i = 0; i < SIZE; i++) {
cout << "Name: " << studentArray[i].name << endl;
cout << "Age: " << studentArray[i].age << endl;
cout << "Major: " << studentArray[i].major << endl;
cout << "GPA: " << studentArray[i].gpa << endl;
cout << endl;
}
return 0;
}
```
在这个示例中,我们定义了一个名为Student的类,它有四个公共成员变量:name、age、major和gpa。我们还定义了一个名为studentArray的数组,用于存储五个学生的信息。在main函数中,我们初始化了数组,并打印了每个学生的信息。