• No results found

Appendix K Introduction to Microsoft Visual C++ 6.0

N/A
N/A
Protected

Academic year: 2021

Share "Appendix K Introduction to Microsoft Visual C++ 6.0"

Copied!
11
0
0

Loading.... (view fulltext now)

Full text

(1)

1284

Appendix K

Introduction to Microsoft Visual C++ 6.0

This appendix serves as a quick reference for performing the following operations using the Microsoft Visual C++ integrated development environment (IDE):

 Starting a new project and entering code

 Saving a project to disk

 Compiling and executing a project

 Opening an existing project

 Creating multi-file projects

Starting a New Project

The first step in creating a program with Visual C++ is to start a project. A project is a group of one or more files that make up a software application. (Even if your program consists of no more than a single source code file, it still must belong to a project.)

To start a project:

1. Launch Microsoft Visual C++. The IDE window opens, as shown in Figure K-1.

(2)

Introduction to Microsoft Visual C++ 6.0 1285

2. Click File on the menu bar. On the File menu, click New. You see the dialog box shown in Figure K-2.

3. All of the programs in this book are console programs, so click WIN32 Console Application in the list of project types.

Figure K-1

Figure K-2

Gaddis4.book Page 1285 Monday, January 6, 2003 5:16 PM

(3)

4. Enter the name of the project (such as lab6, or program5) in the Project name text box. (Do not enter an extension.)

5. When you create a project, Visual C++ creates a folder where all the project files are normally stored. The folder is called the project folder and it has the same name as the project. The Location text box lets you type in or browse to a location on your system where the project folder will be created. If you do not want to keep the default location, click the ellipses (...) button to select a different one. Once you have selected a folder the dialog box should appear similar to Figure K-3.

6. Click the OK button. You now see the dialog shown in Figure K-4.

7. Make sure “An empty project” is selected and click the Finish button. You see the dialog box shown in Figure K-5.

Figure K-3

Figure K-4

(4)

Introduction to Microsoft Visual C++ 6.0 1287

8. Click the OK button. You return to the IDE, as shown in Figure K-6.

9. Now you must insert a new C++ source file into the project. Click Project on the menu bar.

On the Project menu, click Add To Project, then click New… You see the dialog box shown in Figure K-7.

Figure K-5

Figure K-6

Gaddis4.book Page 1287 Monday, January 6, 2003 5:16 PM

(5)

10. In the list of file types, click C++ Source File.

11. In the File name text box, enter the name of the source file. (This can be the same as the project name, if you wish.) Be sure to type the .cpp extension. Your screen should look simi- lar to Figure K-8.

12. Click the OK button. You will return to the IDE. The right pane is now a text editor. This is where you type your C++ source code, as shown in Figure K-9.

Figure K-7

Figure K-8

(6)

Introduction to Microsoft Visual C++ 6.0 1289

Saving Your Project to Disk

It is important to periodically save your work. To do so, click File on the menu bar, then click Save Workspace on the File menu.

Opening an Existing Project

To open an existing project, click File on the menu bar, then click on Open Workspace… Use the resulting dialog box to browse to the location of your project. When you have located your project, double-click it.

Compiling and Executing

Once you have entered a program’s source code, you may compile and execute it by any of the fol- lowing methods:

 By clicking the button

 By pressing Ctrl+F5

 By clicking Build on the menu bar, and then clicking Execute.

Figure K-9

Gaddis4.book Page 1289 Monday, January 6, 2003 5:16 PM

(7)

The window at the bottom of the screen shows status messages as the program is compiled. Error messages are also displayed there. If you see an error message, double-click it and the editor will move the text cursor to the line of code where the error was encountered.

For example, look at the program in Figure K-10. The first cout statement is missing a semi- colon. When you double-click the error message, the text cursor moves to the line with the error.

(Actually, in this case the cursor appears on the line after the statement with the missing semico- lon. The compiler did not detect that the semicolon was missing until it encountered the begin- ning of the next line. Finding errors is not always straightforward.)

If the program compiles successfully, an MS-DOS window appears and the program runs. This is illustrated in Figure K-11.

Creating a Multi-File Project

Many of your programs consist of more than one file. For example, consider the following program:

// Filename: RectData.cpp

// This program uses multiple files

#include <iostream>

#include "rectang.h" // contains Rectangle class declaration using namespace std;

Figure K-10

(8)

Introduction to Microsoft Visual C++ 6.0 1291

// Don't forget to link this program with rectang.cpp!

int main() {

Rectangle box;

float wide, long;

cout << "This program will calculate the area of a\n";

cout << "rectangle. What is the width? ";

cin >> wide;

cout << "What is the length? ";

cin >> long;

box.setData(Wide, Long);

box.calcArea();

cout << "Here rectangle's data:\n";

cout << "Width: " << box.getWidth() << endl;

cout << "Length: " << box.getLength() << endl;

cout << "Area: " << box.getArea() << endl;

return 0;

}

This program relies on two other files: rectang.h and rectang.cpp. Before compiling this pro- gram, Rectang.h and Rectang.cpp must be added to the project. The steps listed here show you how to set the project up with all three of the existing files as members.

1. Start a new project by following steps 1 through 8 under the section Starting a New Project, at the beginning of this appendix. Name the project RectData.

2. Insert the main C++ source file into the project. Click Project on the menu bar. On the Project menu, click Add To Project, then click Files… You see the Insert Files into Project dialog box. Use the dialog box to find the file RectData.cpp on the student disk, under the Appendix K folder. When you locate the file, click it and then click the OK button. The file RectData.cpp is now a member of the project.

Figure K-11

Gaddis4.book Page 1291 Monday, January 6, 2003 5:16 PM

(9)

3. Now insert the Rectang.cpp source file into the project. Click Project on the menu bar. On the Project menu, click Add To Project, then click Files… Again, you see the Insert Files into Project dialog box. Use the dialog box to find the file Rectang.cpp on the student disk.

When you locate the file, click it and then click the OK button. The file Rectang.cpp is now a member of the project.

4. Now insert the Rectang.h header file into the project. Click Project on the menu bar. On the Project menu, click Add To Project, then click Files… As before, you see the Insert Files into Project dialog box. Use the dialog box to find the file Rectang.h on the student disk. When you locate the file, click it and then click the OK button. The file Rectang.h is now a mem- ber of the project.

5. At this point, RectData.cpp, Rectang.cpp, and Rectang.h are members of the project. The left pane of the IDE, which is called the workspace window, is shown in Figure K-12. This window allows you to navigate among all the files in the project.

The two tabs at the bottom of the workspace window allow you to switch between Class View and File View. Click on the File View tab. You should see a display similar to Figure K-13.

6. Click the small + sign that appears next to RectData files. The display expands, as shown in Figure K-14, to show folders labeled Source Files, Header Files, and Resource Files. These folders organize the names of the files that are members of the project.

Figure K-12

Figure K-13

Figure K-14

(10)

Introduction to Microsoft Visual C++ 6.0 1293

7. Notice that the Source Files and Header Files folders have small + signs next to them. This indicates the folders hold filenames. Click on the small + sign next to the Source Files and Header Files folders. The filenames RectData.cpp, Rectang.cpp, and Rectang.h are listed, as shown in Figure K-15.

8. Double-click on the name RectData.cpp. The file is displayed in the editor. (You may display any of the files in the editor by double-clicking its name.)

9. You may compile and execute the project by following the steps listed under the section Com- piling and Executing.

Creating a New Multi-File Project

If you are creating a new multi-file project (not from existing files), follow steps 1 through 12 under the section Starting a New Project. Type the code for the main source file into the editor.

When you are ready to create another source file (such as a header file for a class declaration, or a .cpp file to contain class member function definitions), follow these steps.

1. Click Project on the menu bar, click Add to Project, then click New.

2. In the New dialog box, click C++ Source File if you are creating a new .cpp file, or C/C++

Header File if you are creating a new header file.

3. In the File Name text box, enter the name of the file. Be sure to include the proper extension.

4. Click the OK button. You will return to the text editor. Enter the code for the new file.

5. Repeat the steps above for each new file you wish to add to the project.

Removing a File from a Project

To remove a file from a project, simply select the file’s name in the project browser window and press the Delete key. This operation removes the file from the project, but does not delete it from the disk.

Figure K-15

Gaddis4.book Page 1293 Monday, January 6, 2003 5:16 PM

(11)

Where Files are Created

When you create a new project, Visual C++ creates a project folder with the same name as the project. This is where all of the files associated with the project are normally stored. You can spec- ify the location of the folder when you create the project. (See step 5 of the Starting a New Project section.)

For example, suppose we’ve created a project named Lab5, and its project folder is at C:\MyProjects\Lab5. All of the C++ source files that you have created for this project will be stored in that folder. In addition, Visual C++ will create another folder named Debug, under the C:\MyProjects\Lab5 folder. The Debug folder will contain the project’s executable file and its object file(s).

It’s important to know the location of the project folder when writing code that opens files for input or output. When running a program from the Visual C++ IDE, the project folder is the default location for all data files. For example, suppose our Lab5 project creates a file with the fol- lowing code:

ofstream outputFile("names.txt");

Because we have not specified a path with the file name, the file will be created in the C:\MyProjects\Lab5 project folder. Similarly, if our program attempts to open an existing file for reading, it will look in the project folder by default. For example, suppose our Lab5 program attempts to open a file with the following statement:

ifstream inputFile("names.txt");

Because we have not specified a path, the program will look for the file in the project folder, C:\MyProjects\Lab5.

If we want to open a file in a location other than the project folder, we must provide a path as well as a file name. For example, the following statement opens a file named data.txt in the C:\Data folder.

ofstream outputFile("C:\\Data\\Data.txt");

Notice that we’ve used two backslashes in the path where you normally use only one.

Remember, in a literal string, the compiler interprets the backslash character as the beginning of an escape sequence. In order to represent a backslash as a character in a literal string, you must use two backslashes.

Here’s another example:

ifstream inputFile("A:\\names.txt");

This statement attempts to open the names.txt file in the root folder of drive A.

References

Related documents

The Cash Flow report (page 8) now shows only $294K in supplemental cash has been collected so far for the first five months of the fiscal year compared to the year-end projected

Next, as in the attack presented in Section 2.2, since the leakage produced GnuPG’s by multiplica- tion routine depends on the number of zero limbs in its second operand (See

  I f you got a new iPad, iPhone or iPod touch for Christmas, then you’ll want to know what the best games on the App Store are.. This guide to the best games for iPad and iPhone

The secretary may have responsibility for the agenda but their primary role is to take the minutes of the meeting and to make sure that a draft is sent to the members of the

As you flip through the book and point out the various circled portions of the pages you will need to instantly memorize a few of the words contained in one of the cir- cled

The staff of Edmonds Community College recommends that the Board of Trustees adopt the resolution approving the 2016-2017 International Contract program at the June 2016 meeting of

That saves you and your customers installation time, effort, and costs – because no additional cables are required for monitoring using the Sunny Beam or the Sunny Explorer... Te

CEAC: cost effectiveness acceptability curve; CEP: cost effectiveness plane; CMHT: community mental health teams; CPA: Care Approach Programme; CSRI: Client Service Receipt