A large collection of difficult questions for beginners in Visual C++ programming (2)

Beginners have many questions when learning visual c++. Interestingly, many of the questions are not mentioned in the textbook. Many friends in the background asked me related questions, so I summarized them. This is for friends who are learning visual c++ to avoid detours.

Question 6: How to understand the declaration and definition of functions in visual c++

In a complex program, if all the code is written in the main function, main function body will be very large. Divide tasks into other functions, main function is only responsible for the core process of the program, and specific tasks are completed by other functions. This idea is modular programming.

Syntax for declaring and defining functions:

Data type of return value function name (data type of parameter one parameter one, data type of parameter two parameter two,...)

{

  The code that implements the function.

    Return return value;

}

Declaration of function: Let the compiler know the existence of the function, including the data type of the return value, function name and parameter list.

Definition of function: the implementation process of the function.

Notice:

  1. The declaration and definition of the function can be written together or separately. If they are written together, they are usually placed above the main function. If separated, it is usually declared above the main function and mainThe following definition of the function.

  2. If the function declaration and definition are written separately, there must be a semicolon after the function declaration, and there must not be a semicolon after the function definition.

  3. In the same program, a function only needs to be declared and defined once, or it can be declared multiple times, but it can only be defined once.

  4. The declaration of the function must be consistent with the definition of the function (data type of return value, function name and parameter list). If the function name and parameter list are different, it means that they are not the same function.

  5. The data type of the return value of the return statement must be consistent with the declaration of the function.

  6. In the function body, the return statement can be used multiple times.

  7. If the focus of the function is to implement the function and does not care about the return value, fill in void, r for the data type of the return value There is nothing left after the eturn statement.

  8. Functions can have no parameters.

  9. The function name is an identifier and must meet the naming rules for identifiers.

Parameter names can be different in the function declaration and function definition, but there is no need to write them this way.

Question 7: How to call functions in visual c++?

Syntax:Function name (parameter one, parameter two,...)

Notice:

  1. The code that declares the function must be placed before the call, and the code that defines the function can be placed after the call.

  2. When calling a function, the parameter list must be consistent with the function declaration (number, order and data type of parameters).

  3. The main function cannot be called anywhere, but in ordinary functions, other ordinary functions can be called.

  4. The code that calls a function can be a statement alone, or it can be used in expressions (assignment operations, arithmetic operations, relational operations, function parameters).

  5. If the function is used in an expression, the data type of the return value needs to match (otherwise it may be implicitly converted or cause a compilation error).

Question 8: How to understand the scope of variables in visual c++?

ActionThe scope isrefers tothe existence of variables in the program Or the effective area , variables cannot be accessed beyond this area.

Variables are divided into global variables and local variables. Global variables can be accessed throughout the program, while local variables can only be accessed inside a function or statement block.

There are five main scenarios for defining variables in Visual C++:

1) Global variables are defined outside all functions.

2) Global variables are defined in the header file.

3) Local variables are defined inside functions and statement blocks.

4) The parameters of a function are local variables of the function.

5) What is modified with static inside the function is a static local variable.

1)Total station change amount

is valid throughout the entire program life cycle, any function after definition position Zhongdu can access.

Global variables reclaim memory space by the system when the main program exits.

2) Local change amount

infunction orlanguageuse of internal language ,Number of functionsor phrasesexternal is not possiblepurpose.

Local variables are reclaimed by the system when the function returns or the statement block ends.

3) Static local variables

International variables modified withstatic life cycle Same as the program, and will only be initialized once.

Its scope is local, and its scope ends when the function or statement block in which it is defined ends.

When a program wants to use global variables, it should first consider using static (Considering data security).

4) Things to note

Global variables and static local variables are automatically initialized to 0. Local variables will not be automatically initialized, and their values ​​are uncertain. There should be code to initialize local variables in the program, otherwise the compilation may report an error (different compilers vary). The names of local variables and global variables can be the same, in afunction or Within of statement block , if the local variable name is the same as the global variable name , then willshield global variables and use local variables, if you want to use For global variables, you can add:: before the variable name. The scope of the variables defined in the for loop initialization statement is the for statement piece.

Question 9: What should I do if a function in visual C++ needs to return more than one value?

When calling a function, the caller assigns values ​​to the function's parameters.

Actual parameters:The parameters written in the function name brackets in the caller program can be constants, variables and expressions.

Formal parameters:The parameter list of the function.

In the function definition code, modifying the value of the formal parameter will affect the actual parameters.

If a function in visual c++ needs to return more than one value, please use the method of passing the address to make changes in the program.

Question 10:How to handle multi-file projects in visual c++?

When writing a project program, different programs should be written in different files.

头文目(*.h

The header files that need to be included, the declaration of global variables, the declaration of functions, the declaration of data structures and classes, etc.

Source sentence item (*.cpp):

Function definition, class definition.

Main program: main function, the core process of the program, requires #Include the header file. "Header file nameinclude "

Compile:

Windows is an integrated development environment and does not require writing compilation instructions.

in Linux, based on the complete source text, as follows:g++ - o demo demo.cpp tools.cpp others.cpp

You can see the following example

Split a program into five files:

File 1: demo.cpp

Code:

#include "tools.h"

#include "person.h"

int main()

{

cout<<"max(5,10)="<<max(5,10)<<endl;

cout<<"mix(5,10)="<<mix(5,10)<<endl;

print(1,"Study hard.");

}

File 2:

person.cpp

Code:

#include "person.h"

viod print(int no,string str)

{

cout<<"Students whose academic performance ranks"<<no<<" please continue"<<str<<endl;

}

File 3: tools.cpp

Code:

#include "tools.h"

int max(int a,int b)

{

return a>b?a:b;

}

int min(int a,int b)

{

return a<b?a:b;

}

File 4: person.h

Code:

#pragma once

#include<iostream>

using namespace std;

void print(int no,string str);

File 5: tools.h

Code:

#pragma once

#include<iostream>

using namespace std;

int max(int a,int b);

int mix(int a,int b);

About the author: Liyuan Breeze, born in 1981, senior engineer, master of engineering from Zhejiang University, software engineering project manager, worked as a programmer, software designer, system architect, early Windows programmer, loyal user of Visual Studio, user of C/C++ The author is a veteran who has studied, struggled, and struggled in the computer industry for 25 years. He has experienced the UNIX era, the desktop WIN32 era, the Web application era, the cloud computing era, the mobile Android era, the big data era, the ICT era, and AI deep learning. era, the era of intelligent machines, I don’t know what era there will be in the future. I just remember that this journey has been full of hardships and gains. I am willing to go on with everyone and go on full of hope.

Guess you like

Origin blog.csdn.net/wang2015cn/article/details/134055569