定义一个学生类,其中有3个数据成员有学号、姓名、年龄,以及若干成员函数。同时编写主函数使用这个类,实现对学生数据的赋值和输出。并利用这个类建立一个对象数组,内放5个学生的数据,利用指针输出第1,3,5个学生的数据。 要求:使用构造函数和析构函数实现对数据的输入、输出。在头文件student.h中完成类的定义,在student.cpp中完成类的实现,在_tmain().cpp中完成主函数编写。
#include<iostream.h>
#include<string.h>
class student{
private:
int intID;
char charNAME[8];
short shoAGE;
public:
student(int,char *,short);
~student();
void show();
int getID();
void getNAME();
short getAGE();
void setID(int);
void setNAME(char *);
void setAGE(short);
};
student::student(int ID,char * NAME,short AGE){
intID=ID;
strcpy(charNAME,NAME);
shoAGE=AGE;
cout<<"建立了一个学生对象 序列号:"<<intID<<endl;
};
student::~student(){
cout<<"删除了一个学生对象"<<intID<<endl;
};
void student::show(){
cout<<"序列号"<<intID<<"姓名"<<charNAME<<"年龄"<<shoAGE<<endl;
}
int student::getID(){
return intID;
};
void student::getNAME(){
cout<<charNAME<<endl;
};
short student::getAGE(){
return shoAGE;
};
void student::setID(int ID){
intID = ID;
};
void student::setNAME(char *NAME){
strcpy(charNAME,NAME);
};
void student::setAGE(short AGE){
shoAGE=AGE;
};
void main(){
student a(4,"ww",22);
a.show();
a.setID(20030133);
a.setNAME("张三");
a.setAGE(22);
cout<<"学号"<<a.getID()<<endl;
a.getNAME();
cout<<"年龄"<<a.getAGE()<<endl;
}