To see what occurs when a Cardgame object is destroyed, view the destructor for the Cardgame class.
On the menu bar, choose View > Class View.
In the Class View window, expand the Game project tree and select the Cardgame class to display the class members and methods.
Open the shortcut menu for the ~Cardgame(void) destructor and then choose Go To Definition.
To decrease the totalParticipants when a Cardgame ends, add the following code between the opening and closing braces of the Cardgame::~Cardgame destructor.
C++
totalParticipants -= players;
cout << players << " players have finished their game. There are now "
<< totalParticipants << " players in total." << endl;
The Cardgame.cpp file should resemble the code below after you change it:
C++
#include"Cardgame.h"#include<iostream>usingnamespacestd;
int Cardgame::totalParticipants = 0;
Cardgame::Cardgame(int players)
: players(players)
{
totalParticipants += players;
cout << players << " players have started a new game. There are now "
<< totalParticipants << " players in total." << endl;
}
Cardgame::~Cardgame()
{
totalParticipants -= players;
cout << players << " players have finished their game. There are now "
<< totalParticipants << " players in total." << endl;
}
On the menu bar, choose Build > Build Solution.
When the build completes, run it in Debug mode by choosing Debug > Start Debugging on the menu bar, or by choosing the F5 key. The program pauses at the first breakpoint.
To step through the program, on the menu bar, choose Debug > Step Over, or choose the F10 key.
Notice that after each Cardgame constructor executes, the value of totalParticipants increases. When the PlayGames function returns, as each Cardgame instance goes out of scope and is deleted (and the destructor is called), totalParticipants decreases. Just before the return statement is executed, totalParticipants equals 0.
Continue stepping through the program until it exits, or let it run by choosing Debug > Run on the menu bar, or by choosing the F5 key.
This module helps you evaluate your debugging skills. You start with a C# console application that contains code logic issues and a specification that describes the requirements. You’re challenged to demonstrate your ability to identify and fix the issues using the Visual Studio Code debugger tools.