1th--Write a program in C + +

Source: Internet
Author: User
Tags function prototype

First, learn new knowledge

Before learning C + + language, some of the basic is not BB, into the topic.

A few small programs to practice practiced hand:

"Program 1"

#include <iostream>  //Header file using namespace std;//namespace int main () {cout<< "Hello world!" <<endl;return 0;//Return Statement}

"C + + preprocessor"

C + + programs to use C + + input or output tools, you need to provide "#include <iostream>" and "using namespace std;" These two lines of code.

C + + and C also use a preprocessor that processes the source files before the main compilation.

For example, there is a preprocessor that handles compilation directives whose names begin with #. We don't have to do anything to invoke the processor, it will run automatically when the program is compiled.

The compile instruction in program 1, "#include <iostream>", causes the processor to add the contents of the iostream file to the program. This is also a typical preprocessing operation: Replace or add text before the source code is compiled.

We would like to: Why add the contents of the iostream file to the program?

The answer involves communication between the program and the outside world. Io in iostream refers to input (information entering the program) and output (information sent out from the program). The input/output scheme for C + + involves multiple definitions in the iostream file. In order to use cout to display messages, the first program needs these definitions. #include编译指令导致iostream文件的内容随源代码文件的内容一起被发送给编译器. In fact, the contents of the iostream file will supersede the code lines in the program, # include <iostream>. Instead of modifying the original file, the source code file and the iostream are combined into a compound file that will be used by the next stage of the compilation.

"Header file name"

Header file--iostream--is included at the beginning of the file

"Namespaces"

namespace compiler directive--using namespace Std;

Also called using compiler directives

Do not need to know now (see book P17)

The above program only output, uninteresting. I want to enter! I want to enter! I want to enter!

"Program 2"

#include <iostream>  //Header file using namespace std;//namespace int main () {int apple;cin>>apple;//input cout<< Apple<<endl;return 0;//Return Statement}

"Using CIN"

Statement "cin>>apple;" Represents the flow of information from CIN to carrots. C + + sees the output as a stream of characters flowing out of the program, and it also considers the input to be a stream of characters flowing into the program. The iostream file defines CIN as an object that represents this stream, and the output,<< operator inserts the string into the output stream, and CIN uses the >> operator to extract the characters from the input stream when it is entered. (Symbols << and >> are selected to indicate the direction of the information flow)

I also want to learn the custom Function!!

"Program 3"

#include <iostream>using namespace Std;int myFunction (int);//function prototype declaration int main () {int apple;cin>>apple; int Ouo = MyFunction (apple); Call function MyFunction Cout<<apple<<endl;return 0;} int myFunction (int a)//form parameter a {int ans = a * 2;return ans;}

A function prototype describes a function interface, that is, how the function interacts with other parts of the program.

Second, the new knowledge of temperature

What is the preprocessor compiler directive include <iostream>? 】

Before the final compilation, replace the compilation instruction with the contents of the iostream file.

"Statement" using namespace std; " What do you do with it? 】

It enables programs to use the definitions in the Std namespace.

1th--Write a program in C + +

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.