程式實踐:介面與實現分離,實踐介面實現分離
我們將類定義和使用此類的客戶代碼分離,增強了軟體的可複用性.而介面定義並標準化了人和系統等諸如此類事物彼此互動的方式.每個類定義包含了類的公有成員函數的完整定義及其私人資料成員聲明.可是更好的軟體工程是在類定義的外部定義成員函數,這樣這些成員函數的實現細節對客戶代碼而言隱藏的,這種方式保證程式員不會寫出依賴於類的實現細節的客戶代碼.否則,若類的實現更改,則客戶代碼將更可能"遭到損壞".
GradeBook.h:使用函數原型定義類介面
// GradeBook.h GradeBook class definition. This file presents GradeBook's public // interface without revealing the implementations of GradeBook's member // functions, which are defined in GradeBook.cpp. #include <string> // class GradeBook uses C++ standard string class using std::string; // GradeBook class definition class GradeBook { public: GradeBook( string ); // constructor that initializes courseName void setCourseName( string ); // function that sets the course name string getCourseName(); // function that gets the course name void displayMessage(); // function that displays a welcome message private: string courseName; // course name for this GradeBook }; // end class GradeBook
GradeBook.cpp:在獨立的原始碼檔案中定義成員函數
原始碼GradeBook.cpp定義了GradeBook類的成員函數,這些函式宣告位於GradeBook.h.
// GradeBook.cpp GradeBook member-function definitions. This file contains// implementations of the member functions prototyped in GradeBook.h.#include <iostream>using std::cout; using std::endl;#include "GradeBook.h" // include definition of class GradeBook// constructor initializes courseName with string supplied as argumentGradeBook::GradeBook( string name ){ setCourseName( name ); // call set function to initialize courseName} // end GradeBook constructor// function to set the course namevoid GradeBook::setCourseName( string name ){ courseName = name; // store the course name in the object} // end function setCourseName// function to get the course namestring GradeBook::getCourseName(){ return courseName; // return object's courseName} // end function getCourseName// display a welcome message to the GradeBook uservoid GradeBook::displayMessage(){ // call getCourseName to get the courseName cout << "Welcome to the grade book for\n" << getCourseName() << "!" << endl;} // end function displayMessage測試GradeBook類
// GradeBook class demonstration after separating its interface from its implementation.#include <iostream>using std::cout; using std::endl;#include "GradeBook.h" // include definition of class GradeBook// function main begins program executionint main(){ // create two GradeBook objects GradeBook gradeBook1( "CS101 Introduction to C++ Programming" ); GradeBook gradeBook2( "CS102 Data Structures in C++" ); // display initial value of courseName for each GradeBook cout << "gradeBook1 created for course: " << gradeBook1.getCourseName() << "\ngradeBook2 created for course: " << gradeBook2.getCourseName() << endl; return 0; // indicate successful termination} // end main測試輸出結果
編譯和串連過程
所示是顯示了產生可供教師使用的,可行的GradeBook應用程式的編譯和串連過程.通常由一個程式員建立和編譯類的介面和實現,而由不同的實作類別客戶代碼的程式員使用它們.因此這個顯示了類實現程式員和客戶代碼程式員需要做的部分.圖中虛線劃分了類實現程式員,客戶代碼程式員和GradeBook應用程式使用者各自需要做的部分.
負責建立可複用GradeBook類的類實現程式員首先建立兩個檔案,一個是標頭檔GradeBook.h,另一個是包含(#include)標頭檔的原始碼檔案GradeBook.cpp.然後,編譯原始碼檔案,建立GradeBook對象的目標代碼.
關於Program Language更多討論與交流,敬請關注本部落格和新浪微博songzi_tea.