0
57.2 Practice - Player Destructor
You are making a game. The given code declares a Player class with a points member. You need to add the destructor to the Player class, which should print the remaining points when the program finishes execution. *Remember, the destructor is defined using the ~ symbol. My code as below but it doesn’t work. Please help.
5 ответов
+ 5
#include <iostream>
using namespace std;
class Player
{
public:
int points;
Player(int x)
{
points = x;
points %= 5;
}
//define the destructor
~Player() {
cout << points;
}
};
int main() {
int points;
cin >> points;
Player obj(points);
}
Good Luck
+ 2
Print member <points> in destructor
cout << points << '\n';
And you missed the closing curly bracket for main() function body.
+ 1
#include <iostream>
using namespace std;
class Player
{
public:
int points;
Player(int x)
{
points = x;
points %= 5;
}
//define the destructor
~Player() {
cout << points;
}
};
int main() {
int points;
cin >> points;
Player obj(points);
}
0
Thanks Ipang !
- 1
#include <iostream>
using namespace std;
class Player
{
public:
int points;
Player(int x)
{
points = x;
points %= 5;
}
//define the destructor
~Player() {
}
};
int main() {
int points;
cin >> points;
Player obj(points);