程式實踐:介面與實現分離,實踐介面實現分離

來源:互聯網
上載者:User

程式實踐:介面與實現分離,實踐介面實現分離

我們將類定義和使用此類的客戶代碼分離,增強了軟體的可複用性.而介面定義並標準化了人和系統等諸如此類事物彼此互動的方式.每個類定義包含了類的公有成員函數的完整定義及其私人資料成員聲明.可是更好的軟體工程是在類定義的外部定義成員函數,這樣這些成員函數的實現細節對客戶代碼而言隱藏的,這種方式保證程式員不會寫出依賴於類的實現細節的客戶代碼.否則,若類的實現更改,則客戶代碼將更可能"遭到損壞".

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.



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.