2025年设计一个Student类

设计一个Student类1 基本信息 学号 姓名 性别 出生日期 院系 专业 数据成员中 日期 要声明为一个内嵌子对象 2 Student 类要包含 构造函数 内联成员函数 带默认参数的构造函数 复制构造函数 3 成员函数基本功能有

大家好,我是讯享网,很高兴认识大家。

(1)基本信息:学号、姓名、性别、出生日期、院系、专业; 数据成员中:“日期”要声明为一个内嵌子对象

(2)Student类要包含:构造函数、内联成员函数、带默认参数的构造函数、复制构造函数

(3)成员函数基本功能有:

  A)可以从键盘输入学生的基本信息;

  B)定义一个函数setInfo(形参表),可以修改学生的一些基本信息函数,例如:姓名,学号可以作修改;


讯享网

从键盘输入基本信息,调用带参数的构造函数生成学生对象;

 不同类型的信息须使用合理的变量类型,姓名、院系等可定义为字符串,使用string来定义。

#include <iostream> #include <string> using namespace std; class Date{ public: Date(){} Date(int year1,int month1,int day1): year(year1),month(month1),day(day1){} Date(Date& p):year(p.year),month(p.month),day(p.day){} ~Date(){} void get(); void sca(); private: int year; int month; int day; }; inline void Date:: sca(){ cout << "请分别输入出生日期的年月日" << endl; cin >> year >> month >> day; } inline void Date::get(){ cout << year << "." << month << "." << day << endl; } class Student{ public: Student(){} Student(Date date,int ID = 0,string name = "无名",string sex = "未知", string fac = "未知",string spe = "未知"): date(date),ID(ID),name(name),sex(sex),fac(fac),spe(spe){} Student(Student &p): ID(p.ID),name(p.name),sex(p.sex),fac(p.fac),spe(p.spe),date(p.date){} ~Student(){} void stc(Date& p); void setlnfo(Student& p); void show(); private: int ID; string name,sex,fac,spe; Date date; }; inline void Student::stc(Date &p){ cout << "请分别输入这个学生的学号、姓名、性别、院系、专业" << endl; cin >> ID >> name >> sex >> fac >> spe; date = p; } inline void Student::show(){ cout << ID << " " << name << " " << sex << " " << fac << " " << spe << " "; date.get(); } inline void Student::setlnfo(Student& p){ int m; string n; cout << "请分别输入要更改的学号和姓名" << endl; cin >> m >> n; p.ID = m; p.name = n; } int main(){ bool choose = 0; Date a; a.sca(); Student b; b.stc(a); cout << "您是否要修改学生的学号和姓名,是请输入1,否请输入0" << endl; cin >> choose; if(choose) b.setlnfo(b); cout << "这个学生的学号、姓名、性别、院系、专业、出生日期分别是" << endl; b.show(); }

讯享网

小讯
上一篇 2025-03-07 13:27
下一篇 2025-02-10 18:38

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/17336.html