#include <iostream.h>
class student
{
public:
student(char * na="no name")
{
name=na;
semesterHours=0;
average=0.0;
}
void addCourse(int hours,float grade)
{
average=(semesterHours*average+grade);
average/=semesterHours;
}
int getHours()
{
return semesterHours;
}
float getAverage()
{
return average;
}
void display()
{
cout<<"name:"<<name<<endl;
cout<<"semesterHours:"<<semesterHours<<endl;
cout<<"average:"<<average<<endl;
}
protected:
char * name;
int semesterHours;
float average;
};
class GraduateStudent:public student
{
public:
GraduateStudent( char * na="no name",int a):student(na)
{
qualifierGrade=a;
}
int getQulifier()
{ if(average>=1)
qualifierGrade=90;
else
qualifierGrade=80;
return qualifierGrade;
}
void showQulifier()
{
cout<<"qualifierGrade:"<<getQulifier()<<endl;
}
protected:
int qualifierGrade;
};
void main()
{
student mm("beckhame");
GraduateStudent nn("nn",8);
mm.addCourse(3,2.5);
mm.display();
cout<<"\n"<<endl;
nn.addCourse(3,3.0);
nn.display();
nn.getQulifier();
nn.showQulifier();
}
红色为出错的地方,但是如果我把派生类构造函数char * na="no name"和int a的位置互换的话,程序错误消除,大家说,这是为什么