Program practice: separation of interfaces and Implementations

Source: Internet
Author: User

Program practice: separation of interfaces and Implementations

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 client 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 
 
   // 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 
 
  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 << getCourseName()       << ! << endl;} // end function displayMessage
 
Test the GradeBook class
// GradeBook class demonstration after separating its interface from its implementation.#include 
 
  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()      << gradeBook2 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.

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.