Answer to Question #183594 in C++ for jainab

Question #183594

Q2. Define a class student with static data member and static function:

data members:


static admno integer

static sname 20 character

static totalMarks float

member function:

static Showdata() Function to display all the data members on the screen


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

class Student {
public:    
    Student(string name, int regNumber, float marks=0);
    string GetName() const {
        return name;
    }
    int GetRegistrationNumber() const {
        return registrationNumber;
    }
    float GetMarks() const {
        return marks;
    }
    void SetMarks(float marks);
    void PassExam() const;
    static void Showdata();


private:
    string name;
    int registrationNumber;
    float marks;


    static int admno;
    static char sname[20];
    static float totalMarks;
};


int Student::admno=11111;
char Student::sname[20] = "Jon Snow";
float Student::totalMarks = 0.0;

Student::Student(string name, int regNumber, float marks) :
    name(name), registrationNumber(regNumber), marks(marks)
{
    totalMarks += marks;
}

void Student::Showdata()
{
    cout << "Admno: " << admno << endl;
    cout << "Sname: " << sname << endl;
    cout << "Total marks: " << totalMarks << endl;
}


void Student::SetMarks(float marks) {
    totalMarks -= this->marks;
    this->marks = marks;
    totalMarks += this->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 (marks > 90)
            grade = "A";
        else if (marks > 80)
            grade = "B";
        else if (marks > 70)
            grade = "C";
        else if (marks > 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.SetMarks(94);
    st1.PassExam();
    cout << endl;

    Student::Showdata();
}

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