21xrx.com
2025-07-14 10:50:30 Monday
登录
文章检索 我的文章 写文章
C++实现学生信息输出:姓名、性别、学号、期中成绩、期末成绩
2023-07-01 02:59:52 深夜i     103     0
C++ 学生信息 姓名 学号 期中成绩 期末成绩

C++是一种功能强大、被广泛应用的编程语言,它可以用来开发各种各样的程序,包括学生信息输出。在这里,我们将介绍如何使用C++来实现学生信息输出,包括姓名、性别、学号、期中成绩和期末成绩。

首先,我们需要定义一个学生类,其中包括姓名、性别、学号、期中成绩和期末成绩。通过对这个类进行定义,我们可以在程序中使用这些属性来表示每个学生的信息。下面是一个示例:

#include<iostream>
using namespace std;
class Student
public:
  string name; // 姓名
  string gender; // 性别
  string studentId; // 学号
  int midtermScore; // 期中成绩
  int finalScore; // 期末成绩
;

在这个类中,我们使用了string类型来表示学生的姓名、性别和学号,同时使用了int类型来表示期中成绩和期末成绩。对于这些属性,我们还需要定义一些函数来实现输入和输出的功能。下面是一个示例:

void input(Student &s) {
  cout << "Please input the name of the student: ";
  getline(cin, s.name);
  cout << "Please input the gender of the student: ";
  getline(cin, s.gender);
  cout << "Please input the student ID of the stduent: ";
  getline(cin, s.studentId);
  cout << "Please input the midterm score of the student: ";
  cin >> s.midtermScore;
  cout << "Please input the final score of the student: ";
  cin >> s.finalScore;
}
void output(Student s)
  cout << "Name: " << s.name << endl;
  cout << "Gender: " << s.gender << endl;
  cout << "Student ID: " << s.studentId << endl;
  cout << "Midterm Score: " << s.midtermScore << endl;
  cout << "Final Score: " << s.finalScore << endl;

利用这些函数,我们可以方便地输入和输出学生的信息。下面是一个示例程序:

#include<iostream>
using namespace std;
class Student
public:
  string name; // 姓名
  string gender; // 性别
  string studentId; // 学号
  int midtermScore; // 期中成绩
  int finalScore; // 期末成绩
;
void input(Student &s) {
  cout << "Please input the name of the student: ";
  getline(cin, s.name);
  cout << "Please input the gender of the student: ";
  getline(cin, s.gender);
  cout << "Please input the student ID of the stduent: ";
  getline(cin, s.studentId);
  cout << "Please input the midterm score of the student: ";
  cin >> s.midtermScore;
  cout << "Please input the final score of the student: ";
  cin >> s.finalScore;
}
void output(Student s)
  cout << "Name: " << s.name << endl;
  cout << "Gender: " << s.gender << endl;
  cout << "Student ID: " << s.studentId << endl;
  cout << "Midterm Score: " << s.midtermScore << endl;
  cout << "Final Score: " << s.finalScore << endl;
int main() {
  Student s;
  input(s);
  output(s);
  return 0;
}

通过这个程序,我们可以向屏幕输出学生的姓名、性别、学号、期中成绩和期末成绩。这对于学生信息管理和处理来说非常有帮助,特别是在学校管理系统中。因此,如果你需要实现这样的功能,C++是一个很好的选择。

  
  

评论区