The class student contains roll number, name, and course as data members and Input_student and display_student as member functions. Create a class exam and publicly inherit it from the student class. The derived class contains an array of marks and no_of_subjects as data members and input_marks and display_result as member functions. Overload “<=”, “()” and “+=” operators and use the overloaded operators in the main function. Create an array of objects of the exam class and display the result of 5 students.
#include <iostream>
#include <string>
using namespace std;
class Student{
private:
int rollNumber;
string name;
string course;
public:
void input_student(){
cout<<"Enter the student roll number: ";
cin>>rollNumber;
cin.ignore();
cout<<"Enter the student name: ";
getline(cin,name);
cout<<"Enter the student course: ";
getline(cin,course);
}
void display_student(){
cout<<"The student roll number: "<<rollNumber<<"\n";
cout<<"The student name: "<<name<<"\n";
cout<<"The student course: "<<course<<"\n";
}
Student & operator+=(int value){
this->rollNumber+=value;
return *this;
}
};
class Exam:public Student{
private:
// array of marks and no_of_subject
int marks[100];
int no_of_subject;
public:
void input_marks(){
cout<<"Enter the number of subjects: ";
cin>>no_of_subject;
for(int i=0;i<no_of_subject;i++){
cout<<"Enter the mark of subject "<<(i+1)<<": ";
cin>>marks[i];
}
}
void display_result(){
cout<<"The number of subjects: "<<no_of_subject<<"\n";
for(int i=0;i<no_of_subject;i++){
cout<<"The mark of subject "<<(i+1)<<": "<<marks[i]<<"\n";
}
}
friend bool operator<= (Exam &Exam1, Exam &Exam2){
return Exam1.no_of_subject<=Exam2.no_of_subject;
}
};
int main(){
//5 students.
Exam exams[5];
int n=2;
for(int i=0;i<n;i++){
cout<<"Enter the detail information for student "<<(i+1)<<"\n";
exams[i].input_student();
exams[i].input_marks();
cout<<"\n";
}
for(int i=0;i<n;i++)
{
for(int j=1;j<n-i;j++)
{
if(exams[j-1]<=exams[j])
{
Exam temp=exams[j-1];
exams[j-1]=exams[j];
exams[j]=temp;
}
}
}
cout<<"\n\n";
for(int i=0;i<n;i++){
exams[i]+=1000;
exams[i].display_student();
exams[i].display_result();
cout<<"\n";
}
system("pause");
return 0;
}
Comments
Leave a comment