C++ examples for Class:Class Creation
Define class interface first
#include <string> /*from ww w .j a v a 2 s .c o m*/ class Course { private: std::string courseName; public: Course(std::string); // SETTERS void setCourseName(std::string); // GETTERS std::string getCourseName(); void displayMessage(); }; #include <iostream> // Constructor Course::Course(std::string name) { setCourseName(name); } // SETTERS void Course::setCourseName(std::string name) { courseName = name; } // GETTERS std::string Course::getCourseName() { return courseName; } void Course::displayMessage() { std::cout << "Welcome to the grade book for\n" << getCourseName() << "!" << std::endl; } int main(int argc, const char *argv[]) { Course gradeBook1("C++."); Course gradeBook2("Java"); std::cout << "gradeBook1 created for course: " << gradeBook1.getCourseName(); std::cout << "\ngradeBook2 created for course: " << gradeBook2.getCourseName() << std::endl; return 0; }