Advertisement Jump to content


This topic is now archived and is closed to further replies.

Paradigm Shift 2000

Object Oriented Programming

This topic is 6322 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have read several books on C++ and the Object Oriented programming paradigm and while I understand the necessity of using a data driven design, I still fail to comprehend why I would ever need to implement a pure virtual base class like CObject that has absolutely no code associated with it. Could somebody who knows please explain this? Paradigm Shift 2000

Share this post

Link to post
Share on other sites
Why? To define properties and methods which are common to all the classes in your class hierarchy. Java is a good example. The Object class defines such methods as clone(), toString(), equals(), and hashCode(). Because everything extends Object, you know that you can always use these methods.

Anyway, that''s what I find useful about it. The OO Gurus can probably offer you a better explanation.


Share this post

Link to post
Share on other sites
So that code which requires one of a number of object types can have a uniform interface. If you have the base class "Parent" and the derived classes "Derv1", "Derv2" and "Derv3", if your code is applicable to any of Derv1, Derv2 or Derv3, a pure virtual base class allows you to employ any of them in your code and be sure that you are referring to an instantiated object.

Pure virtual objects can never be created, so that relieves you from wondering whether the object you''re about to work on is defined or not.

Share this post

Link to post
Share on other sites
The whole idea of OOP is to encasulate your code and decouple it from your logic. Doing this promotes reuse and makes you code easier to understand and debug.

In C you would have to do something along the lines of this:

for(int i=0;i drawEnemy(enemy);

for(int i=0;i drawMissile(misiles[i]);

for(int i=0;i drawPlayer(players[i]);

As you can see each objects needs a draw function that would have to know how to draw the data to the screen. Using OOP all your objects could draw themselves by having a base class or an interface with the draw method in it and just extend that class and overide the draw method or implement the draw method (in the case of virtual/interface)

then your code could do this :

for(int i=0;i gameObjects[i].draw();

That''s the idea. There are plenty of resources on the web and in the book store that can help you understand OOP. Believe me, once you understand the principles you will be amazed at how much faster you can code once you have a good design. You will also find yourself coding less and actually reusing code too!

Share this post

Link to post
Share on other sites
Encapsulating data and methods into a class makes sense. I''ve built container classes before, especially for file loaders where I want to insulate myself from that messy code for loading image files in. But I still don''t understand why I would need to derive these containers from a pure virtual class. Like for instance: Why would I want to define a pure virtual class with place holders for void LoadFile(const char * filename) and void UnloadFile(void) and subclass file loaders from this, overriding the LoadFile() and UnloadFile() methods with code specific to each file type, when I could just create a new class for each different file type because ultimately, the data and method of retrieving the data for each file type is going to be different. I''m going to wind up redefining the entire class after subclassing it anyway. So, like I said, why should I want to do something like this?

Paradigm Shift 2000

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
Ok, I learned polymorphism by example. One of the best examples i''ve seen was with a paint program that I had to do at school.

In my paint program, I needed a way to draw and undo paint objects. The objects that I supported were :
1.) a point
2.) a line
3.) a box
4.) a circle

Now, say that you wanted to keep track in which order the items got drawn, as this is extremely important. You would need to store each item in a list or vector. ( i''m referring to the stl::list and stl::vector classes ) If I did not use polymorphism, i would have to keep a separate list for each object type, or I would need to implement each type of object with a switch statement in my main implementation class( this is not good as it would require a lot of maintenance and changes to your main drawing code everytime you needed to implement a new type, this would work, but the implementation is not very clean and maintainable.) Now say that you want a way to undo the objects in the list, how do you maintain order? You can do it, but again, it would be difficult.

A clean implementation uses polymorphism.

I created a base virtual class called CShape
CShape had methods like Draw(), Undo()
CShape had attributes like XPosition, YPosition

I then created an implementation class for each type of object that I want to support, so I created a CLine : public CShape
CPoint : CShape
CBox : CShape
CCircle : CShape

I then created one list to store all objects drawn

Now, I can easly iterate through the list calling the Draw method on all objects. I let polymorphism decide if it should draw a point, line, box or circle.

If I need to add a new drawable object, I can easly add a new implementation object without impacting my main drawing routines and undo routines.

CShape *NewObject = CLine();

// This method actually calls the draw method on CLine and not CShape;

// Add your new object to the object list

Does this make sense?
This is hard to explain in writing.

In your example of file loader, I have a base FileDecoder class, I then have implemented a decoder class to support .bmp, .jpg, .gifs. It really doesn''t matter to my graphic engine which type of format I want to support, the graphic engine only cares about a Direct X Surface.

Poylmorphism is one of the hardest to grasp..A lot of new c++ developers sometimes make mistakes as referring to it as function overloading, which is not the case, what so ever.

Polymorphism guarantees that by calling a virtual method on a base class, that it will call the correct method for you.


Share this post

Link to post
Share on other sites
Hmm.. So then that means, say i made a pure virtual base called CFile, and derived 4 file loaders from them
CBmp : public CFile
CWav : public CFile
CJpg : public CFile
CTile : public CFile

and I did something like this:

CFile file("blah.bmp");

It would automatically use my CBmp class instead of one of the other 3?

Paradigm Shift 2000

Share this post

Link to post
Share on other sites

You can not directly instantiate a pure virtual base class. You would need to create an instance of a derived class and assign it to a pointer of the base class.

You would do the following:

// This constructs a CBmpFile object, and assigns it to File

CFile* File = CBmpFile();

// Now Execute the Virtual Methods


// Finished with the file, so release


Does that make sense?


Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!