Answer to Question #218612 in C++ for bhunu

Question #218612

A college offers diploma course with five set modules each. Define a class Student as an ADT that uses separate files for the interface and the implementation. This class represents a record of one student’s registration and results. For each Student the following information should be kept as member variables:

the student’s name, ID, student number, the name of the diploma, average mark, an array with the five module codes for the diploma, and another array with five results - one for each of the modules.

Class Student should also have a member function calcAverage() to calculate the average for the five results and a member function pass() to determine whether or not a student has passed the module. A student only passes a diploma course if he or she has obtained more than 50% for all the modules, regardless of his or her average mark. Member function pass()should return a boolean value.

.


1
Expert's answer
2021-07-19T04:25:00-0400

student.h



#ifndef STUDENT_H
#define STUDENT_H
#include <iostream>
using namespace std;

class Student {
    private:
        // Member variables
        string studentName; // student’s name
        int ID;             // ID 
        int studentNumber;  // student number
        string diplomaName; // name of the diploma
        double averageMark; // average mark
        string modules[5];  // an array with the five module codes for the diploma
        int results[5];     // an array with five results - one for each of the modules

    public:
        // Default constructor that initialises all string variables 
        // to empty strings and all other variable types to 0.
        Student();

        // Destructor to print a message “Bye!”
        ~Student();

        // Method to calculate the average for the five results.
        // It returns the average of 5 results stored in results[5].
        double calcAverage();

        // Method to determine whether or not a student has passed the module.
        // It returns true if every value in results is >=50 otherwise returns false.
        bool pass();

        // Method to display a student’s results after the testing.
        // It displays a students name, student#, diploma name, results for each module
        // and pass or fail
        void displayResults();

        // Accessors
        string getStudentName();            // For student’s name
        int getID();                        // For student’s ID
        int getStudentNumber();             // For student’s number
        string getDiplomaName();            // For names of the diploma
        double getAverageMark();            // For average mark
        string getModuleName(int index);    // Get the module name by index

        // Mutators
        void setStudentName(string name);       // For student’s name
        void setID(int id);                     // For student’s ID
        void setStudentNumber(int num);         // For student’s number
        void setDiplomaName(string dipName);    // For names of the diploma
        void setAverageMark(double mark);       // For average mark
        void setExamResult(int r, int index);   // Set result by index

        // Overload the stream insertion << to output an object of class Student 
        // containing values for all the member variables
        friend ostream &operator << (ostream &outs, const Student &s);
};

student.cpp


#include "student.h"
#include <iostream>
#include <string>

using namespace std;

// Default constructor
Student::Student() {
    // Initialise all string variables 
    // to empty strings and all other variable types to 0.
    studentName = "";
    ID = 0;
    studentNumber = 0;
    diplomaName = "";
    averageMark = 0;
    for (int i = 0; i < 5; i++) {
        modules[i] = "";
        results[i] = 0;
    }
}

// Destructor
Student::~Student() {
    // Display Bye! message
    cout << "Bye!" << endl;
}

// Method to calculate the average for the five results.
double Student::calcAverage() {
    double temp = 0.0;
    // Loop over the array of results
    for (int i = 0; i < 5; i++) {
        // Add result
        temp += results[i];
    }
    // Calculate average
    averageMark = temp / 5.0;
    // Return calculated average
    return temp / 5.0;
}

// Method to determine whether or not a student has passed the module.
bool Student::pass() {
    // Loop over the array of results
    for (int i = 0; i < 5; i++) {
        // Check if every value in results is >= 50
        if (results[i] < 50) {
            // If not, returns false
            return false;
        }
    }

    // Otherwise return True
    return false;
}

// Method to display a student’s results after the testing.
void Student::displayResults() {
    // Display name, number, diploma names
    cout << studentName << endl;
    cout << studentNumber << endl;
    cout << diplomaName << endl;
    // Display modules name
    for (int i = 0; i < 5; i++) {
        cout << modules[i] << ": " << results[i] << endl;
    }
    // Display average 
    cout << "AVERAGE MARK: " << averageMark << endl;

    // Display whether passed or failed
    if (pass()) {
        cout << "You have PASSED." << endl;
    } else {
        cout << "You have FAILED." << endl;
    }

    cout << endl;
}

// Accessors
// Get student’s name
string Student::getStudentName() {
    return studentName;
}

// Get student’s ID
int Student::getID() {
    return ID;
}

// Get student’s number
int Student::getStudentNumber() {
    return studentNumber;
}

// Get names of the diploma
string Student::getDiplomaName() {
    return diplomaName;
}

// Get average mark
double Student::getAverageMark() {
    return averageMark;
}

// Get the module name by index
string Student::getModuleName(int index) {
    return modules[index];
}

// Mutators
// Set student’s name
void Student::setStudentName(string name) {
    studentName = name;
}

// Set student’s ID
void Student::setID(int id) {
    ID = id;
}

// Set student’s number
void Student::setStudentNumber(int num) {
    studentNumber = num;
}

// Set names of the diploma
void Student::setDiplomaName(string dipName) {
    diplomaName = dipName;
    if (diplomaName == "Garden Design") {
        for (int i = 0; i < 5; i++) {
            modules[i] = "G" + to_string(i + 1);
        }
    } else if (diplomaName == "Gourmet Cooking") {
        for (int i = 0; i < 5; i++) {
            modules[i] = "C" + std::to_string(i + 1);
        }
    }
}

// Set average mark
void Student::setAverageMark(double mark) {
    averageMark = mark;
}

// Set the result by index
void Student::setExamResult(int r, int index) {
    results[index] = r;
}

// Overloaded stream insertion << operator
ostream& operator << (ostream &outs, const Student &s) {
    bool pass = true;

    // Write student’s name, ID, student number, the name of the diploma, and average mark.
    outs << "Student name: " << s.studentName << endl;
    outs << "Student ID: " << s.ID << endl;
    outs << "Student number: " << s.studentNumber << endl;
    outs << "Diploma name: " << s.diplomaName << endl;
    // outs << endl;
    for (int i = 0; i < 5; i++) {
        outs << s.modules[i] << ": " << s.results[i] << endl;
        // For pass/fail result
        if (s.results[i] < 50) {
            pass = false;
        }
    }

    outs << "AVERAGE MARK: " << s.averageMark << endl;
    if (pass) {
        outs << "Result: PASSED" << endl << endl;
    } else {
        outs << "Result: FAILED" << endl << endl;
    }
    
    return outs;
}

main.cpp


#include <fstream>
#include <iostream>
#include "student.h"

using namespace std;

int main() {
    // Create an array of 3 objects of type Student
    Student students[3];

    // Set their names
    students[0].setStudentName("John Martin");
    students[1].setStudentName("Busi Molefe");
    students[2].setStudentName("Sean Naidoo");

    // Set their IDs
    students[0].setID(78120189);
    students[1].setID(81011201);
    students[2].setID(69812018);

    // Set their numbers
    students[0].setStudentNumber(12345);
    students[1].setStudentNumber(23456);
    students[2].setStudentNumber(34567);

    // Set their diploma names
    students[0].setDiplomaName("Garden Design");
    students[1].setDiplomaName("Gourmet Cooking");
    students[2].setDiplomaName("Garden Design");

    // Display the information of 3 students stored
    for (int i = 0; i < 3; i++) {
        cout << students[i].getStudentName() << endl;
        cout << students[i].getID() << endl;
        cout << students[i].getStudentNumber() << endl;
        cout << students[i].getDiplomaName() << endl;

        // Ask user to enter results of each course
        cout << "Enter the results for each course: " << endl;
        for (int j = 0; j < 5; j++) {
            int mark;
            cout << students[i].getModuleName(j) << ": ";
            cin >> mark;
            // Set the result
            students[i].setExamResult(mark, j);
        }
        cout << endl;
    }

    // Calculate averagae
    students[0].calcAverage();
    students[1].calcAverage();
    students[2].calcAverage();

    // Write data to file (RegisteredStudentsResults)
    ofstream fout;
    fout.open("RegisteredStudentsResults");

    // Check if successfully written
    if (fout.fail()) {
        cout << "Failed to open output file." << endl;
        exit(1);
    }

    fout << students[0] << students[1] << students[2];
    fout.close();

    cout << endl;
    cout << "=======================================================" << endl;
    cout << "======================= RESULTS =======================" << endl;
    cout << "=======================================================" << endl;
    
    // Display all the information stored
    students[0].displayResults();
    students[1].displayResults();
    students[2].displayResults();

    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