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;
#define MAX 10
class student
{
private:
char name1[30];
int rollNo1;
int total1;
float average1;
char name2[30];
int rollNo2;
int total2;
float average2;
public:
void getDetails(void);
void putDetails(void);
};
//member function definition
void student::getDetails(void){
cout << "Enter name of student1: " ;
cin >> name1;
cout << "Enter roll number: ";
cin >> rollNo1;
cout << "Enter total marks of the three subjects: ";
cin >> total1;
average1=(float)total1/3;
cout << "Enter name of student2: " ;
cin >> name2;
cout << "Enter roll number: ";
cin >> rollNo2;
cout << "Enter total marks of the three subjects: ";
cin >> total2;
average2=(float)total2/3;
}
//friend function definition
void student::putDetails(void){
if(average1> average2){
cout << "New Student details:\n";
cout << "Name:"<< name1 << ",Roll Number:" << rollNo1 << ",Total:" << total1 << ",Average:" << average1;}
else{
cout << "New Student details:\n";
cout << "Name:"<< name2 << ",Roll Number:" << rollNo2 << ",Total:" << total2 << ",Average:" << average2;
}
}
int main()
{
student std[MAX];
int loop;
cout << "Enter details of the 2 students "<< ":\n";
std[loop].getDetails();
cout << endl;
cout << "Details of student " << ":\n";
std[loop].putDetails();
return 0;
}
Comments
Leave a comment