使用C++语言设计一个 Studnet(学生)类 1、基本信息:学号、姓名、性别、出生日期、年级、班级、院系、专业; 其中:基本信息为 private 属性,成员函数为public 属性; 2、Student 类有多个构造函数:缺省构造函数、带参数的构造函数、带默认参数的构造 函数 3、基本功能: 1)使用对象数组保存学生对象; 2)可以从键盘输入学生的基本信息; 3)SetInfo(形参表)函数可以修改学生的基本信息函数,例如:班级、专业等; 4)Show()函数显示学生信息;
时间: 2023-07-10 14:16:53 浏览: 60
好的,下面是C++语言设计的Student类的示例代码:
```cpp
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string sno; // 学号
string name; // 姓名
string gender; // 性别
string birthday; // 出生日期
string grade; // 年级
string cls; // 班级
string department; // 院系
string major; // 专业
public:
// 默认构造函数
Student() {}
// 带参数的构造函数
Student(string sno, string name, string gender, string birthday, string grade, string cls, string department, string major) {
this->sno = sno;
this->name = name;
this->gender = gender;
this->birthday = birthday;
this->grade = grade;
this->cls = cls;
this->department = department;
this->major = major;
}
// 带默认参数的构造函数
Student(string sno = "", string name = "", string gender = "", string birthday = "", string grade = "", string cls = "", string department = "", string major = "") {
this->sno = sno;
this->name = name;
this->gender = gender;
this->birthday = birthday;
this->grade = grade;
this->cls = cls;
this->department = department;
this->major = major;
}
// 从键盘输入学生的基本信息
void InputInfo() {
cout << "请输入学生的基本信息:" << endl;
cout << "学号:";
cin >> sno;
cout << "姓名:";
cin >> name;
cout << "性别:";
cin >> gender;
cout << "出生日期:";
cin >> birthday;
cout << "年级:";
cin >> grade;
cout << "班级:";
cin >> cls;
cout << "院系:";
cin >> department;
cout << "专业:";
cin >> major;
}
// 修改学生的基本信息
void SetInfo(string cls, string major) {
this->cls = cls;
this->major = major;
}
// 显示学生信息
void Show() {
cout << "学号:" << sno << endl;
cout << "姓名:" << name << endl;
cout << "性别:" << gender << endl;
cout << "出生日期:" << birthday << endl;
cout << "年级:" << grade << endl;
cout << "班级:" << cls << endl;
cout << "院系:" << department << endl;
cout << "专业:" << major << endl;
}
};
int main() {
// 使用对象数组保存学生对象
Student students[3];
// 从键盘输入学生的基本信息
for (int i = 0; i < 3; i++) {
students[i].InputInfo();
}
// 修改学生的基本信息
students[0].SetInfo("Class 1", "Computer Science");
// 显示学生信息
for (int i = 0; i < 3; i++) {
students[i].Show();
cout << endl;
}
return 0;
}
```
注:以上代码仅供参考,实际使用时需要根据具体情况进行修改。