Definition of class Course that determines a class average. - C++ Class

C++ examples for Class:Member Function

Description

Definition of class Course that determines a class average.

Demo Code

                   
#include <string>
#include <iostream>
class Course {/*from  w w  w .j  a  va 2  s .co  m*/
 private:
    std::string courseName;
                   
 public:
    Course(std::string);
                   
    // SETTERS
    void setCourseName(std::string);
                   
    // GETTERS
    std::string getCourseName();
                   
    void displayMessage();
    void determineClassAverage();
};
                   
Course::Course(std::string name) { setCourseName(name); }
// SETTERS
void Course::setCourseName(std::string name) {
    if (name.length() <= 25)
        courseName = name;
    else {
        courseName = name.substr(0, 25);
        std::cout << "Name \"" << name << "\" exceeds maximum length (25).\n";
        std::cout << "Limiting courseName to first 25 characters.\n" << std::endl;
    }
}
// GETTERS
std::string Course::getCourseName() { return courseName; }
                   
void Course::displayMessage() {
    std::cout << "Welcome to the grade book for\n" << getCourseName() << "!\n" << std::endl;
}
// determine class average based on 10 grades entered by user
void Course::determineClassAverage() {
    int total;
    int gradeCounter;
    int grade;
    int average;
                   
    total = 0;
    gradeCounter = 1;
                   
    while (gradeCounter <= 10) {
        std::cout << "Enter grade: ";
        std::cin >> grade;
        total += grade;
        gradeCounter++;
    }
                   
    average = total / 10;
                   
    std::cout << "\nTotal of all 10 grades is " << total << std::endl;
    std::cout << "Class average is " << average << std::endl;
}
                   
int main(int argc, const char *argv[]) {
    Course myCourse("C++");
                   
    myCourse.displayMessage();
    myCourse.determineClassAverage();
    return 0;
}

Result


Related Tutorials