. H and. cpp files,

Source: Internet
Author: User

. H and. cpp files,

First, all the code can be placed in a cpp file. This is no different for computers,

However, for a project, bloated code is a disaster and is not suitable for reading and post-maintenance,

Therefore, the. h and. cpp files regulate programmer writing habits.

Usage

1. The. h file is directly # include to the required. cpp file, which is equivalent to copying the. h file code to the. cpp file.

2. The. cpp file must first generate the. o file and connect different. o files to generate executable files.

For example, there are three cpp files: a. cpp, B. cpp, and c. cpp. One of them contains the main () function, and the test program needs to be generated,

Steps:

1. generate three. o files: cc-c a. cpp

Cc-c B. cpp

Cc-c. cpp

In this way, three. o files are obtained: a. o, B. o, and c. o.

2. Generate the test program by link: cc-o test a. o B. o c. o

You can get the test executable program, and enter./test to execute the program.

Specifications

1. H files generally contain Class declarations;

2. the cpp file is generally a class function declared by the H file definition with the same name

Note: Generally, you can directly add main () to the cpp file to test the function of this module.

Example (g ++ ):

1 //point.h2 #include<stdio.h>3 typedef struct Point Point;4 struct Point{5     int x,y;6     Point(int _x,int _y);7     void ADD(Point _p);8     void Print();9 };
 1 //point.c 2 #include"point.h" 3 #define DEBUG 1 4 Point::Point(int _x,int _y){ 5     x=_x; 6     y=_y; 7 } 8 void Point::ADD(Point _p){ 9     x+=_p.x;10     y+=_p.y;11   12 ▽oid Point::Print(){13     printf("(%d,%d)\n",x,y);14 }15 16 #if DEBUG17 int main(){18     Point a(1,1);19     Point b(2,2);20     a.Print();21     a.ADD(b);22     a.Print();23     return 0;24 }25 #endif

Run:

G ++-c point. c
G ++-o test point. o

Obtain the executable program test

Run test to obtain the result:

[Zjp @ virtual-CentOS-for-test workstation] $./test
(1, 1)
(3, 3)

 

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.