Siêu thị PDFTải ngay đi em, trời tối mất

Thư viện tri thức trực tuyến

Kho tài liệu với 50,000+ tài liệu học thuật

© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

C++ Programming for Games Module I phần 7 potx
MIỄN PHÍ
Số trang
34
Kích thước
451.0 KB
Định dạng
PDF
Lượt xem
1714

C++ Programming for Games Module I phần 7 potx

Nội dung xem thử

Mô tả chi tiết

leak. Note that you never invoke a destructor yourself; rather, the destructor will automatically be called

when an object is being deleted from memory.

Constructors and destructors are special methods and they require a specific syntax. In particular, a

constructor has no return type and its name is also the name of the class. Likewise, a destructor has no

return type, no parameters, and its name is the name of the class but prefixed with the tilde (~). This

next snippet shows how the constructor and destructor would be declared in the Wizard class definition:

class Wizard

{

public:

// Constructor.

Wizard();

// Overloaded constructor.

Wizard(std::string name, int hp, int mp, int armor);

// Destructor

~Wizard();

...

The implementations of these function is done just as any other method, except there is no return type.

The following snippet gives a sample implementation:

Wizard::Wizard()

{

// Client called constructor with zero parameters,

// so construct a "wizard" with default values.

// We call this a “default” constructor.

mName = "DefaultName";

mHitPoints = 0;

mMagicPoints = 0;

mArmor = 0;

}

Wizard::Wizard(std::string name, int hp, int mp, int armor)

ent called constructor with parameters, so

// construct a "wizard" with the specified values.

mName = name;

mHitPoints = hp;

m

}

Wizard::~Wizard()

{

// No dynamic memory to delete--nothing to cleanup.

}

N e that we have overloaded the constructor function. Recall that the act of defining several

different versions—which differ in signature—of a function is called function overloading. We can

overload methods in the same way we overload functions.

{

// Cli

mMagicPoints = mp;

Armor = armor;

ote: Observ

156

Constructors are called when an object is created. Thus instead of writing:

Wizard wiz0;

We now write:

wiz0();// Use “default” constructor.

or:

Wizard wiz0(“Gandalf”, 20, 100, 5);// Use constructor with

// parameters.

Note that the following are actually equivalent; that is, they both use the default constructor:

Wizard wiz0; // Use “default” constructor.

W ;// Use “default” constructor.

Constructors and the Assignment Operator

and an assignment operator. If you do not explicitly define

these methods, the comp e default ones automatically. A copy constructor is a method

that constructs an object via another object of the same type. For example, we should be able to

construct a new Wizard object from another Wizard object—somewhat like a copy:

...

Wizard wiz1(wiz0);// Construct wiz1 from wiz0.

If you use the default copy constructor, the object will be constructed by copying the parameter’s bytes,

byte-by-byte, into the object being constructed, thereby performing a basic copy. However, there are

times when this default behavior is undesirable and you need to implement your own copy constructor

code.

Similarly, an assignment operator is a method that specifies how an object can be assigned to another

object. For example, how should a Wizard object be assigned to another Wizard object?

Wizard wiz0;

...

Wizard wiz1 = wiz0;// Assign wiz0 to wiz1.

If you use the default assignment operator, a simple byte-by-byte copy from the right hand operand’s

memory into the left hand operand’s memory will take place, thereby performing a basic copy.

However, there are times when this default behavior is undesirable and you need to override it with your

own assignment code.

Wizard

izard wiz0()

5.2.6 Copy

Every class also has a copy constructor

iler will generat

Wizard wiz0;

157

We discuss the details of cases where you would need to implement your own copy constructor and

assignment operator in Chapter 7. For now, just be aware that these methods exist.

Game: Class Examples

To help reinforce the concepts of classes, we will create several classes over the following subsections.

We will then use these classes to make a small text-based role-playing game (RPG).

When utilizing the object oriented programming paradigm, the first thing we ask when designing a new

program is: “What objects does the program attempt to model?” The answer to this question depends on

the program. For example, a paint program might utilize objects such as Brushes, Canvases, Pens,

Lines, Circles, Curves, and so on. In the case of our RPG, we require various types of weapon

objects, monster objects, player objects, and map objects.

After we d on what objects our program will use, we need to design corresponding classes

which define the properties of these kinds of objects and the actions they perform. For example, what

aggregate set of data members represents a Player in the game? What kind of actions can a Player

perform in the game? In addition to the data and methods of a class, the class design will also need to

consider the relationships between the objects of one class and the objects of other classes—for

example, how they will interact with each other. The following subsections provide examples for how

these class design questions can be answered.

5.3.1 The Range Structure

Our game will rely on “dice rolls” as is common in many role-playing games. We implement random

dice rolls with a random number generator. To facilitate random number generation, let us define a

range structure, which can be used to define a range in between which we compute random numbers:

5.3 RPG

have decide

// Range.h

#ifndef RANGE_H

#define RANGE_H

// Defines a range [mLow, mHigh].

struct Range

{

int mLow;

int mHigh;

};

#endif //RANGE_H

This class is simple, and it contains zero methods. The data members are the interface to this class.

Therefore, there is no reason to make the data private and so we leave them public. (Note as well that we

actually used the struct type rather than the class type as discussed in section 5.2.4).

158

Tải ngay đi em, còn do dự, trời tối mất!