Introduction to Visual C ++ 6.0

xiaoxiao2021-04-09  322

Introduction to Visaul C 6.0

Document Name: Visaul C 6.0 Brief Introduction to document author: Shen Jie Creation Date: 2004.03.20 Current Version: 1.0.0.1 modify the record: N / A

Summary:

1. Basic Concept | | __1.1 Project Concept |? | __1.2 Concept of Workspace

2. VC interface introduction? |? | __2.1 Frequent menu item? | ?? |? | ?? | __2.1.1 file menu? | ?? |? | ?? | __2.1.2 Compile menu? |? | __2 .2 class view introduction? |? | __2.3 file view introduction? |? | __2.4 resource profile? |? | __2.5 code color

3.VC common data type

4. An instance - start your first VC program step by step

5. Conclusion

Document content:

1. Basic concept:

1.1 The concept of the project: ??? The collection of individual source code files and other auxiliary files used to generate a final program is called a "project". This concept may be more unfamiliar for some beginners, which is completely understandable. In Turbo C we used before, although it provides support for project file management (Project menu item), its function is not powerful, and it is cumbersome to use the steps, so it is not valued. And the procedures we face at that time are generally relatively simple, put all the code in a source file, so there is no urgent need to use the project. But in actual programming work, a program is often very complicated, and if you want to use a file to achieve almost impossible. So the programmers often divide a program into some of the functional modules, and then implement the functions of each module in different source files. At compile time, first compile each source file, generate a series of .Obj files, then connect them to each other (often connected to the necessary .lib static library file) to get the final program file. In order to maintain the interregional relationship between a project in a project, an item description file for describing these relationships is generally added in the project, and the contents and formats of these files are different from the compiler. For VC, it is a project file. The .dsp file in the clip. The content of this file is automatically maintained by VC without the need to be modified by programmers. • Let's list the instance of a project to explain the organizational structure of the project in the VC. Some of this example may be designed in the future knowledge, so we don't expect readers to fully understand it here, just want to build a "project" intuitive concept.

Example 1.1: Project Name: Apitest? Project folder name: ApiteSt project folder structure:

Apitest ??? Project folder || __stdafx.h ?? One of the program source files can be modified by the user || __stdafx.cpp ?? Program source file, can be modified by the user || __apitest.h ?? Program source file First, can be modified by the user || __apitest.cpp ??, can be modified by the user || __Resource.h ??, content about the definition of resource symbols, by VC maintenance, we do not recommend users to modify This file || __apitest.rc ?? Resource Description File, can be modified by the user from the VC's resource editor, for some advanced users, you can also use text mode to modify || __apitest.dsw ?? Workspace description file , By the VC Maintenance || __APITEST.DSP ?? Item Description File, compiled by VC Maintenance || __apitest.plg ?? Maintenance, users can view || __apitest.aps ?? Project related files, by VC Maintenance | | __Apitest.ncb ?? Project Related Files, by VC Maintenance || __Readme.txt ?? The project is generated by the VC, describing the function of some of the main files in the project, does not recommend user modification || __debug ?? The default output path when compiling the item is "debug" |? ||? | __APITEST.EXE? Compile and connected final program (Debug version) |? ||? | __ *. * ?? generated during compilation Document, including .obj, .ilk et al. || __release ?? When the compile selection is "release", the default output path when "release" |? ||? | __APITEST.EXE? Compile and connected final program (Release version) |? ||? | __ *. *? Media, including .obj, .ilk et al. || __res ??? project resource folder, use the .rc file, used to store the item needs Various resources (.bmp, .ico, etc.) are responsible for maintenance by VC. It is not recommended to modify it outside the VC .? | ?? | __ *. * ?? The resource file of the project is just a small one. Procedure, as seen, VC project composition is very complicated. However, it is necessary to declare that VC has a good automatic maintenance mechanism on the project, so many files do not require us to personally maintain. Even in a project, we generally rarely go to new or open files, but we will make specific requirements to make VC to determine whether to create a new file and establish a file. For example, when we want to create a new class, the VC automatically creates a set of source files (.h and .cpp) for this class code. At the same time, when we want to see a certain piece of code, you don't need to open the file in person and then look up. You only need to select the function you want to read in the class view window. The VC will automatically find the file it is in and open. it. Overall, VC is here to assume a large-managed role, which helps us manage complex project file structures, and present the contents of the logical level in front of us. That is, when we are writing code, we don't need to care about which file is written. It only needs to care which class is under the class, which function is OK. In other words, VC let us see the concept of files, rendering is just a class, functional concept in front of us, which is the power of the VC project management mechanism.

1.2 The concept of workspace concept Workspace can be considered to be an extension of the VC project management mechanism to the project concept. In our work, many times, not only only concerned about the contents of each file. For example, we have just added a function in the A file, then get off work, and finally I can take a break. But after all, this function has not been written yet, so we hope to automatically open the A file when you start working again and set the input location to the last interrupt location instead of the file header to facilitate our continued work. VC is satisfied by a "work area" to meet this demand, the concept of "Workspace" is just as its name, is used to describe the current working status of the user. For example, which files have been opened, where information is now saved in the workspace file, it will be automatically saved when VC is closed. When the user continues to work next time, just open this workspace file, you can go back to the status before the last closure, and no need to recall, which is changed, which is changed. This is specifically given here, one: The work area and the project are not necessarily one or one, and one work area can also contain two or more items. But in general, people still like to work only in one project, so there is only one project in a work area, or that each project often has a work area corresponding to it; second: Just When we want to open a project, often use the "Open Workspace Open Workspace" menu item to open the workspace file in this project folder instead of using "Open Project Open Project". 2. Introduction to the VC interface

2.1 Frequent menu items: ??? As you see, there is a very menu item on the interface of the VC, but many features are generally not used, so this is only a simple introduction to commonly used items.

2.1.1 File (File) menu group: ??? new? (New): New project or new file is completed by it, click a dialog box to ask new projects or new files and new items or files type. The specific operation will be described in detail later: Open: Used to open the file. Note that this menu item is not prepared for opening the project, but is only used to open a separate file, so its default open type is .h and .cpp files. This feature is generally not commonly used, but sometimes you want to open some of the functionality when you do not belong to any project. ??? Close: Turn off the file currently opened and active. ??? Open Workspace: Open the options commonly used when opening the project, use it to open the .dsw file in the project folder. It can also be used to open the project description file (.dsp), but if the presence of a .dsw file is detected, the project will be opened to open this .dsw file. ??? Save Workspace: Save the current workspace status. This feature is not commonly used because this operation is automatically executed when VC is turned off. ??? CLOSE WORKSPACE: When we need to start or open a new project, it is best to turn off the current workspace before we need to start or open a new project. This menu item should be used at this time. Just close all open files is not, the workspace at this time is still open, but only the code source file that is not opened. ??? Save: Save the source file currently being edited. This function is not very large, because it can only save a file at a time, and can only save files that open (.h and .cpp, etc.) in the workspace, if you do files for the resource description file The modification (of course, it is powerful through the resource editor, not directly modified by text). ??? Save as (Save as): The file is deposited. It exists the same shortcomings as the Save command. ??? Save all: Save all files currently open, including files that are not text (such as .rc, .aps, etc.), so that you have saved all modifications made to the project. This command is often used. ??? Other items and common menu items do not have much difference, will not be detailed. There are two points to note: 1. There are two options in the VC and the Recent Workspace, a corresponding to the open file, a corresponding to the workspace. The latter is much more common than the former; Second, if there is no saved modification in the currently activated file, a "*" prompt will appear in the title bar.

? 2.1.2 Compile (Compile) menu group: ??? Compile XXX (Compile current file): Compile the current source file to generate its corresponding .Obj file. ??? Build XXX (EX file for building a project): First compile all the "Ibj files and other files generated the final EXE file" to the source file that is not compiled or modified. It is often used to implement construction. ??? Rebuild All: The difference between it is also used to build EXE, it and the difference is: Build only the excessive .Obj file, and it does not consider whether these .Obj is the latest version, and recompile all Source file, then connect the production tool EXE file. This is generally rarely used. ??? EXECUTE XXX (executes an exe file): First build an EXE file, then run, equivalent to the functionality of Ctrl F9 in Turbo C. Recommended Use. ??? Set Active Configuration: VC program generally has two compilation configurations: debug version (Debug) and full version (Release), the newly created project defaults to debug configuration. In this configuration, the exe file is used in the project folder's debug directory, it is generally large, but includes a lot of debugging information, which is convenient to complete the debugging of the project with the VC debugger. The Release version of the program is much smaller, but it is not possible to debug, so it is generally the final finished product of the project, and the Debug version is generally used in the production engineering. As for other menu items, it is generally related to debugging. It is necessary to talk about it. Fortunately, for smaller projects, not how to use it, so I will be slightly.

? 2.2 Class View Introduction:??? When there is an active item in the VC, a set of tabs appears on the left side of the interface, which is Class View from left to right, respect, Resource View (Resource View) and File View. The Class View is the content to be introduced in this section, and it is also the most useful one in three views. The Class View of the VC displays the class hierarchy in the current project in the form of a tree map. Generally, when you just open a project, all classes are displayed in a folded manner, that is, only the class name is displayed, and there is a " " icon in front. In addition, there is a "Global" item under all classes, including functions and global variables that are not in any class. Double-click a class name that will automatically transfer to this class definition in the code editing window. To further view the contents of this class, you can click the " " flag on the left side of the name. At this time, the hierarchical structure is deployed, and all of its member functions and member variables are displayed. At the same time, the previous icons are changed to "-", and then return to the folded state. The function is in front of the pink small square icon, and the front of the variable is a green small square sign. If there is a lock icon in front of the small square, it means that the member is private. If a key is a flag, it is protected. If there is no other icon, it is public (public) of. Double-click the name of a function, then the input location of the edit window jumps to the definition of the function (implementation), if you want to jump to its declaration position, you can right-click the function name in the class view, in the menu that appears. Select one of the "Go to Declaration"; selecting the Property, you can directly view the properties of the function, including the return value type, parameter type, etc. If you double-click a variable name, you will jump to the defined location of the variable, and you can also use the protymnote menu item to view the type of variable. If you want to build a category, you can select the "New class" item in the right-click menu in the Class View (Show XXX Classes, XXX for the current item name), and fill in the corresponding dialog box in the pop-up The content, click OK. Then the VC will help you generate relevant files and classes of the initial definition information and switch the current input location to the newly created class implementation file. To create a new function or variable in the class, you can select the "Add Member" or "Add Member" item in the right-click menu of the class name. After filling the corresponding content, the VC will automatically generate the code and jump input. To delete a function, especially message processing function or mapping function, it is best to use the "delete" command in the function name right-click menu, which will not only clear the implementation and declaration of the function, but also clear the message map macro Related items (for this part of the concept, we will introduce in detail later). Of course, in addition to this, it is also possible to use the traditional pure manual method, just the former workload is small and it is not easy to go wrong. ??? The content displayed in the Class View is automatically maintained by VC, which will automatically complete the update according to the user's modification, and automatically saved when exiting the VC.

2.3 File View Introduction: ??? File View is the third tab on the left side of the VC interface. It is also shown in the form of a tree chart. The file in the file view is also displayed in a folder tree similar to the WIN Explorer, but the file hierarchical relationship shown here is not the actual file tree, but is a logical relationship between each file in the project. Each project has four logical folders: Source File, Header File (header), Resource File, and External Dependencies. In addition, there are some files that do not belong to any logical folder, such as readme.txt, etc., these files generally do not require developer maintenance. The Source file includes all files in the program in the program to be auspicious. Header file is the header file (.h) in the project. In Resource File is the resource file in the project, to edit the resource file, do not need to be opened here, and you can open the corresponding ID number in the Resource View tab to modify them. The files in External Dependencies are the external dependencies of the project. For example, there is a "include xxx.h" in your project, and XXX.H is not the header file in the project, then this file is considered to be external dependent. When I just opened the project, VC generally doesn't know where the external dependence on the project must build a project, and the content in this folder will be updated. Sometimes it will find that there is a "*" symbol after some file names, indicating that there is no saved modification in this file. ??? Double-click the file name in the File View, open this file, if it has been opened, switch the input focus to the file. In the right-click menu of the file name, select the delete command to delete the project, but must pay attention to it, this file is not deleted, nor is it moved, just being considered no longer belonging to the project. If you want to delete the file, you will need to delete this file outside of the VC. This should be paid to this at the time you need to override a file. If you delete only in the File View, you may cause some strange errors.

? 2.4 Resource Introduction: ??? Resources is a new concept for programming under Windows. As we all know, the WIN program is known in its exquisite GUI (Graphice User Interface graphical user interface). But if all graphics are implemented in the program, the workload is simply unimaginable. In addition, in many cases, we may want the program to play some sound prompts. If you are stored in the form of a file, there is no taste, but of course, it is better to write them directly to the program. Therefore, the procedures under WIN have introduced the concept of resources, and resources are generally some of the pictures, icons, or other procedures that require direct use of non-coded components, which are stored in the "resource section" of the generated EXE file. . Each resource in the project is usually stored in the RES folder in the corresponding file, and the relationship between them is stored in the .rc file. They are compiled into a resource target file, and then connect to the final EXE file with .Obj, .lib file, etc.. In the project, each resource is identified with a 32-bit unsigned integer, in order to facilitate memory, they are defined with the #define macro, such as IDi_maInframe, etc., which is called resource ID (Resouce Identifier ). These macros are stored in the resource.h header file, so you can see the commands of the #include "resource.h" in a lot of .CPP files, which is to make it identifying these constant macros. Resource.h is maintained by VC, when the user adds, deletes a resource or change the resource ID, it will be automatically updated. As for the way of using resources, WinAPI and MFC provide a lot of corresponding functions, which will not be detailed here, they are not the focus of this article. One in the three tabs on the left side of the VC interface is the Resource View, which is also displayed in the form of a logical file hierarchy similar to the File View. ??? Need to explain, is very simple to the information here, we don't expect readers to fully learn the use of resources here, just hope that the concept of "resources" will be established here to facilitate us Rear introduction. 2.5 Code Color: ??? Perhaps the color of the code here is a bit ridiculous, but to know that in the VC, the color of the code has shown considerable information in addition to the decoration left and right. There are four colors in the VC code: black, blue, green and ash. Black is the most common color, all ordinary code is expressed in black. Blue identifies keywords, including if, for such program process keywords, int, float, data type keywords, but it only includes basic ANSI C types, new types generated by typedef or #define are not identified This is indeed quite dissatisfied. The content of the green logo is the program annotation, that is, between /*...*/ and / / to the end of the end. The gray code is a code maintained by the Class Wizard (one of the components of VC). We do not recommend users to modify, which may cause the Class Wizard's work to work abnormal and cannot guarantee that your change is finally reserved. --Class Wizard is completely likely to override this code.

3. VC Common Data Type: ??? Of course, using ANSI C data types in VC is complete, but the VC has added a lot of "new" data types, and the quotation marks is because they are not really new. Things, but rename the existing data types through Typedef. For example, the DWORD type is TypeDef unsigned long dword ;, DWORD is equivalent to Unsigned long. The purpose of this is to increase the readability, such as Handle and DWORD are unsigend long, but it can be seen from the literal sense that the Handle type represents the handle (a concept under Win, this is no longer detailed, will be detailed later Introduction), while DWORD represents a general data type, apparently a function declares that DWord getPara (Handle Hwnd) is easier to understand than Unsigned Long getpara (unsigned long hwnd). In addition, DWORD is shorter than unsigned long. Don't underestimate this point difference. When the project is huge, this will save a lot of input work when writing code. ??? The next new data that is usually used in VC is as follows: ??? Byte? Unsigned char; ??? Word? Unsigned short; ??? dword? Unsigned long; ??? long? Signed long ; ???? lpctstr? Const char *; ??? lpvoid? Void *; ??? Handle? Unsigned long; ??? There is also a relatively special type: bool, it is ANSI There is nothing in C . It is actually an integer, there are two values, true (1) and false (0), of course, it can also be used as a general integer data, but I don't want you to do this, after all, we are taken It comes to the Boolean value. In addition, it is specifically declared that in the VC, int and long are 32-bit, and the 16-bit shaping should be defined as a short shape. This is different from the previous Turbo C. In addition, in WIN programming, all pointers are 32 bits rather than 16 bits, so it has 4GB virtual address space, so there is no far pointer under DOS, near pointer concept, or say, in Win All pointers are far pools, so this troubleshooting is not considered.

4. One example - step by step start your first VC program: ??? Tell so much, maybe it has been written? Indeed, the concept of VC is much more, and if you think it is clear, it is not so easy. Just as Marx's attention to philosophy said that human awareness is never practiced, so in the end of this article, we will explain an example to handle, and let's explain some skills, I hope to understand and memories for readers. help. ??? The program we want to write is a simple Windows Console program (this is a WIN program that is very close to the program under DOS. It doesn't have a window. There is no message loop. Therefore, the structure is simple, it is very suitable for learning C Do the use of exercises). Of course, we can make a "hello world!", But I think something is more interesting, so this time we do is a small game: randomly selected a number in 0 ~ 99, then Players guess which number is selected by the computer. Here is the basic steps to establish this project: ??? First, let us build our project: ??? Open the VC, select File-> New, will then first a dialog. Remember, we have said that the basic units in the VC are project, so let's switch to the "Project" property page. A list box is displayed on the left side of the dialog, which shows a lot of project types. Generally, MFC AppWizard (DLL) (MFC Dynamic Library) (MFC AppWizard (EXE), Win32 Application Ordinary Win32 program), Win32 Console Application, Win32 Dynamic-Link Library (Win32 Dynamic Library). Here we choose Win32 Console Application, there are two text input boxes on the right, one is location, ie the location of the project folder, can be input directly, or select it through the "..." button. The other is Project, ie the project name, and the name of the project folder (the actual location of the project folder is Location / Project), and the other content uses the default option, click OK, then go to the next setting. Here, in order to make a convenience in the future, we assume that the project name is "example01", the location is "E: / Project / Teaching". ??? After completing the settings just now, the VC will start a component called Application Wizard. It is only necessary to fill in this architecture in this architecture by asking the user to generate a framework code. Code can complete a program. The App Wizard of the Console program is relatively simple, it doesn't need too much information. As you can see, it is only one step.

There are four options in the dialog shown here, and they mean: an Empty Project, an empty project, which does not contain any files, so this generally does not use this; A Simple Project, a simple item, it Contains some purely framework code. After generation, you can directly compile into an "nothing" EXE program, and then the programmer adds the functionality that needs to be implemented, and the program is completed; A "Hello World!" Appli, It produces a source code for a Hello World program. Under Console, this item is not used, but in other types of projects, we sometimes build this type of project and modify the final program, this ratio From SIMPLE Project to save more; an Application That Supports MFC, a project supporting MFC, and A Simple Application is almost, but using its project can use the non-interface part of the MFC, which is often used, after all Using MFC is more convenient than purely API. But here, we don't do not need to use the contents of the MFC, so we select the second item: a Simple Application, then click Finnish to complete the AppWizard settings. After that, I have a report, telling you what work has been conducted by AppWizard, and here is the opportunity for your last regret, one but click OK, AppWizard will start working according to the settings you just, to know, The work of AppWizard is not canceled or modified after completion, so be sure to read this report to determine if the settings you just have correct. However, for a console type program, the AppWizard settings are not much, and it seems that there is nothing to have an error, so let us go directly to OK. ??? Next Let's take a look at the appwizard in the file view! At this time, four files in the project are example01.cpp, stdafx.cpp, stdafx.h and readme.txt. EXAMPLE01.CPP is the primary file of the program. This program's main function is in this file. Herein the content: // example01.cpp: defines the entry point for the console application.//

#include "stdafx.h"

INT Main (int Argc, char * argv []) {? return 0;

Can you see that this is nothing special, so we skip this, look at the following documents. STDAFX.CPP and Stdafx.h are the pre-compiled file group. Let's take a look at their content:

// stdafx.h: include file for standard system incrude files, //? or project specificin files That Are used frequently, but // ????? Are Changed Infrequently //

#if! defined (afX_STDAFX_H__46C144A1_935D_4D36_9A04_F77C1728143A__INCluded _) # define afX_STDAFX_H__46C144A1_935D_4D36_9A04_F77C1728143A__INCLUDED_

#if _MSC_VER> 1000 # pragma overce # endif //_msc_ver> 1000 // Todo: Reference Additional Headers Your Program Requires Here

// {{AFX_INSERT_LOCATION}} // Microsoft Visual C Will Insert Additional Declarations Immediately Before The Previous Line.

#ENDIF /! Defined (AFX_STDAFX_H__46C144A1_935D_4D36_9A04_F77C1728143A__InCluded_)

// stdafx.cpp: Source File That Includes Just The Standard Includes //? EXAMPLE01.PCH WILL BE The pre-compiled header //? stdafx.obj Will Contain The Pre-Compiled Type Information

#include "stdafx.h"

// Todo: Reference Any Additional Headers you need in stdafx.h // and not in this file

??? First let's take a look at the top two lines and the last line of Stdafx.h, which is called the header file protection mechanism. In this file, first determine whether the AFX_STDAFX_H__46C144A1_935D_4D36_9A04_F77C1728143A_1_F77C1728143A_INCLUDED_ macro is defined, if it is not defined (obviously this is this), then execute the following code: Define this macro, then write the contents of the header, then one #ENDIF . So if this header file is twice, the first time I have defined AFX_STDAFX_H_46C144A1_935D_4D36_9A04_935D_4D36_9A04_F77C1728143A__INCLUDED_ Macro, the second time including this header file will not meet the "macro undefined" condition, so those heads behind The content of the file will not be repeated once. This avoids a header file that is directly or indirectly including multiple times. As for the name of this macro, it is VC, which is so long, that is to repeat if you want to be free with the user's definition, I don't think there is such a programmer who wants to play such a strange name in my own code? Then we can find a row in this file: "// Todo: Reference Additional Headers your Program Requires here". This is prompted to write the #include macro of all the headers you want to use in the project, then use the #include "stdafx.h" in all .cpp files, there is a lot of benefits: first because Convenience. When we want to change some header files, simply modify the content in stdafx.h, without changing the #include statement in each source file. In addition, there is a mechanism called "pre-compiled" in the VC: Just as we see, STDAFX.CPP has only one line of code in addition to the comment: #include "stdafx.h", when VC is compiled, it will Compile this file to generate a .pch file, then compile the files with the #include "stdafx.h", just connect this .pch file, and no need to compile the STDAFX.H included. Head file, this will greatly improve the efficiency of the compilation process (of course, the method of this technology is not as simple, I will no longer have a big interlatable). As for other content in stdafx.h, it is not necessary. So what is the header files that we need to include our project? Of course, iostream.h must be, and also need time.h (for the back to the Time function call) and stdlib.h (standard library header file), let us join in stdafx.h to join this two lines Bar. Hereinafter, the modified stdafx.h content (adding the new part). // stdafx.h: include file for standard system incrude files, //? or project specificin files That Are used frequently, but // ????? Are Changed Infrequently //

#if! defined (afX_STDAFX_H__46C144A1_935D_4D36_9A04_F77C1728143A__INCluded _) # define afX_STDAFX_H__46C144A1_935D_4D36_9A04_F77C1728143A__INCLUDED_

#if_msc_ver> 1000 # pragma overce # endif //_msc_ver> 1000 // Todo: Reference Additional Headers your program Requires here # include "iostream.h" #include "time.h" #include "stdlib.h"

// {{AFX_INSERT_LOCATION}} // Microsoft Visual C Will Insert Additional Declarations Immediately Before The Previous Line.

#ENDIF /! Defined (AFX_STDAFX_H__46C144A1_935D_4D36_9A04_F77C1728143A__InCluded_)

??? The last file is Readme.txt, which is a project description file generated by VC, which explains the general content and meaning of each file of the project. It is no longer explained here. In fact, it is some crying, I think it is not very good. ??? I have seen the content of these four files, let us start our work! Let us first create a class that implements a number, comparison of numbers. Right click on the root node of Class View (the title is eXample01 classes), select "New Class", enter the class name in the subsequent dialog: cguess (there is a habit of all class names in 'c') Above the class name, you can find the current class implementation file name "Guess.cpp", you can click on the Change button to modify, but we don't recommend this, in addition, this class does not require the base class, so the following BASE Class Let it blank. Then click OK. After you can see a class cguess in the Class View, it has an empty manual function and a destructor. Let us think about this class first below? First it takes an INT type member variable m_wvalue to record the computer "thinking" is that. Select "Add MEMBER VARIABLE" in the right-first menu in the CGUESS in the CGUESS in the CGUESS in the CGUESS in the CGUESS in the CGUESS, which pops up. Enter int in Variable Type, enter m_wvalue in Variable Name, and other points, pay attention to the default access Permissions are public, in order to comply with object-oriented encapsulation principles, we can change it to Protect, then click OK to complete the operation of adding variables. In addition, we also need a variable to record the number of comparisons, ok, let us use the same approach to add a protecty variable of a protectial permission: m_comptimes. Below, we believe that a function needs to make your computer to generate a random number. Ok, let's use the new member function command. Fill in the related content in the pop-ups: Function Type is Void, Function Declaration is initialize (), is of course the public, static, and virtual check box, then point the OK button, add a Void added in this class. Cguess :: Initialize () function, of course, it is also an empty function, the content needs to be written by us. Below we use the same way to add a function that compare numbers: int cguess :: Compare (int winput), of course, it is also public. In addition, in order to read the M_comptimes of the Access Permissions as Protect, we also need a public cguess :: getComptimes () function of the PUBLIC. After this, let's write the code, let us complete the function of this class. The code after the modified guess.cpp is as follows (the black body is increasing): // Guess.cpp: importation of the cguess.////

#include "stdafx.h" #include "guess.h"

//// construction / destruction //

Cguess :: cguess () {? M_comptimes = 0;? M_wvalue = 0;? :: SRAND (NULL));? // Initialization Random number} cguess :: ~ cguess () {

}

Void cguess :: initialize () {? m_comptimes = 0;? m_wvalue = rand ()% 100; ??? // Generate random number}

INT CGUESS :: Compare (int winput) {? m_comptimes ;? Return Winput-m_wvalue; ??? // Comparison}

INT cguess :: getcomptime () {? return m_comptimes;

?

??? Next, let's complete the rewrite of the main function, the modified EXAMPLE01.CPP content is as follows (the black body is a new part):

// Example01.cpp: defines the entry point for the console application.//

#include "stdafx.h" #include "guess.h"

Int main (int Argc, char * argv []) {? float finput;? int winput; cguess guestobj;? cout << "/ n ********* welcomed this program! *** ******* / n ";? for (;;)? {?? char sleel; ?? cout <<" I have already thought of numbers (0 ~ 99), please guess! / n "; ?? guessobj.initialize (); ?? for (;;) ?? {??? int bcomp; ??? cout << "/ n I think:"; ??? cin >> finput; ?? Winput = finput; ??? bcomp = guestobj.compare (winput); ??? if (! bcomp) ??? {???? cout << "/ n Congratulations, guess it!" 0) ??? {???? cout << "/ n sorry, you guess the bigger! / n"; ???}? ?? Else ??? {???? cout << "/ n sorry, you guess the number of small! / n"; ???} ??} ?? cout << "/ n you still want to play again ? ('N' = no, others = yes) / n "; ?? cin >> SEL; ?? cout << '/ n'; ?? IF (SEL == 'n' || SEL == ' N ') ?? {??? Break; ??}?}? Cout << "********* Thank you for your use! ********** / n"; ? Return 0;}

??? Now entering the job is over, we can compile below. Generally click on the "!" Button on the toolbar, you can build and run the program. If you want to compile the Release version, you can use the previously described method, from the SET ACTIVE Configuration Settings Currently configured to Release Release. This example is over here, and we have this example here to contact some basic methods of use of Application Wizard and Class Wizard, which believes to help readers will be helpful. Of course, this example is not perfect, there are many ways to improve, such as what we can make a limited number of guess or to record players have already guess what numbers. Of course, this part will leave more interested readers to complete. 5. Conclusion: ??? To this, I believe that all the basic concepts and operational knowledge for VC, then we have to congratulate you to finally have the ability to continue to learn. I wish you a happy study!

转载请注明原文地址:https://www.9cbs.com/read-133049.html

New Post(0)