溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

GradeBook類(lèi)怎么定義

發(fā)布時(shí)間:2022-04-06 16:21:38 來(lái)源:億速云 閱讀:298 作者:iii 欄目:編程語(yǔ)言

這篇文章主要講解了“GradeBook類(lèi)怎么定義”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“GradeBook類(lèi)怎么定義”吧!

定義具有無(wú)參數(shù)的成員函數(shù)

這里,GradeBook類(lèi)表示可供教師管理學(xué)生考試成績(jī)的成績(jī)簿,而在main函數(shù)創(chuàng)建了一個(gè)GradeBook對(duì)象.main函數(shù)使用這個(gè)對(duì)象和它的成員函數(shù),在屏幕上顯示一條歡迎教師進(jìn)入成績(jī)簿程序的信息.

PS:關(guān)鍵字class后跟類(lèi)名GradeBook.按照慣例,用戶定義的類(lèi)名字以大寫(xiě)字母開(kāi)頭,而且為了增強(qiáng)可讀性,類(lèi)名中每個(gè)隨后的單詞其首字母也為大寫(xiě).同時(shí),每個(gè)類(lèi)的體包圍在一對(duì)花括號(hào)中({和}).類(lèi)的定義以分號(hào)結(jié)束.

// 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 definition
class 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 execution
int main()
{
   GradeBook myGradeBook; // create a GradeBook object named myGradeBook
   myGradeBook.displayMessage(); // call object's displayMessage function	
   return 0; // indicate successful termination
} // end main

測(cè)試結(jié)果

GradeBook類(lèi)怎么定義

定義具有參數(shù)的成員函數(shù)

在這里,我們重新定義了GradeBook類(lèi),它的displayMessage成員函數(shù)將課程名稱作為歡迎消息的一部分,這個(gè)新的成員函數(shù)displayMessage規(guī)定了一個(gè)表示要輸出的課程名稱的形參.

// 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 class
using std::string;
using std::getline;

// GradeBook class definition
class 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 execution
int 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

感謝各位的閱讀,以上就是“GradeBook類(lèi)怎么定義”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)GradeBook類(lèi)怎么定義這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI