标签:程序实践 接口与实现分离
我们将类定义和使用此类的客户代码分离,增强了软件的可复用性.而接口定义并标准化了人和系统等诸如此类事物彼此交互的方式.每个类定义包含了类的公有成员函数的完整定义及其私有数据成员声明.可是更好的软件工程是在类定义的外部定义成员函数,这样这些成员函数的实现细节对客户代码而言隐藏的,这种方式保证程序员不会写出依赖于类的实现细节的客户代码.否则,若类的实现更改,则客户代码将更可能"遭到损坏".
// 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类的成员函数,这些函数声明位于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 argument GradeBook::GradeBook( string name ) { setCourseName( name ); // call set function to initialize courseName } // end GradeBook constructor // function to set the course name void GradeBook::setCourseName( string name ) { courseName = name; // store the course name in the object } // end function setCourseName // function to get the course name string GradeBook::getCourseName() { return courseName; // return object‘s courseName } // end function getCourseName // display a welcome message to the GradeBook user void GradeBook::displayMessage() { // call getCourseName to get the courseName cout << "Welcome to the grade book for\n" << getCourseName() << "!" << endl; } // end function displayMessage
// 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 execution int 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.
标签:程序实践 接口与实现分离
原文地址:http://blog.csdn.net/songzitea/article/details/42528241