Write a program having a base class Student with data member rollno and member
functions getnum() to input rollno and putnum() to display rollno. A class Test is derived from class Student with data member marks and member functions getmarks() to input marks and putmarks() to display marks. Class Sports is also derived from class Student with data member score and member functions getscore to input score and putscore() to display score. The class Result is inherited from two base classes, class Test and class Sports with data member total and a member function display() to display rollno, marks, score and the total(marks + score).
#include <iostream>
using namespace std;
class Student{
protected:
int rollno;
public:
Student(){}
void getnum(){
cout<<"Input rollno: ";
cin>>rollno;
}
void putnum(){
cout<<rollno<<endl;
}
};
class Test: public Student{
protected:
int marks;
public:
Test(): Student(){}
void getmarks(){
cout<<"Input marks: ";
cin>>marks;
}
void putmarks(){
cout<<marks<<endl;
}
};
class Sports: public Student{
protected:
int score;
public:
Sports(): Student(){}
void getscore(){
cout<<"Input score: ";
cin>>score;
}
void putscore(){
cout<<score<<endl;
}
};
class Result: public Sports, public Test{
int total;
public:
Result(): Sports(), Test(){
Test::getnum();
getmarks();
getscore();
total = score + marks;
}
void display(){
cout<<"Rollno: "; Test::putnum();
cout<<"Marks: "; Test::putmarks();
cout<<"Score: "; Sports::putscore();
cout<<"Total: "<<total<<endl;
}
};
int main(){
Result result;
result.display();
return 0;
}
Comments
Leave a comment