C + + header file

Source: Internet
Author: User
Tags class definition

A very important concept in C + + is the header file .

The main reason for using header files in C + + is that there may be multiple source code files for the same project in C + +, and that the source code is compiled separately.

In other words, when compiling one of these files, the compiler does not know what is defined in other files, such as classes, global variables, and so on.

This requires that we must declare it in every file where we want to use a class, function, or variable, or C + + cannot find it.

For example, suppose you write a generic function add, which is defined as follows:

? View Code CPP
1234
int add (int a, int b) {   return a+b;}

Many files may need to use addition. If you have a file b.cpp need to use this function, then it must first declare it, although it does not need to be rewritten.

? View Code CPP
12
int add (int a, int b); add (5,5);

If you have a lot of files to use this function, then this will become cumbersome, especially if you write a class, then you need to maintain a large number of declarations (for each public object), and if your class definition has changed, you may have to change countless declarations.

Therefore, the C + + language presents the concept of a header file. You only need to declare it once in the header file, defined once in the implementation file, and in all required files, you only need to reference the file, which is equivalent to each file containing a declaration.

To prevent duplicate inclusion of a header file , the contents of the header file should normally be written using the preprocessing Directives #define (defining symbols), #ifndef (if not defined), #endif (ending judgment).

Please understand the following example, which is an improvement to the Xiao class in the last note.

Implementation of the Xiao class (Xiao.cpp)

? View Code CPP
1234567891011
#include "xiao.h" bool Xiao::mobaixiao () {return This->mobai ("Xiao", 10000);//correct}bool Xiao::mobai (char* cowname, int Mobai_times) {//Worship God Ox. return true;}

Xiao.h header File

? View Code CPP
12345678910
#ifndef xiao_h#define xiao_hclass xiao{public:bool mobaixiao ();p rivate:bool Mobai (char* cowname, int mobai_times);}; #endif

Main program :

? View Code CPP
123456789
#include "xiao.h" int main () {(Xiao ()). Mobaixiao (); Correct//(Xiao ()). Mobai ("Xiao", 10000); Error    return 0;}

C + + header file

Related Article

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.