第1关:设计一个学生信息类:
#includeusing namespace std; class StInfo { public: //在此处声明StInfo类 int SID; char *Name; char *Class; char *Phone; void SetInfo(int sid,char *name,char *cla,char *phone); void PrintInfo(); }; //在此处定义StInfo类 void StInfo::SetInfo(int sid,char *name,char* cla,char* phone){ SID=sid; Name=name; Class=cla; Phone=phone; } void StInfo::PrintInfo() { cout<<"学号:"< 第2关:设计一个长方形类 后段没有main函数,因为头歌平台自带了main函数,再加会报重复,若有需要可以自行添加。
#includeusing namespace std; class Rectangle { //在此处实现Rectangle类 private: int height; int width; public: void Set(int h,int w); int GetArea(); }; void Rectangle::Set(int h,int w) { height=h; width=w; } int Rectangle::GetArea() { return height*width; } Rectangle GetRect(int h,int w) { //返回一个 h*w 的 Rectangle 对象 Rectangle rect; rect.Set(h,w); return rect; } int GetRectArea(Rectangle rect) { //返回 rect 对象的面积 return rect.GetArea(); } 第3关:设计汽车类
#includeusing namespace std; //在此处实现一个汽车类 class Car { public: string door; string light; int speed; void opdoor() {door="ON";} void cldoor() {door="OFF";} void oplight() {light="ON";} void cllight() {light="OFF";} void spspeed() {speed+=10;} void nospeed() {speed-=10;} void printfcar() {cout<<"车门 "<< door< >cmds; Car car; car.door="OFF"; car.light="OFF"; car.speed=0; for(int i=0;i<25;i++) { if(cmds[i]=='1') car.opdoor(); if(cmds[i]=='2') car.cldoor(); if(cmds[i]=='3') car.oplight(); if(cmds[i]=='4') car.cllight(); if(cmds[i]=='5') car.spspeed(); if(cmds[i]=='6') car.nospeed(); } car.printfcar();



