Define class interface first - C++ Class

C++ examples for Class:Class Creation

Description

Define class interface first

Demo Code

                  
#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;
}

Result


Related Tutorials