Now, from an example consisting of a gradebook class and a main function, this example is the first of a series of examples that are explained in a follow-up post, and ultimately a gradebook class with many features.
to define a member function with no parameters
Here, the Gradebook class represents a score book for teachers to manage student exam scores, while a gradebook object is created in the main function . The main function uses this object and its member functions to display a message on the screen that welcomes the teacher's entry into the transcript program.
PS: The keyword class is followed by the class name Gradebook. by convention, a user-defined class name begins with an uppercase letter, and for readability, the first letter of each subsequent word in the class name is capitalized . At the same time, the body of each class is surrounded by a pair of curly braces ({and}). The definition of the class ends with a semicolon .
Define class Gradebook with a member function displaymessage;//Create a Gradebook object and call its DisplayMessage f Unction. #include <iostream>using std::cout;using std::endl;//gradebook class Definitionclass GradeBook{public: //function that is 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 Gra Debook object named Mygradebook mygradebook.displaymessage ();//Call object ' s DisplayMessage function return 0 ; Indicate successful termination}//End Main
Test results
to define a member function with parameters
Here, we redefine the Gradebook class, whose DisplayMessage member function takes the course name as part of the welcome message, and the new member function displaymessage specifies a formal parameter that represents the name of the course to be exported.
Define class Gradebook with a member function, takes a parameter;//Create a Gradebook object and call its Displaym essage function. #include <iostream>using std::cout; Using std::cin;using Std::endl; #include <string>//program uses C + + standard string classusing std::string;using St d::getline;//Gradebook class Definitionclass Gradebook{public://function that displays a welcome message to the Grade Book user void DisplayMessage (string coursename) {cout << ' Welcome to the grade book for\n ' << Co Ursename << "!" << Endl; }//End Function DisplayMessage}; End Class Gradebook//function main begins program Executionint main () {string nameofcourse;//String of character S 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 Mygrade Book.displaymessage (Nameofcourse); return 0; Indicate successful termination}//End Main
Test results
For more discussion and exchange on program language, please follow this blog and Sina Weibo songzi_tea.
Program Practice: Define a class with member functions