Answer to Question #183593 in C++ for Garima

Question #183593

Create a class student. Take data members: Student name, Registration Number and Total Marks. Create a function that will perform task student is pass or not in exam. If the student is Pass than it should display grade according to percentage.


1
Expert's answer
2021-04-20T10:16:48-0400
#include <iostream>
#include <string>
using namespace std;

class Student {
public:    
    Student(string name, int regNumber, int marks=0);
    string GetName() const {
        return name;
    }
    int GetRegistrationNumber() const {
        return registrationNumber;
    }
    int GetTotalMarks() const {
        return totalMarks;
    }
    void SetTotalMarks(int marks) {
        totalMarks = marks;
    }
    void PassExam() const;

private:
    string name;
    int registrationNumber;
    int totalMarks;
};

Student::Student(string name, int regNumber, int marks) :
    name(name), registrationNumber(regNumber), totalMarks(marks)
{}

void Student::PassExam() const
{
    cout << "The student " << name;
    if (totalMarks < 50) {
        cout << " has not pass the Exam";
    }
    else {
        cout << " has pass the Exam with grade ";
        string grade;
        if (totalMarks > 90)
            grade = "A";
        else if (totalMarks > 80)
            grade = "B";
        else if (totalMarks > 70)
            grade = "C";
        else if (totalMarks > 60)
            grade = "D";
        else
            grade = "E";
        cout << grade;
    }
    cout << endl;
}

int main()
{
    Student st1("Mary Smith", 12345, 45);
    Student st2("Bob Brown", 45678, 80);

    st1.PassExam();
    st2.PassExam();
    st1.SetTotalMarks(94);
    st1.PassExam();

}

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