![]() ![]() |
![]() ![]() ![]() ![]() |
![]() ![]() |
![]() | Section 7: Class Declarations |
In addition to all of the member functions you'll create for your objects, there are two special kinds of functions that you should create for every object. They are called constructors and destructors. Constructors are called every time you create an object, and destructors are called every time you destroy an object.
The constructor's job is to set up the object so that it can be used. Remember in Chapter 3.2, when we first declared a variable? Before we initialized the variable, it stored a garbage value. We needed to initialize the variable to 0 or to some other useful value before using it. The same is true of objects. The difference is that with an object, you can't just assign it a value. You can't say:
Player greenHat = 0;because that doesn't make sense. A player is not a number, so you can't just set it to 0. The way object initialization happens in C++ is that a special function, the constructor, is called when you instantiate an object. The constructor is a function whose name is the same as the object, with no return type (not even void). For our video game, we'll probably want to initialize our Players' attributes so that they don't contain garbage values. We might decide to write the constructor like this:
Player::Player() { strength = 10; agility = 10; health = 10; }We would also have to change the class declaration so that it looks like this:
class Player { int health; int strength; int agility; Player(); // constructor - no return type void move(); void attackMonster(); void getTreasure(); };
One problem with this constructor is that all of the players will be initialized to have strength=10, agility=10, and health=10. We might want to create players with different values for strength and agility to make our game more interesting. So, we can add a second constructor, which has parameters for strength and agility. Our class declaration would now look like this:
class Player { int health; int strength; int agility; Player(); // constructor - no return type Player(int s, int a); // alternate constructor takes two parameters void move(); void attackMonster(); void getTreasure(); };and we would add a function definition for the alternate constructor, which looks like this:
Player::Player(int s, int a) { strength = s; agility = a; health = 10; }Now, when we want to instantiate the
Player
object four
times, we can do the following:
Player redHat; // default constructor Player blueHat(14, 7); // alternate constructor Player greenHat(6, 12); // alternate constructor Player yellowHat(10, 10); // alternate constructor
Destructors are less complicated than constructors. You don't call them explicitly (they are called automatically for you), and there's only one destructor for each object. The name of the destructor is the name of the class, preceeded by a tilde (~). Here's an example of a destructor:
Player::~Player() { strength = 0; agility = 0; health = 0; }Since a destructor is called after an object is used for the last time, you're probably wondering why they exist at all. Right now, they aren't very useful, but you'll see why they're important in Section 8.3.
![]() ![]() |
![]() ![]() ![]() ![]() |
![]() ![]() |