使用类建立一个对象数组,内放五个学生的数据(学号、成绩)用指针指向数组元素,输出第1、3、5个学生的数据。
时间: 2024-06-10 17:08:55 浏览: 104
好的,这是一个比较简单的问题。下面是代码实现:
```
#include <iostream>
using namespace std;
class Student {
public:
int id; // 学号
float score; // 成绩
};
int main() {
Student students[5] = {
{1001, 89.5},
{1002, 76.0},
{1003, 92.5},
{1004, 85.0},
{1005, 78.5}
};
Student *p = students; // 指向数组首元素的指针
cout << "第1个学生的数据:学号=" << p->id << ",成绩=" << p->score << endl;
p += 2; // 指向第3个学生的指针
cout << "第3个学生的数据:学号=" << p->id << ",成绩=" << p->score << endl;
p += 2; // 指向第5个学生的指针
cout << "第5个学生的数据:学号=" << p->id << ",成绩=" << p->score << endl;
return 0;
}
```
输出结果如下:
```
第1个学生的数据:学号=1001,成绩=89.5
第3个学生的数据:学号=1003,成绩=92.5
第5个学生的数据:学号=1005,成绩=78.5
```
希望这个答案能够解决你的问题。
相关问题
建立一个对象数组,内放5个学生的学号和成绩,用指针指向数组的首元素,输出第1,3,5学生的数据。用C++实现
下面是用C++实现的代码:
```cpp
#include <iostream>
using namespace std;
struct Student {
int id; // 学号
int score; // 成绩
};
int main() {
Student students[5] = {
{1001, 85},
{1002, 92},
{1003, 78},
{1004, 90},
{1005, 88}
}; // 定义并初始化学生数组
Student* p = students; // 使用指针指向数组的首元素
cout << "第1个学生的数据:" << endl;
cout << "学号:" << p->id << ",成绩:" << p->score << endl;
p += 2; // 指向第3个学生的数据
cout << "第3个学生的数据:" << endl;
cout << "学号:" << p->id << ",成绩:" << p->score << endl;
p += 2; // 指向第5个学生的数据
cout << "第5个学生的数据:" << endl;
cout << "学号:" << p->id << ",成绩:" << p->score << endl;
return 0;
}
```
输出结果:
```
第1个学生的数据:
学号:1001,成绩:85
第3个学生的数据:
学号:1003,成绩:78
第5个学生的数据:
学号:1005,成绩:88
```
建立一个对象数组,内放五个学生的数据(学号、成绩),用指针指向数组首元素,输出第1,3,5学生的数据
可以使用以下代码来建立一个对象数组,内放五个学生的数据(学号、成绩),并用指针指向数组首元素:
```
#include <iostream>
using namespace std;
class Student {
public:
int id;
int score;
};
int main() {
Student students[5] = {{1, 90}, {2, 80}, {3, 70}, {4, 60}, {5, 50}};
Student* ptr = &students[0];
cout << "第1个学生的数据:学号=" << ptr->id << ",成绩=" << ptr->score << endl;
ptr += 2;
cout << "第3个学生的数据:学号=" << ptr->id << ",成绩=" << ptr->score << endl;
ptr += 2;
cout << "第5个学生的数据:学号=" << ptr->id << ",成绩=" << ptr->score << endl;
return 0;
}
```
输出结果为:
```
第1个学生的数据:学号=1,成绩=90
第3个学生的数据:学号=3,成绩=70
第5个学生的数据:学号=5,成绩=50
```
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)