(1)基本信息:学号、姓名、性别、出生日期、院系、专业; 数据成员中:“日期”要声明为一个内嵌子对象
(2)Student类要包含:构造函数、内联成员函数、带默认参数的构造函数、复制构造函数
(3)成员函数基本功能有:
A)可以从键盘输入学生的基本信息;
B)定义一个函数setInfo(形参表),可以修改学生的一些基本信息函数,例如:姓名,学号可以作修改;
C)包括成员函数void show()显示学生信息;
【提示】 注意带默认参数值的构造函数的声明与定义;
从键盘输入基本信息,调用带参数的构造函数生成学生对象;
不同类型的信息须使用合理的变量类型,姓名、院系等可定义为字符串,使用string来定义。
实验思路:设计Student类,构造函数、内联成员函数、带默认参数的构造函数、复制构造函数 void show()要作为公有类型成员,而其中的一个点类设计要作为子对象,这时就要注意在主函数中先构造一个点类,能让下面Student使用。
#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();
}
1237

被折叠的 条评论
为什么被折叠?



