Multi-File composition

Source: Internet
Author: User

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

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.