//在头文件CStudent.h里。
#include <iostream>
using namespace std;
class CStudent
{
public:
CStudent()
{
strcpy_s(m_iName,"***");
m_iSex='f';
m_iGrade=1;
m_iClass=1;
}
char * getName(){return m_iName;}
char getSex(){return m_iSex;}
unsigned short getGrade(){return m_iGrade;}
unsigned short getClass(){return m_iClass;}
void setName(char * iName){strcpy_s(m_iName,iName);}
void setSex(char iSex){m_iSex=iSex;}
void setGrade(unsigned short iGrade){m_iGrade=iGrade;}
void setClass(unsigned short iClass){m_iClass=iClass;}
void SetInfor(char * Name,char Sex,unsigned short Grade,unsigned short Class);
void ShowInfor();
private:
char m_iName[10];
char m_iSex;
unsigned short m_iGrade;
unsigned short m_iClass;
};
void CStudent::SetInfor(char * Name,char Sex,unsigned short Grade,unsigned short Class)
{
strcpy_s(m_iName,Name);
m_iSex=Sex;
m_iGrade=Grade;
m_iClass=Class;
}
void CStudent::ShowInfor()
{
cout<<"姓名:"<<CStudent::getName()<<" "
<<"性别:"<<CStudent::getSex()<<" "
<<CStudent::getGrade()<<"年"<<CStudent::getClass()<<"班"<<endl;;
}
//在源文件main.cpp里。
#include"CStudent.h"
using namespace std;
int main()
{
CStudent stu[3];
cout<<"请输入学生的姓名、性别、年级和班级:"<<endl;
char*cName;
char cSex;
unsigned short cGrade,CClass;
for(int i=0;i<3;i++)
{
cin>>cName>>cSex>>cGrade>>CClass;
stu[i].SetInfor(cName,cSex,cGrade,CClass);
}
for(int i=0;i<3;i++)
stu[i].ShowInfor();
return 0;
}