Define class Course that contains a courseName data member and member functions to set and get its value - C++ Class

C++ examples for Class:Class Creation

Description

Define class Course that contains a courseName data member and member functions to set and get its value

Demo Code

#include <iostream>
#include <string>
class Course {//ww w  . j a  va 2 s .co m
 private:
    std::string courseName;

 public:
    // SETTERS
    void setCourseName(std::string name) { courseName = name; }

    // GETTERS
    std::string getCourseName() { return courseName; }

    // function that displays a welcome message
    void displayMessage() {
        std::cout << "Welcome to the grade book for\n" << getCourseName() << "!" << std::endl;
    }
};


int main(int argc, const char *argv[]) {
    std::string nameOfCourse;
    Course myCourse;

    // display initial value of courseName
    std::cout << "Initial course name is: " << myCourse.getCourseName()<< std::endl;

    // prompt for input and set course name
    std::cout << "\nPlease enter the course name: " << std::endl;
    std::getline(std::cin, nameOfCourse);

    myCourse.setCourseName(nameOfCourse);

    std::cout << std::endl;

    myCourse.displayMessage();
    return 0;
}

Result


Related Tutorials