C++问题,为什么这个代码虽然没报错但是执行出来却有问题?

2025-04-30 10:46:08
推荐回答(1个)
回答1:

35行:cout << f << endl; 改为 cout << W.f << endl;
36行:avescore = f / 2;改为 avescore = W.f / 2;
你写的Student类继承了Scorea类,又在Student类中定义了一个Scorea类对象(见24行),而你在Student::inputdata()函数中调用的是W.inputscore()(见26行)。35行直接写f是指Student父类Scorea中f,这是继承来的,但是你在Student类没有给它赋值。所以,要用f,得用W.f。
此外,分数和学生不应该是继承关系。