Answer to Question #272341 in C++ for Imran

Question #272341

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).

1
Expert's answer
2021-11-28T05:31:01-0500

Source code

#include <iostream>




using namespace std;




class Student{
    private:
    	int rollno;
    
    public:
    	void getnum(){
    		cout<<"\nEnter roll no: ";
    		cin>>rollno;
    	}
    	void putnum(){
    		cout<<"\nRoll no is: "<<rollno;
    	}


};
class Test:public Student
{
    protected:
    	int mark1;
    	int mark2;
    	int mark3;
    public:
    	void getmarks(){
    		cout<<"Enter mark 1: ";
    		cin>>mark1;
    		cout<<"Enter mark 2: ";
    		cin>>mark2;
    		cout<<"Enter mark 3: ";
    		cin>>mark3;
    	}
    	void putmarks(){
    		cout<<"Mark 1: "<<mark1<<"\n";
    		cout<<"Mark 2: "<<mark2<<"\n";
    		cout<<"Mark 3: "<<mark3<<"\n";
    	}


};
class Sports:public Student
{
    protected:
    	int score;
    public:
    	void getscore(){
    		score=-1;
    		while(score<0 || score>100){
    			cout<<"Enter score: ";
    			cin>>score;
    		}
    	}
    	void putscore(){
    		cout<<"Score: "<<score<<"\n";
    	}


};




class Result :public Test, public Sports
{
    private:
    	int total;
    public:
    	void display(){
    		int total=score+mark1+mark2+mark3;
    		cout<<"\nThe total (marks + score): "<<total;
    	}
};




int main(){
	Result res;
	Student s;
	s.getnum();
	res.getmarks();
	res.getscore();
	res.putmarks();
	res.putscore();
	res.display();


	return 0;
}


Output





Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS