本来想来着,但是因为打印的时候还要转换一下。就偷懒了
成功会使人骄傲。如果你骄傲自大,你就会停止学习。不学习,人就停止了进步
#include <iostream.h>
class student
{
protected:
char *name;
int age;
char *sex;
public:
student(char *n,int a,char *s)
{
name=n;
age=a;
sex=s;
}
virtual void print()=0;
};
class Undergraduate:public student
{
public:
Undergraduate(char *n,int a,char *s,char *d):student(n,a,s)
{
degree=d;
}
void print()
{
cout<<"姓名:"<<name<<','<<"年龄:"<<age<<','<<"性别:"<<sex<<','<<"学位:"<<degree<<endl;
}
private:
char *degree;
};
class Graduate:public student
{
public:
Graduate(char *n,int a,char *s,char *d):student(n,a,s)
{
degree=d;
}
void print()
{
cout<<"姓名:"<<name<<','<<"年龄:"<<age<<','<<"性别:"<<sex<<','<<"学位:"<<degree<<endl;
}
private:
char *degree;
};
void main()
{
student *s;
Undergraduate u("王明",23,"男","本科");
Graduate g("刘军",27,"男","硕士");
s=&u;
s->print();
s=&g;
s->print();
}