GradeBook類怎么定義-創(chuàng)新互聯(lián)

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

專注于為中小企業(yè)提供網(wǎng)站制作、成都做網(wǎng)站服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)橫峰免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了上1000家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

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

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

PS:關(guān)鍵字class后跟類名GradeBook.按照慣例,用戶定義的類名字以大寫字母開頭,而且為了增強(qiáng)可讀性,類名中每個隨后的單詞其首字母也為大寫.同時,每個類的體包圍在一對花括號中({和}).類的定義以分號結(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

測試結(jié)果

GradeBook類怎么定義

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

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

// 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類怎么定義”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對GradeBook類怎么定義這一問題有了更深刻的體會,具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,,小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!

文章標(biāo)題:GradeBook類怎么定義-創(chuàng)新互聯(lián)
URL網(wǎng)址:http://bm7419.com/article8/dihoip.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、品牌網(wǎng)站建設(shè)、域名注冊、企業(yè)網(wǎng)站制作小程序開發(fā)、微信公眾號

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)