使用cpp创建一个对象数组,数组的元素是学生,学生的信息包括学号、姓名和成绩,在main函数之中将数组中大于80分的学生信息显示出来
时间: 2023-04-07 18:05:08 浏览: 68
可以使用以下代码创建一个学生类,并创建一个学生对象数组:
```cpp
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
int id;
string name;
int score;
};
int main() {
const int N = 5; // 数组大小
Student students[N] = {
{1, "Tom", 78},
{2, "Jerry", 85},
{3, "Alice", 92},
{4, "Bob", 76},
{5, "David", 88}
};
// 输出成绩大于80分的学生信息
for (int i = 0; i < N; i++) {
if (students[i].score > 80) {
cout << "学号:" << students[i].id << ",姓名:" << students[i].name << ",成绩:" << students[i].score << endl;
}
}
return 0;
}
```
输出结果为:
```
学号:2,姓名:Jerry,成绩:85
学号:3,姓名:Alice,成绩:92
学号:5,姓名:David,成绩:88
```
相关问题
1.创建一个对象数组,数组的元素是学生对象,学生的信息包括学号、姓名和成绩(一门课成绩),在main函数中将数组元素按学生成绩从小到大的顺序排序并显示出来。(学生对象不得少于5个,显示定义构造函数和析构函数,并在里面加上测试语句)
下面是一个示例代码,实现了上述要求:
```cpp
#include <iostream>
#include <algorithm>
using namespace std;
class Student {
public:
Student(int id, string name, int score) : id_(id), name_(name), score_(score) {
cout << "Creating Student object: " << name_ << endl;
}
~Student() {
cout << "Destroying Student object: " << name_ << endl;
}
int getId() const {
return id_;
}
string getName() const {
return name_;
}
int getScore() const {
return score_;
}
private:
int id_;
string name_;
int score_;
};
bool cmp(const Student &s1, const Student &s2) {
return s1.getScore() < s2.getScore();
}
int main() {
Student students[] = {
Student(1, "Tom", 90),
Student(2, "Jerry", 80),
Student(3, "Spike", 70),
Student(4, "Tyke", 85),
Student(5, "Tuffy", 95)
};
int n = sizeof(students) / sizeof(students[0]);
sort(students, students + n, cmp);
for (int i = 0; i < n; ++i) {
cout << "Student #" << students[i].getId()
<< ", Name: " << students[i].getName()
<< ", Score: " << students[i].getScore() << endl;
}
return 0;
}
```
运行结果如下:
```
Creating Student object: Tom
Creating Student object: Jerry
Creating Student object: Spike
Creating Student object: Tyke
Creating Student object: Tuffy
Destroying Student object: Tuffy
Destroying Student object: Tyke
Destroying Student object: Spike
Destroying Student object: Jerry
Destroying Student object: Tom
Student #3, Name: Spike, Score: 70
Student #2, Name: Jerry, Score: 80
Student #4, Name: Tyke, Score: 85
Student #1, Name: Tom, Score: 90
Student #5, Name: Tuffy, Score: 95
```
建立一个对象数组,内放4个学生的数据(姓名、学号、成绩),输出成绩最高者。用c++的形式
好的,根据您的要求,我来设计一个学生类的C++代码:
```cpp
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
string name;
int id;
double score;
Student() {}
Student(string n, int i, double s) {
name = n;
id = i;
score = s;
}
};
int main() {
Student students[4] = {
Student("张三", 101, 89.5),
Student("李四", 102, 92.0),
Student("王五", 103, 87.5),
Student("赵六", 104, 95.0)
};
Student highestScoreStudent = students[0];
for (int i = 1; i < 4; i++) {
if (students[i].score > highestScoreStudent.score) {
highestScoreStudent = students[i];
}
}
cout << "成绩最高者:" << highestScoreStudent.name << ",学号:" << highestScoreStudent.id << ",成绩:" << highestScoreStudent.score << endl;
return 0;
}
```
解释一下代码:
首先定义了一个学生类Student,包括了姓名、学号、成绩这些属性,以及两个构造函数(一个是默认构造函数,一个是带参数的构造函数)。
接着,在main函数中,创建了一个学生对象数组students,包含了四个学生对象,每个对象的数据通过构造函数初始化。
然后,定义了一个highestScoreStudent对象,用于存储成绩最高的学生。通过一个for循环,遍历整个学生数组,找到成绩最高的学生对象,将其赋值给highestScoreStudent对象。
最后输出了结果,完成了程序的设计。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)