1 Start the Visual Studio 2010 development environment, select "File" | "New" | "Project" from the "Start" menu, and the New Project dialog box will pop up, as shown in Figure 1-1 .
Figure 1-1
2 Expand the " Visual C++ " tree node and click Win32 , select Win32 console application, enter the project name Lab5_1 in the name , specify the project storage path in the location, and click the OK button. In the pop-up application wizard dialog box, click "Next" or "Application Settings", select the "Console Application" radio button and the "Empty Project" check box, as shown in Figure 1-2, and then click Click "Finish".
Figure 1-2
Figure 1-3
Figure 1-4
3 In the solution explorer shown in 1-3 , right-click on the project and select "Add" | "New Item", and in the pop-up Add New Item dialog box, select "Code", " C++ File", As shown in Figure 1-4 , fill in the file name and click the "OK" button.
4Enter the code in the file editing window, the example is as follows.
#include<iostream>
using namespace std;
void fn1();
int x=1, y=2; // global variable
int main()
{
cout<<"Begin..."<<endl;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"evaluate x and y in main..."<<endl;
int x=10, y=20;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
cout<<"step into fn1()..."<<endl;
fn1();
cout<<"back in main..."<<endl;
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
return 0;
}
void fn1()
{
int y=200;
cout<< "x=" <<x<<endl;// Note that x=1 at this time , the global variable is visible
cout<<"y="<<y<<endl;//显示y=200
}
5Click F7 or select the menu "Generate" | "Generate Solution" to compile the program (create an executable program) . If the generation is successful, click CTRL+F5 or select the menu "Debug" | "Start Execution (without debugging) ( H )" to run the program.
A friend function is a function declared using the friend keyword, which can access protected members and private members of the corresponding class;
A friend class is a class declared using the friend keyword, and all its member functions are friend functions of the corresponding class.
Friend relationships are not commutative; friend relationships are not transitive; friend relationships cannot be inherited.
Variables are of the following types:
Auto storage type: uses stack method to allocate memory space, which is temporary storage. Its storage space can be overwritten and used multiple times by several variables;
Register storage type: stored in general register;
extern storage type: can be referenced in all functions and program sections;
Static storage type: It is stored at a fixed address in the memory and is valid during the entire program running.
The input file for compilation is the source file, and the output file is the target file; the input file for connection is the target file, and the output is the executable file.