Program describes system objects inter-acting
|
||
---|---|---|
328 | B. | |
This appendix covers a host of topics, including:
• A brief explanation of the uses and features of object-oriented programming
In an object-oriented view of programming, a program describes a system of objects inter-acting. This is contrary to a procedure-oriented view of programming, in which a program describes a series of steps to be performed.
Object-oriented programming involves a few key concepts. The most basic of these is abstraction, which simplifies writing large programs. Another is encapsulation, which makes it easier to change and maintain a program. The concept of class hierarchies is a powerful classification tool that can make a program easily extensible.
329 | |
---|---|
What Is Object-Oriented Programming? |
All procedural languages support procedural abstraction, in which a piece of source code is put into a function and reused this way. For example, consider matrix multiplication:
FLOAT pM[16];
ZeroMemory( pM, sizeof(D3DXMATRIX) );
for( WORD i=0; i<4; i++ )
for( WORD j=0; j<4; j++ )
for( WORD k=0; k<4; k++ )
pM[4*i+j] += pM1[4*i+k] * pM2[4*k+j];
memcpy( pOut, pM, sizeof(D3DXMATRIX) );
|
||
---|---|---|
330 | B. |
// animation frames in mesh
// stores vertices per animation frame in // vertices[FrameNum][VertexNum]int iNumTriangles; // Mesh triangles
TRIANGLEVERT *pTriangles;
int iNumTextures; // Mesh textures
TEXNAMES *pTexNames;
LPDIRECT3DTEXTURE8* pTexturesInterfaces;
TEXCOORDS *pfTextureCoords;
// Mesh tex coordinates TEXCOORDS *pfEnvTextureCoords; // not used here
};int iMeshFrameNum;
VMESHFRAME *vMeshFrames;// animation frames in mesh
// stores vertices per animation frame in\ // vertices[FrameNum][VertexNum]
331 | |
---|---|
What Is Object-Oriented Programming? |
Now MD3MESH is an abstract data type that might be used in combination with functions as their parameter. These functions handle the details of filling the specific fields.
This traditional procedural approach lets you view data abstraction and procedural abstrac-tion as two distinct techniques. A more elegant way would be to link them. The following section describes the object-oriented programming approach, which uses classes.
long ReadLong(FILE* File); // file i/o helper
short ReadShort(FILE* File);
FLOAT ReadFloat(FILE* File);
void CreateVB(LPDIRECT3DDEVICE8 lpD3Ddevice); // vertex buffer system void DeleteVB();
void CreateTextures(LPDIRECT3DDEVICE8 lpD3Ddevice); // texture system void DeleteTextures();
public:
// class methods
BOOL CreateModel( char *fname, LPDIRECT3DDEVICE8 lpD3Ddevice);
void DeleteModel();
void InitDeviceObjects(LPDIRECT3DDEVICE8 lpD3Ddevice);
void DeleteDeviceObjects();
void Render(LPDIRECT3DDEVICE8 lpD3Ddevice);
CMD3Model();
// constructor/destructor ~CMD3Model();
};
332 | B. |
|
|
|||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
A truly encapsulated class surrounds or hides its data with its functions so that you can access the data only by calling the func-tions. An illustration might look like Figure B.1. On the left are the public data mem-bers, and on the right are the private data members.
|
---|
|
|
---|