Loading...


bookmark - More Advanced C++ Polymorphic / Abstract / Virtual example

More Advanced C++ - Polymorphic / Abstract / Virtual example

 
 Discussion by qwijibow with 4 Replies.
 Last Update: October 11, 2008, 11:54 am
 
bookmark - More Advanced C++ Polymorphic / Abstract / Virtual example  
Quickly Post to More Advanced C++ Polymorphic / Abstract / Virtual example w/o signup Share Info about More Advanced C++ Polymorphic / Abstract / Virtual example using Facebook, Twitter etc. email your friend about More Advanced C++ Polymorphic / Abstract / Virtual example Print
Reply / Comment New Discussion / Topic Share / Bookmark E-Mail a Friend Print

This example assumes you already know basic c++ functions and classes.
it is by no means a complete c++ tutorial, but should give you a good idea of the power of c++.

In this tutorial, i will be using virtualisation, abstraction, and polymorphism to draw a simple picture.
the code is not complete, because i dont want to tie it into any specific operating system.
i will use onlt standard template library finctions, and graphics requires the use of OS specific API's/

We are going to write a program, which holds an array or different shapes.
and (if complete) would draw thenm ot the screen.. (for this example, the will just output somthing like "I am a circle, radius 12, x=20, y=50"

It doesnt sound impressive, but we will be using a single array to hold different classes without using a container. In other words, its like having an array that holds some integers, some booleans, and some strings. it is normally not allowed.

first, before the code, some simple definitions.

=============================
Virtual functions:
=============================
A virtual function is a function which apears to exist to some parts of the code, but aprears not to exist to other parts of the code.

a Pure virtual function is the same, but has no function body. if a class has at least one pure virtual function, then it is considered abstract. meaning it cannot be used directly, but can be used as a template for other functions.

Take a look at the following example program

CODE

#include<iostream>
using namespace std;

class base_class {
public:
[tab][/tab]void function1() {
 cout << "function1 of base_class" << endl;
[tab][/tab]}

[tab][/tab]virtual void function2() {
 cout << "function2 of base_class" << endl;
[tab][/tab]}
};

class sub_class : public base_class {
public:
[tab][/tab]void function2() {
 cout << "function2 of sub_class" << endl;
[tab][/tab]}
};

class sub_class2 : public base_class {};

int main() {

[tab][/tab]base_class BASE;
[tab][/tab]sub_class SUB_A;
[tab][/tab]sub_class2 SUB_B;

[tab][/tab]BASE.function1(); // outputs "function1 of base_class"
[tab][/tab]BASE.function2(); // outputs "function2 of base_class"
[tab][/tab]
[tab][/tab]SUB_A.function1(); // outputs "function1 of base_class"
[tab][/tab]SUB_A.function2(); // outputs "function2 of sub_class"
[tab][/tab]
[tab][/tab]SUB_B.function1(); // outputs "function1 of base_class"
[tab][/tab]SUB_B.function2(); // outputs "function2 of base_class"

[tab][/tab]return 0;
}


virtual function2 in the base class apears to exist, except to the class sub_class, because this class defines its own function2, it over-rides the virtual function.

we could have made function2 a pure virtual function be declairing it like so....

CODE

virtual void function2() = 0;


a pure virtual function exists ONLY to be over-ridden.
you cannot declair an instance of a class containing pure virtual functions, and so if this had been the case, the line

CODE

base_class BASE;
would have caused a compile error.
a class containing pure virtual functions is called abstract.

=============================
Polymorphic objects. (the clever pointer)
=============================

In C++, the pointer rules have been relaxed.
a pointer of type XYZ can point to any class derived from XYZ

the following code example is perfectly legal in c++

CODE

class commom{};

class tree : public common{};
class bird : public common{};
class car : public common{};
class pan_galactic_gargle_blaster : public common{};

int main() {

[tab][/tab]common* array[5];
[tab][/tab]
[tab][/tab]array[0] = new common;
[tab][/tab]array[1] = new tree;
[tab][/tab]array[2] = new bird;
[tab][/tab]array[3] = new car;
[tab][/tab]array[4] = new pan_galactic_gargle_blaster;

[tab][/tab]return 0;
}


=====================================================
MAIN example, combining the 2 examples above.
======================================================
Armed with the ability to have an array of different types, and the ability to over ride functions,
we can do some very clever things.

we are going to write a simpe drawing program.
it will be an array of different shapes, the base class will have a draw() function
which draws different shapes, depending on how it is used. (polymorphic, to change shape! lol )

// include the usual stuff.

CODE

#include<iostream>
#include<vector>

using std::vector;
using std::cout;
using std::cin;


now we need our common base class

CODE

class shape {

protected:
[tab][/tab]int x_position;
[tab][/tab]int y_position;
[tab][/tab]int width;
[tab][/tab]int height;

public:
[tab][/tab]virtual void draw() = 0;

[tab][/tab]void setX(int x) {
 x_position = x;
[tab][/tab]}

[tab][/tab]void setY(int y) {
 y_position = y;
[tab][/tab]}

[tab][/tab]void setW(int w) {
 width = w;
[tab][/tab]}

[tab][/tab]void setH(int h) {
 height = h;
[tab][/tab]}
};


This is ab abstract class because it contains a pure virtual function draw.
the size variables, and manipulation functions are common to all shapes, so they go in the base class.

now lets add some shapes/

CODE

class triangle : public shape {

public:
[tab][/tab]void draw() {
[tab][/tab]
 cout << "i am a triangle" << endl;
 cout << "[tab][/tab]height:" << height << endl;
 cout << "[tab][/tab]width:" << width << endl;
 cout << "[tab][/tab]X:" << x_position << endl;
 cout << "[tab][/tab]Y:" << y_position << endl;
[tab][/tab]}
};

class circle : public shape {

public:
[tab][/tab]void draw() {
[tab][/tab]
 cout << "i am a circle" << endl;
 cout << "[tab][/tab]radius:" << height << endl;
 cout << "[tab][/tab]X:" << x_position << endl;
 cout << "[tab][/tab]Y:" << y_position << endl;
[tab][/tab]}
};

class rectangle : public shape {

public:
[tab][/tab]void draw() {
[tab][/tab]
 cout << "i am a rectangle" << endl;
 cout << "[tab][/tab]height:" << height << endl;
 cout << "[tab][/tab]width:" << width << endl;
 cout << "[tab][/tab]X:" << x_position << endl;
 cout << "[tab][/tab]Y:" << y_position << endl;
[tab][/tab]}
};

class dot : public shape {

public:
[tab][/tab]void draw() {
[tab][/tab]
 cout << "i am a dot" << endl;
 cout << "[tab][/tab]X:" << x_position << endl;
 cout << "[tab][/tab]Y:" << y_position << endl;
[tab][/tab]}
};


each class needs to override the pure virtual function in the shape class.
if a class failed to override the draw() function, then that class would in turn become abstract.

finally, let se out super cool porgram in action.

CODE

int main() {

[tab][/tab]vector<shape*> shapes;

[tab][/tab]shapes.push_back( new triangle() );
[tab][/tab]shapes.push_back( new circle() );
[tab][/tab]shapes.push_back( new rectangle() );
[tab][/tab]shapes.push_back( new dot() );
[tab][/tab]shapes.push_back( new triangle() );
[tab][/tab]shapes.push_back( new circle() );
[tab][/tab]shapes.push_back( new rectangle() );
[tab][/tab]shapes.push_back( new dot() );

[tab][/tab]// set all the shapes member variables (idealy, use random)
[tab][/tab]for(int n=0; n< shapes.size(); n++) {
 shapes[n]->setX(1);
 shapes[n]->setY(2);
 shapes[n]->setH(3);
 shapes[n]->setW(4);
[tab][/tab]}

[tab][/tab]// draw the picture !
[tab][/tab]for(int n=0; n< shapes.size(); n++) {
 shapes[n]->draw();
[tab][/tab]}
[tab][/tab]
      // clean up, prevent memory leaks.
[tab][/tab]for(int n=0; n< shapes.size(); n++) {
 delete shapes[n];
[tab][/tab]}

[tab][/tab]return 0;
}


the vector MUST be a vector of pointers to shapes, and not a vector of shapes.
the the intelligent pointer that knows what functions to call.

and just for fun, the output of the compiled program.

QUOTE

bash-2.05b$ g++ TEST.cpp -o TEST
bash-2.05b$ ./TEST
i am a triangle
        height:3
        width:4
        X:1
        Y:2
i am a circle
        radius:3
        X:1
        Y:2
i am a rectangle
        height:3
        width:4
        X:1
        Y:2
i am a dot
        X:1
        Y:2
i am a triangle
        height:3
        width:4
        X:1
        Y:2
i am a circle
        radius:3
        X:1
        Y:2
i am a rectangle
        height:3
        width:4
        X:1
        Y:2
i am a dot
        X:1
        Y:2


Even advanced C++ isnt too complicated when you understand whats happening.

Any questions / comments / surgestions ?

   Fri May 6, 2005    Reply         

Can you make it easier to understand, You explained it kind of confusingly

[note=moonwitch] don't quote an unneeded long quote.[/note]

   Mon Nov 7, 2005    Reply         

Wow, great tutorial. I couldn't find any good tutorials yet, so I stopped trying to find good C++ resources on the net and focust on books and things. I must say I realy like this one. Could you also write us a tutorial about game programming in C++? Then I realy would be delighted ;).

-=jeroen=-

   Tue Nov 8, 2005    Reply         


QUOTE

Could you also write us a tutorial about game programming in C++


I can do the next best thing...
Post a link to a great game programming tutorial.

(well, not really a game programming tutorial, its an OpenGL tutorial, that uses games as its example programs. but still very good)

http://nehe.gamedev.net/

   Sun Feb 26, 2006    Reply         

Great tutorial, thanks for posting this, Ill be sure to test this out when I have time. It looks like a very good tutorial.

   Sat Oct 11, 2008    Reply         

Quickly Post to More Advanced C++ Polymorphic / Abstract / Virtual example w/o signup Share Info about More Advanced C++ Polymorphic / Abstract / Virtual example using Facebook, Twitter etc. email your friend about More Advanced C++ Polymorphic / Abstract / Virtual example Print
Reply / Comment New Discussion / Topic Share / Bookmark E-Mail a Friend Print

Similar Topics:

Requesting Advanced Hosting

dear Admin, I have more than 50 Posts registered hence I feel I am eligible for the advanced hosting. I will use the space to create a site for my friends and community. subdomain name : thedevil.astahost.com username : thedevil package name : advanced hos ...more

   01-Jan-2005    Reply         

Advanced Visual Basic

This is a advanced visual basic programming topic I am working on a vb project. This is some advanced examples: In this project you are going to create a email program in visual basic. first make sure mapi controls is on your vb project ...more

   18-Aug-2005    Reply         

How Do I Virtual Pc web Server C...

Does anyone have any experience with MS Windows Virtual PC. I need help with setting up my os for an optimal sandbox configuration. What I would like to do once Virtual PC is setup, is to experiment by learning how to work with Content Management Systems on Windows XP and/or Vista. I ...more

   25-Nov-2007    Reply         

Handling Keyboard Input In Win32 virtal key codes and scan codes   Handling Keyboard Input In Win32 virtal key codes and scan codes (2) (2) Handling Errors To Help You In Debugging finding the source of your errors  Handling Errors To Help You In Debugging finding the source of your errors