In the actual program, a source program can be divided into 3 files according to the structure: class declaration File (*. H), class implementation file (*. CPP), and classes using Files (*. CPP, main function file).
The declaration portion of a class is placed in a class declaration file (header file), which forms the public interface of the class and provides the user with the function prototypes needed to invoke the class member functions.
The definition of a class member function is placed in the class implementation file, which forms the implementation method of the class.
Place the use of the class (usually the main program) in the class usage file, which clearly indicates the work that the program is requesting to complete. Here's an example:
"NodeTriangle.h" class declaration file
#include <iostream>using namespacestd;classnode{Private: DoubleX_;//the horizontal ordinate of the node DoubleY_; Public: Node (DoubleXDoubley); //constructor Function voidDisp ();//Print node coordinates};classtriangle{Private: Node*nodei;//a data member is a pointer to a node typeNode *Nodej; Node*Nodem; Public: Triangle (Node& node1,node& node2,node&node3); voidDisp ();//Print triangle Information};
//"NodeTriangle.cpp", class implementation file#include"NodeTriangle.h"/*************************************/Node::node (DoubleXDoubley) {X_=x; Y_=y;}voidNode::d ISP () {cout<<x_<<"\ t"<<y_<<Endl;}/*************************************/Triangle::triangle (Node& node1,node& node2,node&node3) {Nodei=&node1;//Address Deliverynodej=&Node2; Nodem=&Node3;}voidTriangle::d ISP () {Nodei-disp (); Nodej-disp (); Nodem-disp ();}
// "Main.cpp" class application file #include"NodeTriangle.h"int main () { Node NOD1 (1.2,2), NOD2 (2,3.4), nod3 (4.5,5.3); Triangle Tri (nod1,nod2,nod3); Tri.disp (); return 0 ;}
Multi-File composition