求教!!!!!(100分)

  • 主题发起人 主题发起人 小坤
  • 开始时间 开始时间

小坤

Unregistered / Unconfirmed
GUEST, unregistred user!
设计一个多态数组,该数组可以存放学校中的各类人员,如学生,教师,临时工等。
编 写一个程序演示这个多态数组的用法?
 
看到你的名字里有一个坤,我就来回答你的问题。
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();
}
}
大致上是这个样子,我没有用编译器来确定是否通过运行,
只是随便打出来的。
 
to:whaoye
你的程序看上去感觉是用c编写的,因此是不是要进入c++build里编译!
 
我是随便写的,不用去编译啊,
我只是告诉他堕多态的意思。
 
提供参考哦!
#include <iostream.h>
#include <stdlib.h>
const int row = 100;
const int s_col = 3;
const int t_col = 4;
const int w_col = 2;
class ARRAY {
public:
virtual void add(int a) = 0;
virtual void del(int d) = 0;
virtual void show(int x) = 0;
};
class STUDENT:public ARRAY {
public:
virtual void add(int s) {
cout<<"/tInput data of STUDENT."<<endl;
cout<<"/tFirst:";
cin>>table[0];
cout<<"/tSecond:";
cin>>table[1];
cout<<"/tThird:";
cin>>table[2];
}
virtual void del(int s);
virtual void show(int s);
protected:
int i;
float table[row][s_col];
};
void STUDENT::del(int s) {
for(i=0;i<=s_col-1;i++) table = 0;
}
void STUDENT::show(int s) {
for(i=0;i<=s_col-1;i++) {
cout<<"/t/t/ttable["<<s<<"]["<<i<<"] is "<<table<<endl;
}
}
//protected:
// int i;
// float table[row][s_col];
//};
class TEACHER: public ARRAY {
public:
virtual void add(int t) {
cout<<"/tInput data of TEACHER."<<endl;
cout<<"/tFirst:";
cin>>table[t][0];
cout<<"/tSecond:";
cin>>table[t][1];
cout<<"/tFhird:";
cin>>table[t][2];
cout<<"/tForth:";
cin>>table[t][3];
}
virtual void del(int t);
virtual void show(int t);
protected:
int i;
float table[row][t_col];
};
void TEACHER::del(int t) {
for(i=0;i<=t_col-1;i++) table[t] = 0;
}
void TEACHER::show(int t) {
for(i=0;i<=t_col-1;i++) {
cout<<"/t/t/ttable["<<t<<"]["<<i<<"] is "<<table[t]<<endl;
}
}
//protected:
// int i;
// float table[row][t_col];
//};
class WORKER: public ARRAY {
public:
virtual void add(int w) {
cout<<"/tInput data of WORKER."<<endl;
cout<<"/tFirst:";
cin>>table[w][0];
cout<<"/tSecond:";
cin>>table[w][1];
}
virtual void del(int w);
virtual void show(int w);
protected:
int i;
float table[row][w_col];
};
void WORKER::del(int w) {
for(i=0;i<=w_col-1;i++) table[w] = 0;
}
void WORKER::show(int w) {
for(i=0;i<=w_col-1;i++) {
cout<<"/t/t/ttable["<<w<<"]["<<i<<"] is "<<table[w]<<endl;
}
}
//protected:
// int i;
// float table[row][w_col];
//};
int main() {
STUDENT st;
TEACHER te;
WORKER wk;
ARRAY* ap;
ap = &amp;st;
ap->add(1);
ap->show(1);
ap->del(1);
ap->show(1);
ap = &amp;te;
ap->add(2);
ap->show(2);
ap->del(2);
ap->show(2);
ap = &amp;wk;
ap->add(3);
ap->show(3);
ap->del(3);
ap->show(3);
return 0;
}
 
接受答案了.
 
后退
顶部