Sign in to follow this  
MadMax1992

Virtual functions, #defines and connecting .cpp and .h

Recommended Posts

I'm looking over somebody else's code in a book. It's a DirectX program consisting out of 3 important files: - SomeHeader.h - SomeHeader.cpp - MainFile.cpp Question 1: The file SomeHeader.h begins with:
#ifndef SOMEHEADER_H
#define SOMEHEADER_H
and ends with:
#endif
What does these commands mean, and what do they do? Question 2: The file SomeHeader.h declares several virtual functions (defined inside the public part of a class), I've never encountered a virtual function before. What is a virtual function? and how does it differ from a normal function? Question 3: Furthermore: The file SomeHeader.cpp includes the file SomeHeader.h at the beginning of the file, and starts with writing what all of the functions defined in Someheader.h do. In the MainFile.cpp the file SomeHeader.h is defined, however, the file SomeHeader.cpp isn't. So how is the file SomeHeader.cpp still included in the main file? I hope my knowledge of the english grammar isn't making it impossible for you guys to read this :). I have searched on the forums for an answer to my questions, however, the answer wasn't visible in the first 3 pages. Thanks in advance! Max Henger

Share this post


Link to post
Share on other sites
Virtual functions can be a bit confusing at first if you dont understand inheritance.

They are essentially used for polymorphism, again another scary word if you dont know what it means. Also you need to understand pointers.

I was attempting to explain all of these, but really your best bet is lots and lots and lots of tutorials.

Assuming you do understand these I will skip right to the meat of virtual functions.

Basically when you have a pointer to an object which is inherited from another class and you want to call an overridden function (say Update() ) the 'type' of the pointer will define which function is called.

For example lets assume we have two classes cBaseObject and cTriangle (which is derived from cBaseObject )

cBaseObject has an Update() function which prints "BaseObject" on the screen and cTriangle has an overridden Update() function which prints "Triangle" on the screen.

With non-virtual functions the 'type' of the pointer will define which Update() is called.

e.g

//BaseObject pointer type
cBaseObject * lpObject = new cTriangle;
lpObject->Update();
//THIS WILL PRINT "BaseObject"

conversly..
//Triangle pointer type
cTriangle * lpObject = new cTriangle;
lpObject->Update();
//THIS WILL PRINT "Triangle"


Now if we make the Update() function virtual, instead of calling the update based on the pointers type it will always call the OBJECTS class Update() function.

eg...
//BaseObject pointer type
cBaseObject * lpObject = new cTriangle;
lpObject->Update();
//THIS WILL PRINT "Triangle"

conversly..
//Triangle pointer type
cTriangle * lpObject = new cTriangle;
lpObject->Update();
//THIS WILL PRINT "Triangle"


Again, im sure you can find better explanations of this on the web, but I hope that helps!

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