定义??(10分)

  • 主题发起人 主题发起人 xiaolan
  • 开始时间 开始时间
X

xiaolan

Unregistered / Unconfirmed
GUEST, unregistred user!
如何定义一个多态数组呢?
举个例子???
 
var
b:array of string;
begin
setlenth(b,4);
....
end;
 
写错了应该是setlength
 
若用类继承来实现呢???
那要怎么定义???
 
Class CSchoolPerson
{
public:
CSchoolPenson(){};
virtual CSchoolPenson(){};
public:
virtual void WhoAmI()
{
printf("i am a schoolperson/n");
}
}
Class CTeacher : public CSchoolPerson
{
public:
CTeacher(){};
virtual CTeacher(){};
public:
virtual void WhoAmI()
{
printf("i am a Teacher/n");
}
}
Class CStudent : public CSchoolPerson
{
public:
CStudent(){};
virtual CStudent(){};
public:
virtual void WhoAmI()
{
printf("i am a Student/n");
}
}
void main()
{
CSchoolTeacher *pteacher = new CTeacher;
CStudent *pstudent = new CStudent;
CArray<CPerson,CPerson*> ob;
ob.add(pteacher);
ob.add(pstudent);
for(int i = 0;
i < ob.GetSize();
i ++)
{
CPerson *pPerson = ob.GetAt(i);

pPerson->WhoAmI();
}
}
大致上是这个样子,我没有用编译器来确定是否通过运行,
只是随便打出来的。
 
后退
顶部