用程序编写
定义学生类student,类中包含学生的基本信息:学号、姓名、性别、年级等数据成员,student类中有多个构造函数:带默认值的构造函数、带参数构造函数,以及显示学生信息、修改学生信息等成员函数。建立对象并调用相应的成员函数。
程序代码:
#include<iostream> #include<cstring> using namespace std; class Student { private: int ID,Year; char Sex; char *Name; public: Student(int id,int year,char sex,char *name) { ID=id,Sex=sex,Year=year; Name=new char[strlen(name)+1]; strcpy(Name,name); } Student() { ID=1415240; Year=1; Sex='m'; Name="小明"; } ~Student() { delete[]Name; } void show_StudentInformation() { cout<<"姓名:"<<Name<<endl; cout<<"学号:"<<ID<<endl; cout<<"年级:"<<Year<<endl; cout<<"性别:"<<Sex<<endl; } int set_ID(int id) { ID=id; return ID; } int set_Year(int year) { Year=year; return Year; } char set_Sex(char sex) { Sex=sex; return Sex; } char set_Name(char *name) { Name=new char[strlen(name)+1]; strcpy(Name,name); } }; int main() { cout<<"输出学生信息,例如:"<<endl; Student stu1; stu1.show_StudentInformation(); cout<<endl; Student stu2(1415241,1,'m',"Miszuo"); stu2.show_StudentInformation(); stu2.set_ID(1415242);//修改信息 stu2.set_Name("Missun"); stu2.set_Sex('f'); stu2.set_Year(2); cout<<endl<<"改后信息为:"<<endl; stu2.show_StudentInformation(); cin.get(); }