Code About Us Tutorial


  Inheritance
    What is Inheritance?
    Creating a Derived Class
    Creating an Object
    Simple Inheritance
    Overloading Derived Classes
    Dominating Member Functions
    Polymorphism & Virtual Functions


   
 

Home > Inheritance > Polymorphism & Virtual Functions

December 1, 2009 11:59 am

   

Polymorphism & Virtual Functions

In this next section we will discuss more advanced topics, such as polymorphism and virtual functions. C++ extends its polymorphism to allow pointers to base classes to be assigned to derived class objects. So you could write Business* pEmployee = new Employee; This creates a new Employee object on the heap and returns a pointer to that object, which it assigns to a pointer to Business. This is fine, because an Employee is part of a Business. Virtual functions allow you to call the correct method without having to dominate a class method. Here is an example of what I’m talking about.

 

 
#include <iostream.h>
#include "tq.h"

int main() { X *pY = new Y;

pY->Eat(); pY->Speak();

delete pY; pY = 0;

return 0; }

In this file, I created a Pointer to X, to create a new object Y. Then I called two methods, Eat and Speak. After the objects where done, I used the delete keyword to free the memory stored on freestore. It is proper programming practice to always delete you’re pointers, and always set them to Null or 0 after they are deleted.

Here is the .h file with the classes.

 

 
//tq.h

class X { public: X(): itsVal(3) { cout <<"Base Constructor calledn"; } ~X() { cout <<"Base Destructor calledn";}

int getVal() const {return itsVal;} void setVal(int x) { itsVal = x;}

virtual void Eat() const { cout <<"Mammal ate one mealn";} virtual void Speak() const { cout <<"Mammal Noisen";}

protected: int itsVal; };

class Y: public X { public: Y() {cout <<"Derived Constructor calledn";} ~Y() { cout <<"Dervied Destructor calledn";} void Speak() const { cout <<"Bark,Barkn"; } void Eat() const { cout << "Dog ate 5 mealsn";} };

The most important observation to be made after viewing this file are the two lines where it says

 

 
virtual void Eat()
virtual void Speak().

These are where the virtual functions are put to work. Now when you run this program, and you use the above code, you would get the following output:

Base Constructor called Derived Constructor called Dog ate 5 meals Woof Base Destructor called

If you take off the keyword virtual from one of the base class functions, you will not end up with the result that you wanted. You might have wanted the Dog Methods, but instead you ended up with one of the Mammal methods instead. This is the power of Virtual Functions. It knows which object you want to access. Please Rate this Code:


    Comments for: Polymorphism & Virtual Functions
Annonymouse User says:
Why do have to do C++? just to get a good job? use up 50 hours to make a file that reads in a list of numbers and displays it backwards, WOW and binary trees wots the point , the amount of time it takes to write the stupid coding for it, u can figure out the answer urself.......... Useless i tells yeeee uselessssss

and dont even get me started on linking files, u link 5 files together,so tht u can use this crappy inheritance,but wots the point wen ur program will hardly eva work cos u neva got the pointers right they jus keep creating errors and more errors,why dont u jus stick the whole thing in one damn file

S.R.O

cheesehead [someone@somewhere.com] Posted: 4 times. says:
You must be _THE_ most misinformed person I've ever seen use the internet...

cheesehead [someone@somewhere.com] Posted: 4 times. says:
You must be _THE_ most misinformed person I've ever seen use the internet... says:
ur dum its wurth the 50 hours

Add [mad__add@hotmail.com] Posted: 2 times. says:
\n

cheesehead - yeah, anyone that agrees with that wants to write a large project... welcome to the real world...

pointers are incredibly efficient, over passing entire objects, and as for inheritence.... it's just so damn useful! it saves the re-writing of a *lot* of code.

If you want to critisise learn to be constructive... and while you're at it, learn to spell... 'txt spk' impresses no-one.

Add, UK





Add Comment:
Name:
Email:


 «1» THINKQUEST TEAM C0111571 © 2001. All Rights Reserved.