设计一个Student类

(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();
}

  • 4
    点赞
  • 37
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值