People:name,age,setname(),setage(),getname(),getage()
student:id,setid(),getId()
teacher:id,setid(),getId()
Reader:month,tag,setMonth(),getMonth,setTag(),getTag(),showAll()
(注意:所有的数据成员都是Protected类型,方法为Public成员)
template<class T>
class Reader:public T
{
//...
};
也可以这样:
class People
{
protected:
string name;
int age;
public:
virtual void setname(string n)
{
name=n;
}
virtual void setage(int a)
{
age=a;
}
virtual string getname()
{
return name;
}
virtual int getage()
{
return age;
}
};
class EduPerson:public People
{
protected:
int id;
public:
virtual void setid(int i)=0;
virtual int getid()=0;
};
class Student:public EduPerson
{
public:
virtual void setid(int i)
{
id=i;
}
virtual int getid()
{
return id;
}
};class Teacher:public EduPerson
{
public:
virtual void setid(int i)
{
id=i;
}
virtual int getid()
{
return id;
}
};class Reader
{
int month,tag;
EduPerson* pReader;
bool m_bStu;
public:
Reader(bool bstu=true)
{
m_bStu=bstu;
if(bstu)
pReader=new Student;
else
pReader=new Teacher;
}
~Reader()
{
delete pReader;
}
void setid(int i)
{
pReader->setid(i);
}
int getid()
{
return pReader->getid();
}
void setMonth(int m)
{
month=m;
}
int getMonth()
{
return month;
}
void setTag(int t)
{
tag=t;
}
int getTag()
{
return tag;
}
void showAll()
{
printf(\"%s---,Month:%d,Tag:%d\n\",m_bStu?\"Students\":\"Teacher\",month,tag);
}
};
void DevideTest()
{
Reader tr;
tr.setid(3);
tr.setMonth(2);
tr.setTag(1);
tr.showAll();
}
void main()
{
DevideTest();
}
[此贴子已经被作者于2007-6-4 15:28:06编辑过]