Program practice: separation of interfaces and implementations; isolation of practical Interfaces
We separate class definitions from customer code using this class, enhancing software reusability. interfaces define and standardize the ways in which people and systems interact with each other. each class definition contains the complete definition of the class's Public member function and its private data member declaration. however, a better software engineering is to define member functions externally in the class definition, so that the implementation details of these member functions are hidden from the client code, this method ensures that programmers do not write Customer Code that depends on class implementation details. otherwise, if the implementation of the class is changed, the customer code is more likely to be "damaged ".
GradeBook. h: Use the function prototype to define class interfaces
// 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: Define member functions in independent source code files
The source code GradeBook. cpp defines the member functions of the GradeBook class. These function declarations are located in 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
Test the GradeBook class
// 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
Test output result
Compilation and connection process
It shows the compilation and connection processes of the GradeBook application that can be used by instructors. generally, a programmer creates and compiles class interfaces and implementations, while programmers of different implementation class client codes use them. therefore, this shows what class implementation programmers and client code programmers need to do. in the figure, the dotted line divides the class implementation programmers, and the customer code programmers and GradeBook application users need to do their respective parts.
Creates a class that can reuse the GradeBook class. The programmer first creates two files, one of which is the header file GradeBook. h. The other is the source code file GradeBook that contains the (# include) header file. cpp. then, compile the source code file and create the target code of the GradeBook object.
For more discussions and exchanges on Program Language, please stay tuned to this blog and Sina Weibo songzi_tea.