Program practice: Define classes and member functions with member functions

Source: Internet
Author: User
Tags mygradebook

Program practice: Define classes and member functions with member functions

Now we can start with an example composed of the GradeBook class and the main function. This example is the first in a series of step-by-step examples. These examples are explained through the subsequent blog, and finally a GradeBook class with many functions.

Define a member function with no parameters

Here, the GradeBook class indicates that the instructor can manage the score book of the student examination, and a GradeBook object is created in the main function. the main function uses this object and its member function to display a message on the screen that welcomes the instructor to enter the score book program.

PS: The key word class is followed by the class name GradeBook.By convention, a user-defined class name starts with an uppercase letter. To enhance readability, the first letter of each subsequent word in the class name is also capitalized.. At the same time,The body of each class is enclosed in a pair of braces ({And}). The class definition ends with a semicolon..

// Define class GradeBook with a member function displayMessage;// Create a GradeBook object and call its displayMessage function.#include <iostream>using std::cout;using std::endl;// GradeBook class definitionclass GradeBook{public:   // function that displays a welcome message to the GradeBook user   void displayMessage()   {      cout << "Welcome to the Grade Book!" << endl;   } // end function displayMessage}; // end class GradeBook  // function main begins program executionint main(){   GradeBook myGradeBook; // create a GradeBook object named myGradeBook   myGradeBook.displayMessage(); // call object's displayMessage function   return 0; // indicate successful termination} // end main
Test Results

Define a member function with Parameters

Here, we redefine the GradeBook class. Its displayMessage member function uses the course name as part of the welcome message, this new member function displayMessage specifies a parameter that represents the name of the course to be output.

// Define class GradeBook with a member function that takes a parameter;// Create a GradeBook object and call its displayMessage function.#include <iostream>using std::cout; using std::cin;using std::endl;#include <string> // program uses C++ standard string classusing std::string;using std::getline;// GradeBook class definitionclass GradeBook{public:   // function that displays a welcome message to the GradeBook user    void displayMessage( string courseName )   {      cout << "Welcome to the grade book for\n" << courseName << "!"          << endl;   } // end function displayMessage}; // end class GradeBook  // function main begins program executionint main(){   string nameOfCourse; // string of characters to store the course name   GradeBook myGradeBook; // create a GradeBook object named myGradeBook      // prompt for and input course name   cout << "Please enter the course name:" << endl;   getline( cin, nameOfCourse ); // read a course name with blanks   cout << endl; // output a blank line   // call myGradeBook's displayMessage function   // and pass nameOfCourse as an argument   myGradeBook.displayMessage( nameOfCourse );   return 0; // indicate successful termination} // end main
Test Results




For more discussions and exchanges on Program Language, please stay tuned to this blog and Sina Weibo songzi_tea.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.