< Previous | Contents | Next >

Questions and Answers

Q: What is procedural programming?

A: A paradigm where tasks are broken down into a series of smaller tasks and implemented in manageable chunks of code, such as functions. In procedural programming, functions and data are separate.

Q: What is an object?

A: An entity that combines data and functions. Q: Why create objects?

A: Because the worldand especially game worldsare full of objects. By creating your own types, you can represent objects and their relationships to other objects more directly and intuitively than you might be able to otherwise.

Q: What is object-oriented programming?

A: A paradigm where work is accomplished through objects. It allows pro- grammers to define their own types of objects. The objects usually have relationships to each other and can interact.

Q: Is C++ an object-oriented programming language or a procedural program- ming language?

A: C++ is a multi-paradigm programming language. It allows a game pro- grammer to write games in a procedural way or an object-oriented wayor through a combination of both (to name just a few options).

Q: Should I always try to write object-oriented game programs?

A: Although object-oriented programming is used in almost every commercial game on the market, you dont have to write games using this paradigm. C++ lets you use one of several programming paradigms. In general, though, large game projects will almost surely benefit from an object-oriented approach.

284 Chapter 8 n Classes: Critter Caretaker


Q: Why not make all class members public?

A: Because it goes against the idea of encapsulation. Q: What is encapsulation?

A: The quality of being self-contained. In the world of OOP, encapsulation prevents client code from directly accessing the internals of an object. Instead, it encourages client code to use a defined interface to the object.

Q: What are the benefits of encapsulation?

A: In the world of OOP, encapsulation protects the integrity of an object. For example, you might have a spaceship object with a fuel data member. By preventing direct access to this data member, you can guarantee that it never becomes an illegal value (such as a negative number).

Q: Should I provide access to data members through accessor member functions?

A: Some game programmers say you should never provide access to data members through accessor member functions because even though this kind of access is indirect, it goes against the idea of encapsulation. Instead, they say you should write classes with member functions that provide the client with all of the functionality it could need, eliminating the clients need to access a specific data member.

Q: What are mutable data members?

A: Data members that can be modified even by constant member functions. You create a mutable data member using the keyword mutable. You can also modify a mutable data member of a constant object.

Q: Why is it useful to have a default constructor?

A: Because there might be times when objects are automatically created without any argument values passed to a constructorfor example, when you create an array of objects.

Q: What is a structure?

A: A structure is very similar to a class. The only real difference is that the default access level for structures is public. You define a structure by using the keyword struct.

Exercises 285


Q: Why does C++ have both structures and classes? A: So that C++ retains backward compatibly with C. Q: When should I use structures?

A: Some game programmers use structures to group only data together, without functions (because thats how C structures work). But its probably best to avoid structures whenever possible and use classes instead.