#include <iostream>
using namespace std;
class Student{
protected:
int roll_no;
public:
void getNumber(){
cout << "Enter number\n";
cin >> roll_no;
}
int putNumber(){
return roll_no;
}
};
class Test:public virtual Student{
protected:
int m1,m2;
public:
Test(){
m1=m2=0;
}
Test(int a, int b){
m1=a;
m2=b;
}
void display(){
cout << "Mark 1: " << m1;
cout << "Mark 2: " << m2;
}
};
class Sports:public virtual Student{
protected:
int score;
public:
Sports(){
score=0;
}
Sports(int a){
score=a;
}
void display(){
cout << "Score: " << score;
}
};
class Total:public virtual Test,public virtual Sports{
private:
int total;
public:
Total(){
total=0;
}
int display(){
total=m1+m2+score;
return total;
}
};
int main(){
Test ob1(10,20);
Sports ob2(50);
Total ob3;
cout << ob3.display() << endl;
}
试图实现一个虚拟基类 Student。问题是求Test类的分数和Sports类的分数之和。但是,我得到 0 作为输出而不是 80。有人可以解释一下吗?