Answer to Question #229572 in C++ for Arup

Question #229572

Write a program to create a class to store details of a student ( name, roll and 3 subject marks ). Input details for 2 students and assign all the details of that student who is having higher average mark to a new student.

i) use member function

ii) use friend function

1
Expert's answer
2021-08-25T17:27:55-0400
#include<iostream>
using namespace std;
class StudentDetails{
	private:
		string name;
		int roll_no;
		int subject_marks[3];
	public:
		friend StudentDetails Compare(StudentDetails d);
		void input(){
			cout<<"Enter student name\n";
			cin>>name;
			cout<<"Enter student roll number\n";
			cin>>roll_no;
			cout<<"Enter the student's subject marks\n";
			for(int i=0; i<3; i++){
				cout<<"Subject "<<(i+1)<<endl;
				cin>>subject_marks[i];
			}
		}
		double average(){
			double total = 0.0;
				for(int i=0; i<3; i++){
				total += subject_marks[i];
			}
			return total /3;
		}
		void display(){
			cout<<"Student Name: \t"<<name<<endl;
			cout<<"Student Roll No \t "<<roll_no<<endl;
			cout<<"The marks of the student are:\n";
			for(int i=0; i<3; i++){
				cout<< subject_marks[i]<<endl;
			}
		}
};
StudentDetails Compare(StudentDetails d, StudentDetails newStudent){
	
	if(newStudent.average() > d.average() ){
		return newStudent;
	}
	else{
		return d;
	}
}
int main(){
	cout<<"Enter the details for student: 1\n";
	StudentDetails st1, st2, st3;
	st1.input();
	cout<<"Enter the details for student: 2\n\n\n";
	st2.input();
	st3 = Compare(st1, st2);
	cout<<"The details of the student with higher average mark are: \n";
	st3.display();
}

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