Is it possible for an instance of a class to destroy itself?

Hello,

I'm working on a final project for my C++ Objects class, I'm basically making a very simplistic text-based battle arena game, where the player is set up against 2 different enemies, and the player and enemies will attack each other till either the players health or the enemies health drops to 0 or less.

I have a base class called Enemy, which holds some of the basic information common between any enemy.

Enemy class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#ifndef ENEMY_H
#define ENEMY_H
#include <iostream>
#include <cstring>
using namespace std;

//************************************
// constant variables for char arrays
//************************************
const int NAME = 20;
const int TYPE = 10;

//****************************************
// Enemy Class will create a base for any
// enemy in the game.
//****************************************
class Enemy
{
protected:
	char name[NAME];
	char type[TYPE];
	int level;
	int health;
	int strength;
	int gold;
	bool dead;
public:

	//***************************************************
	// Default constructor, if no arguments are passed
	// on call, then set default values for enemy stats
	//***************************************************
	Enemy()
	{
		strcpy_s(name,"");
		health = 1;
		strcpy_s(type,"");
		level = 1;
		strength = 1;
		gold = 0;
	}

	//***************************************************
	// Constructor, accepts 3 arguments to set the 
	// stats of the enemy
	//***************************************************
	Enemy(char n[],char t[], int lv, int hp, int str, int g)
	{
	 strcpy_s(name,n);
	 strcpy_s(type,t);
	 level = lv;
	 health = hp;
	 strength = str;
	 gold = g;
	}

	//****************************************************
	// Used to display the stats of the enemy, used mainly
	// for testing purposes
	//****************************************************
	void getStats() const
	{
		cout << "Name: " << name << endl;
		cout << "Level: " << level << endl;
		cout << "Health: " << health << endl;
		cout << "Strength: " << strength << endl;
		cout << "Gold: " << gold << endl;
	}

};

#endif 



This is my Soldier class which is a child of the Enemy class

Soldier class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#ifndef SOLDIER_N
#define SOLDIER_N
#include <cstring>
#include <iostream>
using namespace std;


//*********************************************
// A child of the Enemy class, this 
// class holds not only the base variables
// for any enemy, but adds an armor variable
//*********************************************
class Soldier: public Enemy
{
public:
	int armor;

	Soldier()
	{ level = 1;}

	//**************************************************
	// destructor, if the object is destroyed, then the 
	// instance of the enemy hp has reached zero and is
	// no longer neede
	~Soldier()
	{
		cout << name << " has been killed" << endl;
	}


//***************************************************
// Constructor that accepts The level of the enemy
// as an argument.
//***************************************************
Soldier(int lv, char n[])
	{
	 strcpy_s(name,n);
	 strcpy_s(type,"Soldier");
	 level = lv;
	 armor = 5*level;
	 health = 10*level;
	 strength = 3*level;
	 gold = 50*level;
	}
//****************************************
// Displays basic stats about the enemy
//****************************************
void getStats() const
	{
		cout << "Name: " << name << endl;
		cout << "Type: " << type << endl;
		cout << "Level: " << level << endl;
		cout << "Armor: " << armor << endl;
		cout << "Health: " << health << endl;
		cout << "Strength: " << strength << endl;
		cout << "Gold: " << gold << endl;
	}

int getHealth() const
{
	return health;
}


};

#endif 


What I need to know, is it possible to destroy a class before the end of the actual program, allowing the destructor to execute letting the player know that the enemy is dead?

Or am I going about this all wrong?

any help would be greatly appreciated.
If it was allocated dynamically you can use delete
But using the destructor only to print some text is a bad design, you should add a method to get whether it is alive ( you can use getHealth ) and print that outside the class
Thanks Bazzy,

how does this look:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include "Enemy.h"
#include "Soldier.h"
#include "Beast.h"
using namespace std;






//////////////////////////////////////////////////////////////////////////////////


int main()
{
	//create a pointer to a new Soldier object
	Soldier *SoldrNME = new Soldier();
	

	SoldrNME->setHealth(-2);
	SoldrNME->getStats();

	if (SoldrNME->getHealth()<= 0)
	{
		cout << SoldrNME->getName() << " has been killed\n";
		delete SoldrNME;
	}

	cout << "This is the end of the program" << endl;

	/*
	Soldier Soldr(6,"Gladiator");
	Soldr.getStats();

	cout << "\n\n";

	Beast beast(5,"Feral Tiger");
	beast.setHealth(2);
	beast.getStats();
	*/

	return 0;
}


this gives me the results I wanted, but something slightly confuses me,
///////////////////////////////////////
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//I created a getName method in order to get the name of the enemy do 
// display in the kill message, but whenever I wrote it as char getName(),
// it said it couldn't convert name[20] to name (not an array), what did me
// putting the asterisk in front of getName() do to make this work exactly?
//
char *getName() 
{
	return name;
}
//////////////////////////////////////
int getHealth() const
{
	return health;
}
It is because you name is a char* (an array) so you are returning a char* (an array), not just a single character.
I see, thanks guys
Topic archived. No new replies allowed.