Basic questions about the C ++ Language

Source: Internet
Author: User
Tags mscorlib

C ++ is a non-formatted language. The Compiler ignores all spaces, carriage return marks, line breaks, and so on. Next, let's talk about the names of the identifiers. First, we need to have some basic concepts. Second, we 'd better not start with an underscore. Third, we should not name it like a keyword, such as int and return. Then the program steps, edit, compile, Link, run and test.

 

C ++ is a non-formatted language. The Compiler ignores all spaces, carriage return marks, line breaks, and so on.

C ++ is case sensitive!

Next, let's talk about the names of the identifiers. First, we need to have some basic concepts. Second, we 'd better not start with an underscore. Third, we should not name it like a keyword, such as int and return.

Then the program steps, edit, compile, Link, run and test.

Note: There are often many system libraries and class libraries. OBJ file, processing all the library target files of a project is a heavy workload, so usually. merge OBJ files. lib library file for convenience.

Then, create a helloworld program through a Win32 project,

# Include <iostream>

Using namespace STD;

Int main ()

{

Cout <"" helloworld! ";

Cin. Ignore (); // pause the Execution Code to avoid the problem that the result will disappear after the execution.

Return 0;

}

Since the C ++ language has no format, programmers can use tabs or spaces to indent the program layout. The statements in the code block, such as the for loop or if statement, are usually indented (usually four characters in length ). In this way, the programmer can quickly identify the content in the program block.

Regarding Warnings: warnings should be regarded as errors. In other words, they should be excluded. The existence of warnings is rational, and their existence indicates that there are indeed errors in the code.

A variable is the location where data is temporarily stored in the memory.

Type System:

Variable naming Note: Avoid using such a identifier: It starts with two underscores, or followed by an upper-case letter (for example, _ ). Because Microsoft uses the above naming method to specify the keywords dedicated to macro and Microsoft, using these combinations at the beginning of the variable may lead to name conflicts.

Usage of pointers: first, pointers are one of the main ways to pass parameters to functions. Parameters are usually passed as values. This value is only a copy, so you cannot modify the value or want it to be returned to the code that calls it. Pointers allow passing parameters and modifying parameter values. Second, when operating a series of data (such as values in an array), you can use pointers to perform very efficiently.

. NET Framework string class: the string class is not a built-in data type and is part of the. NET Framework. Therefore, before using the string class, you must include some library files in the project. Add the following two lines of code at the top of the project:

# Using <mscorlib. dll>

Using namespace system;

# The using line tells the compiler to find details about the string class from the mscorlib. dll file. The mscorlib. dll library file contains the core components of. net. The job of the using row helps you use some. Net classes. Create a CLR console application. The code in helloworld. cpp is

# Include "stdafx. H"

// # Using <mscorlib. dll>

Using namespace system;

Int main (array <system: String ^> ^ ARGs)
{
String ^ A = "helloworld! ";
Console: writeline ();
Return 0;
}

 

Note: although the function of the string class is very powerful, once a String object is initialized, it cannot be changed. On the surface, the member functions of the string class (such as insert and replace) are strings to be modified. In fact, a new String object containing the modification is returned. In this way, when a string is modified repeatedly, the efficiency of the string method becomes very low. In this case, the stringbuilder class should be used, and the mscorlib. dll assembly and the system. Text namespace must be included to simplify access to members.

Note: prefix increment and decrement operators are more effective than suffix operators when operating on objects. This is because we do not need to use a temporary copy of the object in the expression before performing an increment or decrement operation on the object.

Type conversion: in addition to the standard conversion (type) value of C ++, the following four types of conversion are supported:

1. static_cast <type>

2. const_cast <type>

3. dynamic_cast <type>

4. reinterpret_cast <type>

Int A = 10;

Double B;

B = (double) A; // old version

B = static_cast <double> (a); // C ++

The main reasons for using a function are as follows:

1. Functions are short and discontinuous. After writing a program as a series of functions, the programmer can concentrate on one of the functions, thus reducing the difficulty of program development.

2. After dividing a program into a large number of small functions, it improves the readability of the program and makes debugging easier.

3. Functions can be reused. Once a function is compiled, it can be called at any time. This simplifies code writing and improves development efficiency.

A function prototype is a single-line statement that introduces the function name to the compiler.

Note: In the function prototype, the parameter name is optional. Theoretically, only the parameter type can be specified, but the parameter name is omitted. However, the parameter name can convey the meaning of the parameter, so it is best to use the parameter name in the actual programming process.

Modify the function prototype and define the default parameter value: int add (int A = 2, int B = 3 );

If int main () {return 0;} is used in a function, the return keyword returns the control flow to the called function. Sometimes there is no need for this return, because the braces at the end of the function body can implement implicit return.

Note: in Visual C ++, function bodies can be defined in any order. However, function Nesting is not allowed, that is, the function body cannot be defined in one function body.

Tip: Because the parameter name in the function prototype is only used to indicate that the parameter is required, the function body can use a different parameter name than the function prototype. However, to ensure consistency, we recommend that you use the same parameter name in the function prototype and function body.

 

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.