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
#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();
}
Comments
Leave a comment