欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

c++ 实现学生类设计

程序员文章站 2022-05-06 16:12:06
...

功能:设计一个Student(学生)类

  • 基本信息:学号、姓名、性别、出生日期、年级、班级、院系、专业
    其中:基本信息为private属性,成员函数为public属性

  • Student类有多个构造函数:缺省的构造函数、带参数的构造函数、带默认参数的构造函数

  • 基本功能:
    (1)使用对象数组保存学生对象
    (2)可以从键盘输入学生的基本信息
    (3)SetInfo(形参表)函数可以修改学生的基本信息
    (4)Show()函数显示学生信息

注意:  
1、构造函数分为带参数和不带参数两种,注意声明与定义
2、从键盘输入基本信息,调用带参数的构造函数生成学生对象
3、当输入的数据不合理时,要有相应的处理和提示

代码:

#include<iostream>
#include<string.h> 
using namespace std;

//Student类
class Student
{
	public:
		//这是一个缺省的构造函数,与带默认参数的构造函数只能存在一个 
		Student();
		
		//这是一个带参数的构造函数 
		Student(string ID, string name, char sex, string birthday, int grade, string stu_class, string institute, string major);
		
        //这是一个带默认参数的构造函数
	//	Student( int grade=2018, string institue="com", string major="ddd"); 
		
		//这是一个复制构造函数,注意深拷贝和浅拷贝的区别(成员数据是否有指针变量) 
		Student(const Student& obj);
		
		//析构函数 
		~Student(void);
		
		void SetInfo(string ID, string name, char sex, string birthday, int grade, string stu_class, string institute, string major);
		void Show();
		int Check_input();
	
	private:
		string ID;
		string name;
		char sex;
		string birthday;
		int grade;
		string stu_class;
		string institute;
		string major;
};

Student::Student(void)
{
	cout<<"object is being created by constructor without parameters!"<<endl;
}

//采用初始化列表来初始化字段 
Student::Student(string ID, string name, char sex, string birthday, int grade, string stu_class, string institute, string major):ID(ID),name(name),sex(sex),birthday(birthday),grade(grade),stu_class(stu_class),institute(institute),major(major)
{
	cout<<"object is being created by constructor with paramters!"<<endl;
}

//Student::Student(int garde, string institue, string major)
//{
//	grade=grade;
//	institute=institue;
//	major=major;
//}

Student::Student(const Student& obj)
{
	ID=obj.ID;
	name=obj.name;
	sex=obj.sex;
	birthday=obj.birthday;
	grade=obj.grade;
	stu_class=obj.stu_class;
	institute=obj.institute;
	major=obj.major; 
}

Student::~Student()
{
	cout<<"Student object is being deleted\n"<<endl;
}

void Student::SetInfo(string ID, string name, char sex, string birthday, int grade, string stu_class, string institute, string major)
{
	this->ID=ID;
	this->name=name;
	this->sex=sex;
	this->birthday=birthday;
	this->grade=grade;
	this->stu_class=stu_class;
	this->institute=institute;
	this->major=major;
}

void Student::Show()
{
	cout<<"学生"<<name<<"的基本信息如下:"<<endl;
	cout<<"\t学号:"<<ID<<endl;
	cout<<"\t性别:"<<sex<<endl;
	cout<<"\t出生日期:"<<birthday<<endl;
	cout<<"\t年级:"<<grade<<endl;
	cout<<"\t班级:"<<stu_class<<endl;
	cout<<"\t院系:"<<institute<<endl;
	cout<<"\t专业:"<<major<<"\n"; 
} 

int Student::Check_input()
{
	int length;
	length=ID.length();
	if(length!=7){
		cout<<"输入错误:学号格式不正确!"<<endl;
		if( !(sex=='F'||sex=='M'||sex=='f'||sex=='m') ){
		cout<<"输入错误:性别格式不正确!"<<endl; 
	    }
		return -1; 
	} 
	return 1;
} 

//程序的主函数 
int main()
{
	string ID;
	string name;
	char sex;
	string birthday;
	string stu_class;
	string institute;
	string major;
	int grade;
	int n;
	char flag; //标识学生信息是否进行修改 
	cout<<"请输入要输入信息的学生个数:";
	cin>>n;
	Student *stu[n];  //关于c++对象数组知识的总结参见  https://blog.csdn.net/u012397189/article/details/78784928
	for(int i=0;i<n;i++){
		cout<<endl<<"请输入学生"<<i+1<<"的信息:"<<endl;
		cout<<"学号:";
		cin>>ID;
		cout<<flush<<"姓名:";
		cin>>name;
		cout<<flush<<"性别(F/M):";
		cin>>sex; 
		cout<<flush<<"出生日期 Year-month:";
		cin>>birthday;
		cout<<flush<<"年级:";
		cin>>grade;
		cout<<flush<<"班级:";
		cin>>stu_class;
		cout<<flush<<"院系:";
		cin>>institute;
		cout<<flush<<"专业:";
		cin>>major;
		stu[i]=new Student(ID,name,sex,birthday,grade,stu_class,institute,major); 
		if(stu[i]->Check_input()<0){ 
			cout<<endl<<"是否需要对学生"<<name<<"的信息进行修改?Y/N"<<endl;
            cin>>flag;
	        if(flag=='Y'||flag=='y'){
			    cout<<"请输入修改后学生的信息:"<<endl;
				cout<<"学号:";
				cin>>ID;
				cout<<"姓名:";
				cin>>name;
				cout<<"性别(F/M):";
				cin>>sex; 
				cout<<"出生日期 Year-month:";
				cin>>birthday;
				cout<<"年级:";
				cin>>grade;
				cout<<"班级:";
				cin>>stu_class;
				cout<<"院系:";
				cin>>institute;
				cout<<"专业:";
				cin>>major;
		        stu[i]->SetInfo(ID,name,sex,birthday,grade,stu_class,institute,major);
			}
		} 
	}
	cout<<endl<<"以下是所有学生的基本信息:\n"<<endl; 
	for(int i=0;i<n;i++){
		stu[i]->Show();
		delete stu[i];   //new出来的空间需要手动释放,,delete[] 用于释放无参构造函数new出来的数组,对于有参数的需要一个一个手动释放 
	} 
	return 0;
}

运行结果截屏:

c++ 实现学生类设计

参考资料:

c++构造函数的默认参数
c++动态对象数组的知识总结
c++对象数组中delete 和 delete[] 的区别
c++对象数组调用构造/析构函数的方法

相关标签: C++之旅 c++