Answer to Question #173927 in C++ for Vimal D

Question #173927

Design a C++ program having a base class Student with data member rollno and member functions getnum() to input rollno and putnum() to display rollno. The class Test is derived from class Student with data member marks (Three subject marks) and member functions getmarks() to input marks and putmarks() to display marks. The class Sports is also derived from class Student with data member score (range between 100) 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-03-26T13:31:04-0400
#include <iostream>


using namespace std;


class Student{


public:
	Student(){}
	virtual void getnum(){
		cout<<"Enter roll no: ";
		cin>>rollno;
	}
	virtual void putnum(){
		cout<<"Roll no is: "<<rollno<<"\n";
	}
private:
	int rollno;
};
class Test:virtual public Student
{
public:
	Test(){}


	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";
	}
protected:
	int mark1;
	int mark2;
	int mark3;
};
class Sports:virtual public Student
{
public:
	Sports(){}
	void getscore(){
		score=-1;
		while(score<0 || score>100){
			cout<<"Enter score: ";
			cin>>score;
		}
	}
	void putscore(){
		cout<<"Score: "<<score<<"\n";
	}
protected:
	int score;//(range between 100)
};


class Result :public Test, public Sports
{
public:
	Result(){}


	void display(){
		int total=score+mark1+mark2+mark3;
		cout<<"The total (marks + score): "<<total<<"\n";
	}
private:
	int total;
};


int main(){
	Result result;
	result.getnum();
	result.getmarks();
	result.getscore();
	result.putmarks();
	result.putscore();
	result.display();


	system("pause");
	return 0;
}

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