Home>

Separation of shared interfaces and private implementations

C++ implements encapsulation through classes,Encapsulate data and operations related to this data in a class,In other words, the role of a class is to encapsulate data and algorithms in an abstract data type declared by the user.

In fact, users often don't care how the internals of the class are implemented.You just need to know which function will get what result,What functions can be achieved.

After declaring a class,Users mainly implement the functions provided by the class by calling public member functions (such as setting values ​​for data members,Display the values ​​of the data members,Processing data, etc.). Therefore, the public member function is the public interface of the user using the class, or the external interface of the class.

The data manipulated in the class is private,Implementation details are hidden from the user,This implementation is called a private implementation. This "separation of the public interface of the class from the private implementation" creates information hiding.

One of the most basic principles of software engineering is to separate the interface from the implementation.Information hiding is a very important concept in software engineering.Its benefits are:

If i want to modify or extend the functionality of the class,Just modify the related data members and member functions related to it in this class,Parts outside the class in the program need not be modified.

If you find that the data in the class is read or written at compile time,No need to check the entire program,Just check the few member functions in this class that access this data.

Separation of class declarations and member functions

In object-oriented program development,The general practice is to place the declaration of the class (which contains the declaration of member functions) in the specified header fileIf users want to use this class,Just include the relevant header files.

Since the class declaration is included in the header file,So you can use this class to define objects in your program.Since the declaration of member functions is included in the class body,You can call the public member functions of these objects in your program.

In order to achieve the information hiding described in the previous section,The definition of class member functions is generally not placed in the header file,And put it in another file.

For example, you can write two files separately:

student.h header file

//This is the student.h header file. Class declarations are made in this file
class student //class declaration
{
  public:
  void display ();//Public member function prototype declaration
  private:
  int num;
  char name [20];
  char sex;
};

student.cpp

//define functions in this file
#include<iostream>
#include "student.h" //Don't miss this line,Otherwise it won't compile
void student ::display () //Define display class functions outside the class
{
  cout<"num:"<num<<endl;
  cout<"name:"<name<endl;
  cout<""sex:"<sex<endl;
}

In order to form a complete source program,There should also be a source file (main.cpp) that includes the main function:

//main function module
#include<iostream>
#include "student.h" //Include the class declaration header file
int main ()
{
  student stud;//define the object
  stud.display ();//Execute the display function of the stud object
  return 0;
}

This is a program that includes 3 files,Forms two file modules:one is the main module main.cpp and one is student.cpp. Include the student.h header file in the main module. The contents of the header file student.h replace the #include "student.h" line when precompiled.

Note that since the header file student.h is placed in the user's current directory,So use double apostrophes around the file name ("student.h") instead of angle brackets (), otherwise this file will not be found during compilation.

Use the C++ compilation system to compile the two source files main.cpp and student.cpp respectively.Get two target programs main.obj and student.obj, and then connect them with other system resources.Form the executable file main.exe, as shown in the figure.

Note that the suffix of the object file is different in different c++ compilation systems,For example, in gcc, the suffix is ​​.o, and using .obj here is for vc 6.0.

When running the program, call the display function in stud to output the value of each data member.

Some readers may consider the question:If a class declaration is called multiple times by different programs,Compile a source file (such as student.cpp above) that contains member function definitions each time,Can this be improved?Indeed, you don't have to recompile it every time,You only need to compile it once.Save the object file formed after the first compilation,Later, when it is needed, it can be called up and directly connected with the object file of the program. This is similar to using functions from the library.

This is also a benefit of not including member function definitions in header files.If the definition of a member function is also placed in the header file of the class declaration,Then every compilation of every program using these classes must include the compilation of member function definitions,That is, the definition of the same member function will be repeatedly compiled multiple times.Only put the definition of the member function in another file,In order to achieve separate compilation.

In practical work,Instead of making a class declaration into a header file,Instead, it gathers several commonly used class declarations with similar functions.Form a class library.

There are two types of class libraries:one is the standard class library provided by the C++ compilation system;-The user library is made by users according to their own needs.For your own use and those authorized by you,This is called a custom class library.In program development work,Class libraries are useful,It can reduce the user's own workload of defining classes and member functions.

The class library includes two components:a class declaration header file;Definition of compiled member functions,It is the target file.

Users only need to load the class library into their own computer system (usually into the subdirectory where the C++ compilation system is located), and use the #include command line to include the relevant class declaration header files into the program.You can use these classes and their member functions,Run the program smoothly.This is the same as using the standard functions provided by the C++ system in the program.For example, when calling the sin function, the user only needs to include the header file that declares this function into the program.You can call the library function,It is not necessary to understand how the sin function is implemented (how the function value is calculated).

Of course, the premise is that the system has installed the standard function library.After the user source file is compiled,Link to the system library (which is the object file).

Include the class declaration header file in the user program,The class declaration header file becomes the public interface for the user to use the class.A function prototype declaration for member functions is also provided in the class body of the header file,The user can only use the related classes through the header file.What the user can see and touch is this header file,Any user who wants to use this class need only include this header file.The file containing the member function definitions is the implementation of the class.

Please pay special attention,Class declarations and function definitions are usually placed in two separate texts.Since interfaces are required to be separated from implementation,It has created good conditions for software developers to provide users with class libraries.

Developers put various types of declarations required by users in different header files by category,At the same time compile the source file containing the member function definition,Get the object code defined by the member function.Software vendors provide users with object code for the implementation of these header files and classes (do not provide source code for function definitions). When users use classes in the class library,Just include the relevant header files in your own program,And you can link the object code defined by the member function after compilation.

Due to the emergence of class libraries,Users can use general or specialized classes accumulated in practice as easily as using parts,This greatly reduces the workload of programming.Effectively improve work efficiency.

c
  • Previous Java development environment under Linux and the first HelloWorld
  • Next Asp coding optimization techniques