1 / 7

Classes & Objects

Classes & Objects. 2008/03. 執行結果. Welcome to the Grade Book. 1.1 定義一個類別函數. #include<iostream.h> class GradeBook { public: void printMessage() { cout <<"Welcome to the Grade Book!"<<endl; } }; void main() {

season
Download Presentation

Classes & Objects

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Classes & Objects 2008/03

  2. 執行結果 Welcome to the Grade Book 1.1 定義一個類別函數 #include<iostream.h> class GradeBook { public: void printMessage() { cout <<"Welcome to the Grade Book!"<<endl; } }; void main() { GradeBook myGB; myGB.printMessage(); } //定義一個GradeBook的class //GradeBook類別包含一個成員函數 printMessage //程式執行時,會自動呼叫main函數 //建立一個GradeBook類別的物件,叫做 myGB //使用點符號( . )來呼叫成員函數 printMessage

  3. 執行結果 Please enter the course name: Math101 Introduction to C++ Welcome to the grade book for Math101 Introduction to C++ ! 1.2 定義一個有參數的類別函數 #include<iostream> #include<string> using namespace std; class GradeBook { public: void printMessage(string courseName) { cout<<"Welcome to the grade book for\n"<< courseName <<"!"<<endl; } }; int main() { string course; GradeBook myGB; cout<<"Please enter the course name:"<< endl; getline(cin,course); cout<<endl; myGB.printMessage(course); return 0; } //定義一個GradeBook的class //GradeBook類別包含一個成員函數 printMessage //由於cin遇到space鍵會停止讀取,因此使用 getline函數來輸入字元 //course變數當成參數傳給 printMessage 函數

  4. 1.3 資料成員、set函數、get函數 #include<iostream> #include<string> using namespace std; class GradeBook { public: void setCourseName( string name) { courseName=name; } string getCourseName() { return courseName; } void printMessage() { cout<< "Welcome to the grade book for\n" << getCourseName() << "!" << endl; } private: string courseName; }; // setCourseName接收一個參數,利用此參數設定 courseName的值 // getCourseName回傳 courseName的值 // private 表示只能被類別的成員函數所存取 // 因此 courseName 只能使用在setCourseName、getCourseName與 //printMessage函數

  5. 執行結果 Please enter the course name: Math101 Introduction to C++ Welcome to the grade book for Math101 Introduction to C++ ! 1.3 資料成員、set函數、get函數 int main() { string course; GradeBook myGB; cout<<"Please enter the course name:" << endl; getline( cin,course ); myGB.setCourseName(course); cout << endl; myGB.printMessage(); return 0; } // 呼叫setCourseName 函數來設定值

  6. 1.4 初始化物件---建構子 #include<iostream> #include<string> using namespace std; class GradeBook { public: GradeBook(string name) { setCourseName(name); } void setCourseName( string name) { courseName=name; } string getCourseName() { return courseName; } void printMessage() { cout<< "Welcome to the grade book for\n" << getCourseName() << "!" << endl; } private: string courseName; }; // 建立GradeBook類別的建構子 //建構子名稱要與類別名稱相同

  7. 執行結果 gradeBook1 created for course: Math101 Introduction to C++ gradeBook2 created for course: Math102 Data Structures in C++ 1.4 初始化物件---建構子 int main() { GradeBook myGB1("Math101 Introduction to C++"); //建立物件時,傳入參數設定初始值 GradeBook myGB2("Math102 Data Structures in C++"); cout<<"gradeBook1 created for course:" << myGB1.getCourseName() <<"\ngradeBook2 created for course:" << myGB2.getCourseName() << endl; return 0; }

More Related