设计评选优秀教师和学生的程序。类结构如下。输入一系列教师和学生的记录后,将优秀学生和教师的姓名列出来。类base:char name[8]; //姓名   含虚函数 isgood()类student 和 类teacher 是类base的派生类,类studernt,如果考试成绩超过90分,则调用函数isgood()返回true
类teacher,如果一年发表的论文超过了3篇,则调用isgood()返回true.我的程序:#include <iostream.h>
#include <string.h>
#include <conio.h>class base
{
protected:
char name[8];
public:
virtual void isgood()=0;
void getname()
{
cout<<"please enter  name:";
cin>>name;
}
void display()
{
cout<<"name is:"<<name<<endl;
}
};
class teacher:public base
{
protected:
int num;
bool flag1;
public:
void getarticle()
{
cout<<"please enter count of article:";
cin<<num;
}
bool isgood()
{
flag1=false;
if (num>3){flag1=true;return flag1;};
}};class student:public base
{
protected:
int score;
bool flag2;
public:
void getscore()
{
cout<<"please enter score :";
cin<<score;
}
bool isgood()
{
flag2=false;
if (score>90){flag2=true;return flag2;};
}
};
main()
{
int i,no;
base* base1[100];
teacher* teacher1[100];
student* student1[100]; cout <<"please enter number of students:";
cin>>no;

for(i=0;i<no;i++)
{
base1[i]->getname();
teacher1[i]=new teacher;
teacher1[i]->getarticle();
}}

解决方案 »

  1.   

    又有人来招民工了
    大概看了一下,你的main()有问题
    三个数祖都是指针数祖,base[i]没有分配内存就使用了,而teacher[i]分配了内存却没有释放
      

  2.   

    rengo说得对,你的变量声明不太对,应该这样
             base* base1=new base[100];
    这样才能产生100个base实例
      

  3.   

    class base中应 virtual bool isgood()=0;类studernt,类teacher中 isgood函数有问题,条件不满足时没有返回值。main() base[i]没有分配内存
      

  4.   

    base* base1[100];改成base base1[100];//不用new了,也不用删除,自动析构。
    或者改成base* base1=new base[100];再在main()的结尾加上delete[]base1;
      

  5.   

    大致看了一下,发现如下问题:
    1、virtual bool isgood()=0; 由于base为虚类,故不能建造实例对象;
    2、类studernt,类teacher中 isgood函数有问题,条件不满足时没有返回值。正如 feng   说的一样,只须将return移出if就行;
    3、我对题意还不是很了解,既然
             cout <<"please enter number of students:";
    cin>>no;  请问后面的循环是什么意思?为何根据学生数目来决定老师数目?I don't know!
    4、main()中存在很大的问题,对student和teacher进行赋值时,为何不调用getscore()和getarticle()?  而且没有进行判断是否优秀的循环,为什么?
    建意如下:
    const int MAX=100;
    main()
    {
    int i,nuOfst,nuOftec;
    teacher* teach[MAX];
    student* stud[MAX];
            
    cout <<"please enter number of students:";
    cin>>nuOfst;
             cout <<"please enter number of students:";
             cin>>nuOftec;

    for(i=0;i<nuOfst;i++)    //对学生、老师数据进行初始化
    {
    stud[i]->getname();
    stud[i]->getscore();
                      stud[i]->isgood();
             }
             for(i=0;i<nuOftec;i++)
             {
                      teach[i]->getname();
                      teach[i]->getarticle();
                      teach[i]->isgood();
             } 
         cout<<"The name of excellent student is:"<<endl;     
    //输出优秀学生、老师姓名         
             for(i=0;i<nuOfst;i++)
             {
                     if(stud[i].flag2)   stud[i]->display();
             }
      cout<<"The name of excellent teacher is:"<<endl;                  
             for(i=0;i<nuOftec;i++)
             {
                     if(teach[i].flag2)   teach[i]->display();
             }    }
    上面语句没有经过调试,因为手头无编译器。
    由于对C++不是很熟,有不正确之处请大家指正。