建立一个对象数组,里面放五个学生的数据,用指针指向数组首元素输出第1,3,5学生的数据
时间: 2023-05-13 14:06:46 浏览: 68
好的,以下是建立对象数组并输出指定学生数据的代码:
```c++
#include <iostream>
using namespace std;
class Student {
public:
string name;
int age;
string major;
};
int main() {
Student students[5] = {
{"Alice", 18, "Computer Science"},
{"Bob", 19, "Mathematics"},
{"Charlie", 20, "Physics"},
{"David", 21, "Chemistry"},
{"Eve", 22, "Biology"}
};
Student *p = students;
cout << "The 1st student's data: " << p->name << " " << p->age << " " << p->major << endl;
p += 2;
cout << "The 3rd student's data: " << p->name << " " << p->age << " " << p->major << endl;
p += 2;
cout << "The 5th student's data: " << p->name << " " << p->age << " " << p->major << endl;
return 0;
}
```
输出结果为:
```
The 1st student's data: Alice 18 Computer Science
The 3rd student's data: Charlie 20 Physics
The 5th student's data: Eve 22 Biology
```