Answer to Question #181895 in C++ for VARUN

Question #181895

Dynamic Objects and Run Time Polymorphism


Develop a C++ for the Student Examination Details having student class with data members like roll no, name, marks and grade. Member functions in this class are used for accept / display details of students. Create derived classes such as Engineering, Medical, Sciences etc.. calculate grade based on marks obtained by student. [Note: Use virtual function]


1
Expert's answer
2021-04-17T18:46:46-0400
#include <iostream>
using namespace std;
class Student{
    public:
    int roll_no, marks;
    string name;
    char grade;
    Student(){
        cout<<"\n\nEnter student name: ";
        cin>>this->name;
        cout<<"Enter student roll_no: ";
        cin>>this->roll_no;
        cout<<"Enter student total marks (out of 300): ";
        cin>>this->marks;
        this->setgrade();
    }
    virtual void setgrade(){
        switch(marks/30){
            case 10:;
            case 9:;
            case 8:;
            case 7: this->grade = 'A'; break;
            case 6: this->grade = 'B'; break;
            case 5: this->grade = 'C'; break;
            case 4: this->grade = 'D'; break;
            case 3:;
            case 2:;
            case 1: this->grade = 'E'; break;
            default: this->grade = 'F';
        }
    }
    virtual void display(){
    }
};
class Engineering: public Student{
    int mark;
    char Egrade;
    public:
    Engineering(): Student(){
        cout<<"Enter marks scored in engineering: ";
        cin>>mark;
    }
    void setgrade(){
        switch(mark/10){
            case 10:;
            case 9:;
            case 8:;
            case 7: this->Egrade = 'A'; break;
            case 6: this->Egrade = 'B'; break;
            case 5: this->Egrade = 'C'; break;
            case 4: this->Egrade = 'D'; break;
            case 3:;
            case 2:;
            case 1: this->Egrade = 'E'; break;
            default: this->Egrade = 'F';
        }
    }
    void display(){
        cout<<"Student name: "<<this->name;
        cout<<"\nStudent roll_no: "<<this->roll_no;
        cout<<"\nStudent total marks: "<<this->marks;
        cout<<"\nStudent grade: "<<this->grade;
        cout<<"\nMarks in Engineering: "<<this->mark;
        cout<<"\nGrade in Engineering: "<<this->Egrade;
    }
};
class Medical: public Student{
     int mark;
    char Mgrade;
    public:
    Medical(): Student(){
        cout<<"Enter marks scored in Medical: ";
        cin>>mark;
    }
    void setgrade(){
        switch(mark/10){
            case 10:;
            case 9:;
            case 8:;
            case 7: this->Mgrade = 'A'; break;
            case 6: this->Mgrade = 'B'; break;
            case 5: this->Mgrade = 'C'; break;
            case 4: this->Mgrade = 'D'; break;
            case 3:;
            case 2:;
            case 1: this->Mgrade = 'E'; break;
            default: this->Mgrade = 'F';
        }
    }
    void display(){
        cout<<"Student name: "<<this->name;
        cout<<"\nStudent roll_no: "<<this->roll_no;
        cout<<"\nStudent total marks: "<<this->marks;
        cout<<"\nStudent grade: "<<this->grade;
        cout<<"\nMarks in Medical: "<<this->mark;
        cout<<"\nGrade in Medical: "<<this->Mgrade;
    }
};
class Science: public Student{
    int mark;
    char Sgrade;
    public:
    Science(): Student(){
        cout<<"Enter marks scored in Science: ";
        cin>>mark;
    }
    void setgrade(){
        switch(mark/10){
            case 10:;
            case 9:;
            case 8:;
            case 7: this->Sgrade = 'A'; break;
            case 6: this->Sgrade = 'B'; break;
            case 5: this->Sgrade = 'C'; break;
            case 4: this->Sgrade = 'D'; break;
            case 3:;
            case 2:;
            case 1: this->Sgrade = 'E'; break;
            default: this->Sgrade = 'F';
        }
    }
    void display(){
        cout<<"Student name: "<<this->name;
        cout<<"\nStudent roll_no: "<<this->roll_no;
        cout<<"\nStudent total marks: "<<this->marks;
        cout<<"\nStudent grade: "<<this->grade;
        cout<<"\nMarks in Science: "<<this->mark;
        cout<<"\nGrade in Science: "<<this->Sgrade;
    }
};
int main(){
    Student* S;
    Engineering E;
    S = &E;
    S->setgrade();
    S->display();
    Medical M;
    S = &M;
    S->setgrade();
    S->display();
    Science C;
    S = &C;
    S->setgrade();
    S->display();
    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