要求将数据成员定义为保护类型;
在该类中定义成员函数实现所有学生信息的输入
创建该类的两个学生对象,并将他们的信息以及各自两门课的平均成绩进行输出
#include <iostream>
using namespace std;
class Student {
protected:
string name; // 姓名
int id; // 学号
string gender; // 性别
int age; // 年龄
string class; // 班级
double computer_score; // 计算机成绩
double english_score; // 英语成绩
public:
Student() {
name = "";
id = 0;
gender = "";
age = 0;
class = "";
computer_score = 0;
english_score = 0;
}
string get_name() const { return name; }
int get_id() const { return id; }
string get_gender() const { return gender; }
int get_age() const { return age; }
string get_class() const { return class; }
double get_computer_score() const { return computer_score; }
double get_english_score() const { return english_score; }
void input_info() {
cout << "请输入学生姓名:" << endl;
cin >> name;
cout << "请输入学生学号:" << endl;
cin >> id;
cout << "请输入学生性别:" << endl;
cin >> gender;
cout << "请输入学生年龄:" << endl;
cin >> age;
cout << "请输入学生班级:" << endl;
cin >> class;
cout << "请输入学生计算机成绩:" << endl;
cin >> computer_score;
cout << "请输入学生英语成绩:" << endl;
cin >> english_score;
}
Student(string name, int id, string gender, int age, string class, double computer_score, double english_score) {
this->name = name;
this->id = id;
this->gender = gender;
this->age = age;
this->class = class;
this->computer_score = computer_score;
this->english_score = english_score;
}
};
int main() {
Student student1("张三", 100, "男", 20, "一班", 80, 90);
Student student2("李四", 101, "女", 21, "二班", 90, 85);
cout << "学生 1 信息如下:" << endl;
cout << "姓名:" << student1.get_name() << endl;
cout << "学号:" << student1.get_id() << endl;
cout << "性别:" << student1.get_gender() << endl;
cout << "年龄:" << student1.get_age() << endl;
cout << "班级:" << student1.get_class() << endl;
cout << "计算机成绩:" << student1.get_computer_score() << endl;
cout << "英语成绩:" << student1.get_english_score() << endl;
cout << "学生 2 信息如下:" << endl;
cout << "姓名:" << student2.get_name() << endl;
cout << "学号:" << student2.get_id() << endl;
cout << "性别:" << student2.get_gender() << endl;
cout << "年龄:" << student2.get_age() << endl;
cout << "班级:" << student2.get_class() << endl;
cout << "计算机成绩:" << student2.get_computer_score() << endl;
cout << "英语成绩:" << student2.get_english_score() << endl;
return 0;
}