C++ All-In-One Desk Reference for Dummies by John Paul Mueller & Jeff Cogswell

C++ All-In-One Desk Reference for Dummies by John Paul Mueller & Jeff Cogswell

Author:John Paul Mueller & Jeff Cogswell [Mueller, John Paul & Cogswell, Jeff]
Language: eng
Format: epub
Tags: Reference:Computers
ISBN: 9780470317358
Publisher: For Dummies
Published: 2009-01-02T00:00:00+00:00


Figure 3-5: Use the Breakpoints window to access a number of breakpoints at once.

Watching, Inspecting, and Changing Variables

When you stop at a breakpoint in a program, you can do more than just look at the code. You can have fun with it! You can look at the current values of the variables, and you can change them.

Listing 3-2 is a sample program that you can use to try out these examples of inspecting, changing, and watching variables. Please note that this program is similar to Listing 3-1, earlier in this chapter, but you should see some differences. Specifically, we added a line to the SetNumberOfPieces member function

newamount = newamount * 20;

We added a new function called SpecialMath, and we added an i variable to main that is initialized to 10; then we doubled it, and we passed it into the SetNumberOfPieces function.

Listing 3-2: Using a Program for Breakpoints and Inspections

#include <iostream>

using namespace std;

class BrokenMirror

{

private:

int NumberOfPieces;

public:

int GetNumberOfPieces();

void SetNumberOfPieces(int newamount);

BrokenMirror() : NumberOfPieces(100) {}

};

int BrokenMirror::GetNumberOfPieces()

{

return NumberOfPieces;

}

void BrokenMirror::SetNumberOfPieces(int newamount)

{

newamount = newamount * 20;

NumberOfPieces = newamount;

}

int SpecialMath(int x)

{

return x * 10 - 5;

}

int main()

{

int i = 10;

BrokenMirror mirror;

i = i + SpecialMath(i);

mirror.SetNumberOfPieces(i);

cout << mirror.GetNumberOfPieces() << endl;

// Clear this comment if you want the application to stop to

// display the results.

// system(“PAUSE”);

return 0;

}

Watching the local variables

To watch the local variables in your program, follow these steps:

1. Compile this program with debug information on.

2. Set a breakpoint at the int i = 10; line in main.

3. Click Debug/Continue.

4. When the debugger stops at the breakpoint, click the Next Line button on the Debugger toolbar so that you are one line beyond the following line:

int i = 10;

5. Choose Debug⇒Debugging Windows⇒Watches.

You see the Watches window, as shown in Figure 3-6.

Figure 3-6: The Watches window shows the value of variables and objects.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.