C++ Virtual Function Confusion

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
epicasian
Chaos Rift Junior
Chaos Rift Junior
Posts: 232
Joined: Mon Feb 22, 2010 10:32 pm
Current Project: Gigazilla Engine
Favorite Gaming Platforms: Dreamcast, SNES, PS2, PC
Programming Language of Choice: C/++
Location: WoFo, KY

C++ Virtual Function Confusion

Post by epicasian »

Hey guys,
I'm reading through my C++ book and right now I'm on virtual functions. I get the syntax and all, but I just don't see a point of using them. Can someone please give me an example where virtual functions would be applied?

Thanks for your help,
EpicAsian
User avatar
Bullet Pulse
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 89
Joined: Sun Feb 21, 2010 6:25 pm

Re: C++ Virtual Function Confusion

Post by Bullet Pulse »

The point of using them is so that you can refer to subclass objects through a base class reference and have subclasses' functions have different meanings.
They are useful when you want to design objects that are related to each other.
Ultimately, it allows you to implement a function in different ways.
With virtual functions used, you make a pointer to a base class, and then you assign it to a subclass.

For example:
You might want to draw custom shapes to the screen in your API.
You might have a class named Shape, with virtual functions, and subclasses named Triangle, Rectangle, Circle, Rhombus, et cetera.
These subclasses will have to override the Draw() function defined in the Shape class, so they are drawn to the screen properly.

Now when you want to make a bunch of these shape objects, you can group them together into a vector of pointers to Shape objects.

Code: Select all

vector<Shape*> shapes;
You could then add any type of shape object to that vector like this:

Code: Select all

shapes.push_back(new Triangle());
Now you can call on all of the Shape objects in the vector like this:

Code: Select all

for (Uint32 i = 0; i < shapes.size(); i++)
        shapes[i]->Draw();

So you might be wondering what's the difference between polymorphism and inheritance.
As Wikipedia says: "When a derived object is referred to as being of the base's type, the desired function call behavior is ambiguous."

This basically says that if you don't use virtual functions and try to do the above, the function will just call on the base class's function.
Last edited by Bullet Pulse on Wed Apr 21, 2010 7:55 pm, edited 2 times in total.
Image
User avatar
epicasian
Chaos Rift Junior
Chaos Rift Junior
Posts: 232
Joined: Mon Feb 22, 2010 10:32 pm
Current Project: Gigazilla Engine
Favorite Gaming Platforms: Dreamcast, SNES, PS2, PC
Programming Language of Choice: C/++
Location: WoFo, KY

Re: C++ Virtual Function Confusion

Post by epicasian »

So, this would be a good example of using virtual functions?

Code: Select all

//virtual functions 
#include <iostream>

using namespace std;

class shape
{
      public:
             shape() {cout << "shape constructor\n";}
             virtual ~shape() {cout << "shape destructor\n";}
             virtual int draw() {cout << "imma shape!\n";}

};

class triangle: public shape
{
      public:
             triangle() {cout << "triangle constructor\n";}
             virtual ~triangle() {cout << "triangle destructor\n";}
             int draw() {cout << "imma triangle!\n";}
             
};

class square: public shape
{
      public:
             square() {cout << "square constructor\n";}
             virtual ~square() {cout << "square destructor\n";}
             int draw() {cout << "imma square!\n";}
      
};


int main(int argc, char args[])
{
    shape *pTriangle = new triangle;
    cout << endl;
    shape *pSquare = new square;
    cout << endl;
    shape *pShape = new shape;
    cout << endl;
    pTriangle->draw();
    pSquare->draw();
    pShape->draw();
    delete pShape;
    delete pSquare;
    delete pTriangle;
    
    cin.get();
}
output:

Code: Select all

shape constructor
triangle constructor

shape constructor
square constructor

shape constructor

imma triangle!
imma square!
imma shape!
shape destructor
square destructor
shape destructor
triangle destructor
shape destructor
Thanks again for your help,
EpicAsian
User avatar
zeid
Chaos Rift Junior
Chaos Rift Junior
Posts: 201
Joined: Fri Apr 24, 2009 11:58 pm

Re: C++ Virtual Function Confusion

Post by zeid »

Yup. A derived/inherited class has access to the protected and public functions of its base/parent class. It can use them as if they were it's own. However what if some of the methods don't really fit for the derived class.

In the example bellow most dogs bark, however a dingo doesn't often bark it howls most of the time. So we override the speak function using a virtual function.

Code: Select all

#include<iostream>
#include<conio.h>

using namespace std;

class Dog
{
public:
 	virtual void speak()
 	{
 	 	cout<<"Bark.";
 	}
};

class Pitbull : public Dog
{};

class Dingo : public Dog
{
public:
 	void speak()
 	{
 	 	cout<<"Howl.";
 	}
};

int main()
{
	Dog dog;
	dog.speak();
	dog.jump();

	Pitbull pitbull;
	pitbull.speak();

	Dingo dingo;
	dingo.speak();

	getch();
};
The pitbull doesn't need it's speak function overridden as it should bark just like most other dogs.
Axolotl Pop!
Image
Or, try it for free.

For many more free games online visit www.sam-zeid.com
Post Reply