We separate the class definition from the customer code that uses this class, Enhanced reusability of software. The interface defines and standardizes the way people and systems interact with each other. Each class definition contains the full definition of the class's public member function and its private data member declaration. But better software engineering is defining member functions outside the class definition, In this way, the implementation details of these member functions are hidden from the client code, which guarantees that the programmer will not write out the client code that relies on the implementation details of the class. Otherwise, if the implementation of the class changes, the customer code is more likely to be "corrupted".
Gradebook. h: Defining class interfaces using function prototypes
GradeBook.h gradebook class definition. This file presents Gradebook's public//interface without revealing the implementations of Gradebook ' s member//functions , which is defined in GradeBook.cpp. #include <string>//class Gradebook uses C + + standard string classusing std::s tring;//Gradebook class Definitionclass Gradebook{public: gradebook (string);//constructor that initializes cours ename void Setcoursename (string);//function that is sets the course name string getcoursename ();//function that Gets the course name void DisplayMessage ();//function that displays a welcome messageprivate: String Coursename ; Course name for this gradebook}; End Class Gradebook
Gradebook. CPP: Defining member functions in separate source code files
source code gradebook. CPP defines member functions for the Gradebook class, which are declared at 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 Initializ E 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::g Etcoursename () {return coursename;//Return object ' s coursename}//End Function getcoursename//display a welcome mess Age-to-gradebook uservoid gradebook::d isplaymessage () {//Call Getcoursename to get the coursename cout << " Welcome to the grade book for\n "<< Getcoursename () << "!" << Endl;} End Function DisplayMessageTestGradebookClass
Gradebook class demonstration after separating it interface from its implementation. #include <iostream>using std :: cout; Using Std::endl; #include "GradeBook.h"//include definition of class gradebook//function main begins program Executionin T Main () { //Create 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: ' << gra Debook1.getcoursename () << "\ngradebook2 created for Course:" << gradebook2.getcoursename () < < Endl; return 0; Indicate successful termination}//End Main
Test output results
compilation and connection process
shown is the compilation and connection process of a viable gradebook application that generates a ready for use by a teacher. Typically, a programmer creates and compiles an interface and implementation of a class, and is used by programmers who implement class client code differently. So This shows what the class implements for programmers and client code programmers to do. The dashed line in the diagram divides the class implementation programmer, the client code programmer, and the gradebook application user to do each part.
The class implementation that creates the reusable Gradebook class The programmer first creates two files, one is the header file GradeBook.h and the other is the source file GradeBook.cpp that contains the (#include) header file. Then, compile the source code file, Creates the target code for the Gradebook object.
For more discussion and exchange on program language, please follow this blog and Sina Weibo songzi_tea.
Program Practice: interface and implementation separation