Create a method in class to accept parameter - C++ Class

C++ examples for Class:Class Creation

Description

Create a method in class to accept parameter

Demo Code

#include <iostream>
#include <string>

class MyClass {/*from   w ww.ja v a2  s .  com*/
 public:
    void displayMessage(std::string courseName) {
        std::cout << "Welcome to the grade book for\n" << courseName << "!" << std::endl;
    }
};

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

    // prompt for and input course name
    std::cout << "Please enter the course name: ";
    std::getline(std::cin, nameOfCourse);  // read a course name with blanks
    std::cout << std::endl;

    myMyClass.displayMessage(nameOfCourse);
    return 0;
}

Result


Related Tutorials