Lost Password?

  #1 (permalink)  
Old 03-17-2008, 11:03 PM
Chinmoy's Avatar   
Chinmoy Chinmoy is offline
Programming Professional
 
Join Date: Feb 2008
Location: where heaven meets earth
Posts: 301
Rep Power: 6
Chinmoy has a spectacular aura aboutChinmoy has a spectacular aura about
Default virtual functions

here i have discussed virtual functions using easy to understand examples.

A virtual function is a member function of a class, whose functionality can be over-ridden in its derived classes. It is one that is declared as virtual in the base class using the virtual keyword. The virtual nature is inherited in the subsequent derived classes.

The difference between a non-virtual member function and a virtual member function is, the non-virtual member functions are resolved at compile time whereas virtual members are resolved at runtime.

in this code we have the implementation of virtual functions and their use in c++.

Code:
#include <iostream.h>
class base
{
public:
virtual void display()
{
  cout<<”\nBase”;
}
};
class derived : public base
{
  public:
  void display()
  {
     cout<<”\nDerived”;
  }
};

void main()
{

  base *ptr = new derived();
  ptr->display();
}
In the above example, the pointer is of type base but it points to the derived class object. The method display() is virtual in nature. Hence in order to resolve the virtual method call, the context of the pointer is considered, i.e., the display method of the derived class is called and not that of the base. If the method was non virtual in nature, the display() method of the base class would have been called.

Virtual methods should be used judiciously as they are slow due to the overhead involved in searching the virtual table. They also increase the size of an object of a class by the size of a pointer.

Last edited by Chinmoy; 03-18-2008 at 06:58 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Virtual Memory ... Windows XP mindsurfer Computer Hardware 1 11-01-2007 06:38 PM
Console API Functions dargueta C and C++ 14 10-20-2007 03:38 PM
Virtual Functions ???? Patrick C and C++ 1 10-07-2007 09:54 AM
Get and Set Functions Chan C# Programming 3 08-25-2006 11:44 AM
C# calling parent functions from child form NeedHelp C# Programming 8 08-10-2006 11:53 AM


All times are GMT -5. The time now is 01:42 AM.

Contest Stats

John ........ 223.00000
dargueta ........ 168.00000
Xav ........ 164.00000
LogicKills ........ 20.00000
gaylo565 ........ 18.00000
WingedPanther ........ 15.00000
|pH| ........ 15.00000
Johnnyboy ........ 3.00000
navghost ........ 1.00000

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 67%

Ads