0 Basic QT 4 programming example three: Hook three strands of the four strings must five-file contains statements and standard library usage __QT

Source: Internet
Author: User

Statement:
The column in "0 basic QT4 Programming" is Qter_wd007 's original technical article.
This series of columns may be reproduced at will, but this statement and the original address of each article must be retained.
The author retains the copyright and shall not be used for any commercial purpose without the prior written permission of the author. Otherwise, please pay the relevant legal liability.

Example Description:

In this case, we will write a program to implement the hypotenuse of the right-angled triangle, in which the square-squared operation is required, which can be achieved by calling the functions brought by the C + + standard library. By learning This example, readers can grasp the use of file-containing statements in the C + + language and how to use the functions provided in the standard class library. This console program is done in Qt creator.

We will give two complete code, respectively, using the C library and the C + + library to achieve, we will also analyze the difference between the two, please readers carefully experience.

How a file contains statements works:

Almost every program starts with the statement that the file contains the directive:

#include "file name"

Or is:

#include < filename >

When the compiler encounters a file-containing instruction, the entire contents of the file referred to in the instruction are included in the application, replacing the file containing the instruction and being part of the source program.

Example Explanation:

1. file contains instructions

The file specified #include < filename > is a standard class library file provided by the system. If you have Microsoft Visual Studio installed, the generic C + + class library files are placed under Microsoft Visual Studio's home Directory "vc/include/" (Visual Studio 6.0 is vc98/ Include), the Qt 4 class library files are placed in the QT 4 's home Directory "C:/qt/4.5.0/include" (in the QT 4.5.0 version that I installed, for example, by the default installation SDK method. The reader's actual situation, please correspond to find the authentication path. The compiler searches the default directory for files that contain the files specified in the directive.

#include "filename" is typically used to include files written by users themselves. The compiler finds the containing file in the current directory first, if it is not found, and finds it in the two default directories listed above. It is recommended that this form be used to process file-containing instructions uniformly.

2. Write three strands of four strings must be five procedures

Here is an example of using a mathematical function to illustrate this problem. This program realizes the function of the hypotenuse of the right-angled triangle, in which the operation of square is needed. Whether it is a computer or a person, the accurate square operation is a very difficult task, C + + Math function library provides the function of the square operation:

You need to add the file include statement at the beginning of the file before you use it:

You can then use this function anywhere in the file. The declarations of printf and scanf functions are included in the standard input-Output class library header file stdio.h.

3. Operation Steps

Follow the steps in Example 1 to create a project GOU3GU4XIAN5 based on the console.

step 1th, create the project

Select the Menu "File->new ..." To eject the new dialog box, as shown in the picture.

Diagram to create a console type application

Select "PROJECTS->QT Console Application", create a console type application, and click the "Choose ..." button to go to the next setting.

Step 2nd, set the project name and path

As shown, first click on the "Browse ..." button to select the location of the project file, and then in the "Name:" text box to enter the name of the project, we are here gou3gu4xian5. When the settings are complete, click the "Next" button to go to the next setting.

Diagram sets the location and name of the application to save

3rd Step Finish Creating

As shown in the figure, Qt creator based on the information provided by us, gives a summary of the project, we confirm the correct, click the "Finish" button to complete the project creation.

Figure 5 Complete the project creation

At this point, Qt creator will be able to create the completed project in front of us, as shown in Figure 6, the project contains a Gou3gu4xian5.pro file and a main.cpp file.

Figure Create a good project browsing

Step 4th, writing program code

We complete the coding in the QT creator Code Editor. This program is mainly for the main () main function of the modification, in the projects sidebar double-click Main.cpp, in the right code Editor to enter the following code:

Step 5th, compile and run the program

Compile (ctrl+b) run (ctrl+r) program to get the results of the program running as shown in Figure 8:

Figure 8 Program Run results

Description: The C + + standard class library contains

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.