使用cpp创建一个对象数组,数组的元素是学生,学生的信息包括学号、姓名和成绩,在main函数之中将数组中大于80分的学生信息显示出来
时间: 2023-04-07 19:05:08 浏览: 125
可以使用以下代码创建一个学生类,并创建一个学生对象数组:
```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
```
建立一个对象数组,内放10个学生的数据(姓名、学号、成绩),建立一个函数max,用指向对象的指针做函数参数,在max函数中找出10个学生中成绩最高者,并输出其学号。根据学号,将对象的姓名、成绩等信息使用拷贝构造函数,拷贝到一个新对象中。对对象进行打印输出,并编写程序调试。
以下是代码实现:
```cpp
#include <iostream>
#include <cstring>
using namespace std;
class Student {
public:
Student(const char* n = "", const char* i = "", float s = 0.0);
Student(const Student& stu);
const char* getName();
const char* getID();
float getScore();
private:
char name[20];
char ID[10];
float score;
};
Student::Student(const char* n, const char* i, float s) {
strcpy(name, n);
strcpy(ID, i);
score = s;
}
Student::Student(const Student& stu) {
strcpy(name, stu.name);
strcpy(ID, stu.ID);
score = stu.score;
}
const char* Student::getName() {
return name;
}
const char* Student::getID() {
return ID;
}
float Student::getScore() {
return score;
}
int max(Student* s) {
int maxIndex = 0;
for (int i = 1; i < 10; i++) {
if (s[i].getScore() > s[maxIndex].getScore()) {
maxIndex = i;
}
}
cout << "最高分的学生学号为:" << s[maxIndex].getID() << endl;
return maxIndex;
}
int main() {
Student students[10] = {
Student("张三", "2021001", 90),
Student("李四", "2021002", 80),
Student("王五", "2021003", 70),
Student("赵六", "2021004", 95),
Student("钱七", "2021005", 85),
Student("孙八", "2021006", 75),
Student("周九", "2021007", 92),
Student("吴十", "2021008", 83),
Student("郑十一", "2021009", 78),
Student("冯十二", "2021010", 87)
};
int maxIndex = max(students);
Student maxStudent(students[maxIndex]);
cout << "学生姓名:" << maxStudent.getName() << endl;
cout << "学生学号:" << maxStudent.getID() << endl;
cout << "学生分数:" << maxStudent.getScore() << endl;
return 0;
}
```
输出结果:
```
最高分的学生学号为:2021004
学生姓名:赵六
学生学号:2021004
学生分数:95
```
在该代码中,首先定义了一个 `Student` 类,包含了姓名、学号和成绩三个属性,其中构造函数和拷贝构造函数分别实现了对象的初始化和拷贝功能。
接着,在主函数中创建了一个存储学生数据的数组 `students`,并将每个学生的姓名、学号和成绩传递给类对象的构造函数,初始化了数组中的每一个元素。
在 `max` 函数中,通过循环对数组 `s` 中的每个学生的成绩进行比较,找出最高分数对应的学生,并输出该学生的学号。
最后,利用拷贝构造函数将最高分对应的学生数据拷贝到新对象 `maxStudent` 中,然后分别输出该对象的姓名、学号和成绩。
阅读全文