57.2 Practice - Player Destructor | Sololearn: Learn to code for FREE!
¡Nuevo curso! ¡Todo programador debería aprender IA Generativa!
Prueba una lección gratuita
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.

21st Dec 2021, 10:49 PM
Chin Eu
Chin Eu - avatar
5 Respuestas
+ 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
25th Jan 2022, 5:53 PM
Muhammad Alif Deva Rizqon
Muhammad Alif Deva Rizqon - avatar
+ 2
Print member <points> in destructor cout << points << '\n'; And you missed the closing curly bracket for main() function body.
22nd Dec 2021, 12:52 AM
Ipang
+ 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); }
10th Aug 2022, 1:22 AM
Najwan Najmussabah
0
Thanks Ipang !
22nd Dec 2021, 3:36 AM
Chin Eu
Chin Eu - avatar
- 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);
21st Dec 2021, 10:49 PM
Chin Eu
Chin Eu - avatar