• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
wayneprim

Why even use virtual functions in a Parent class?

10 posts in this topic

So I am learning about inheritence and currently I am on polymorphism... I got the whole inheriting functions from the parent class in order to not duplicate code however I am stuck on one thing: The virtual function...

So say I have a class named Automobile like so with a child class that is named Ferrari:

[source lang="cpp"]
class Automobile
{
public:
virtual void drivespeed();
};

class Ferrari: public Automobile
{
public:
Ferrari(string name);
void drivespeed();
private:
string mName;
};
[/source]

[source lang="cpp"]//implementation of classes
void Automobile::drivespeed()
{
//does nothing
cout << "UNDEFINED" << endl;
}

Ferrari::Ferrari(string name)
:mName(name)
{
}

void Ferrari::drivespeed()
{
//drives fast
cout << "DRIVES REALLY FAST" << endl;
}
[/source]



now in the main.cpp file in the main function i could write like so...

[source lang="cpp"]
int main(int argc, char **argv)
{
Automobile *auto = new Ferrari("Spider");
//invoke drivespeed function
auto->drivespeed();


return 0;
}
[/source]

all this does is creates an object of the ferrari type (upcasting it to the automobile type) and then lets me invoke methods of that class... So here is my question:

What difference does the virtual function declaration in the Automobile class make? Couldn't I skip the declaration in automobile function since it is really not even a function (it does nothing) and just declare the function as normal in the Ferrari class? and if i made more child classes of the parent class Automobile, i could do the same?

Please let me know

Thanks,

Wayne Prim Edited by wayneprim
0

Share this post


Link to post
Share on other sites
hopefully this isn't so to everyone but in the second block of code it seems to be cut off... ill include it here:

//implementation of classes
void Automobile::drivespeed()
{
//does nothing
cout << "UNDEFINED" << endl;
}
Ferrari::Ferrari(string name)
:mName(name)
{
}
void Ferrari::drivespeed()
{
//drives fast
cout << "DRIVES REALLY FAST" << endl;
}
0

Share this post


Link to post
Share on other sites
This does help, thanks!! I just feel like making that virtual declaration in Automobile is so useless still, because if I wanted I could just erase it, and then in the main.cpp just call

Ferrari auto("NAME");
auto.drivespeed();

maybe ill get it more when there are more complex functions but it just seems like a useless addition... lol sorry if I am not getting this... the answer is probably very simple and will be something like, "Ohhhh, wow I see it now.." :P
0

Share this post


Link to post
Share on other sites
Imagine this is not about a function [font=courier new,courier,monospace]drive_speed [/font]but about a function [font=courier new,courier,monospace]ignition_on[/font]. Every car needs to call [font=courier new,courier,monospace]ignition_on() [/font]before it can drive, and it does the same thing. Why write the code for every type of car if you can have it in the parent class?

Oh, and then there's electric cars which don't have ignition... this is where you override it to do nothing. In the future, there may be cars that are propelled by blowing up a big explosive charge on the rear. In this case, [font=courier new,courier,monospace]ignition_on [/font]will be overloaded to trigger the explosive. Edited by samoth
1

Share this post


Link to post
Share on other sites
samoth, your example of electric cars is a typical case of interface bloat. If not all cars need ignition_on, don't put it into the base class. But it's okay as an explaination of virtual functions, I guess ;-) Edited by rnlf
0

Share this post


Link to post
Share on other sites
They have a "power on" button, but ignition connotes lighting something on fire (igniting), i.e. sparking the fuel in the cylinders.
Electric cars just have an on switch.

OP: another example: let's say you have an object Garage that keeps an array of Automobile pointers (Automobile* cars[20]). You can go through and new those pointers into any kind of class that derives from automobile. But with the virtual function drivespeed() in the base Automobile class, you could loop through the entire array and call

[code]
for(int i = 0; i < 20; i++)
{
cars[i]->drivespeed();
}
[/code]

without having to know what kind of subclass they even were, and knowing full well that each subclass instance would run their version of the base method appropriately.
1

Share this post


Link to post
Share on other sites
[quote name='Rattenhirn' timestamp='1346829155' post='4976726']
[quote name='wayneprim' timestamp='1346827465' post='4976722']
What difference does the virtual function declaration in the Automobile class make? Couldn't I skip the declaration in automobile function since it is really not even a function (it does nothing) and just declare the function as normal in the Ferrari class? and if i made more child classes of the parent class Automobile, i could do the same?
[/quote]

Well, you could've just tried it out!

Anyway, here's the solution:
If you do not have a "drivespeed" function in "Automobile", your main would not compile.
If you'd make it not virtual, then your main would call the "drivespeed" function of the "Automobile" class and not of "Ferrari".
And that's exactly what polymorphism is. You can use a pointer to the base class to call a virtual function and it will automatically call the function implementation that matches the actual class.

Additionally, you state that the "drivespeed" function doesn't really make sense, because the speed of an "Automobile" is unknown. This can also be expressed in C++ by making the function "pure virtual".
[/quote]

Perfect explanation. Thanks a lot
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0