You Like My Tutorials? Click The Banner To Keep 'Em Coming!

glGameDeveloper(): The Game Developer's Source for OpenGL Coding
Main Menu
Latest News
Archived News
Tutorials
Links
E-Mail Me

Other
Message Board
My Guestbook

Tutorials -- Working with Objects
 


Working with Objects Using Classes Page 2 of 4
< Previous Jump to Page: 1 | 2 | 3 | 4 Next >

Defining Classes and Making Instances of them

First off lets go over the syntax of a class definition. The greyed out text is optional and is only used in certain cases.

class CLASSNAME : public DERIVEDCLASSNAME {
public:
	int VARIABLE;
	float VARIABLE;
	
	void FUNCTION();
};

In the above code, the words in all caps are those specific to the class. Here's a listing of what they mean.

  • CLASSNAME - the name of the class, this is what you use to define instances of this class
  • DERIVEDCLASSNAME - This is completely optional. It's the name of the class CLASSNAME will be derived from. In other words, CLASSNAME will be a subclass of DERIVEDCLASSNAME. Take out the whole ": public DERIVEDCLASSNAME" if you aren't deriving this class from another class.
  • VARIABLE - after the line "public:" you can define variables just as you would anywhere else. The only difference is that these variables belong to this class.
  • FUNCTION() - a function belonging to this class.

Each instance of the class you create contains the variables and functions of that class. Also, if you define a subclass of a class, it too will contain these variables and functions without having to redefine them.

Referring to the blueprint-house analogy mentioned earlier: a blueprint isn't a house. You have to make a house based on the blueprint for it to be anything. Similarly you have to make an instance of a class in order to do anything with it. Here's how we'd make an instance of CLASSNAME:

CLASSNAME InstanceOfClassName;

Now if we can set InstanceOfClassName.VARIABLE = 1 or execute the function InstanceOfClassName.FUNCTION().

Lets go ahead and introduce my patented BasicObjectTM class. This one includes all the stuff you'll need to make an object in 3d space, position it, and give it velocity and acceleration.

Working with Objects Using Classes Page 2 of 4
< Previous Jump to Page: 1 | 2 | 3 | 4 Next >

This site best viewed with MS Internet Explorer 5.0 at 16-bit color and at least 800x600.

All Contents of this site are Copyright 1999, 2000 Joe Zimmerman. No part of this site (code, HTML, etc.) may be reproduced without my permission.

Click Here!