GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 30-Nov-2006, 09:46
Mofix Mofix is offline
New Member
 
Join Date: Oct 2006
Posts: 12
Mofix is on a distinguished road

Difference between Virtual and Overloaded


Virtual functions are like this:

CPP / C++ / C Code:
   class Window
     {
       public:
          virtual void Create()
          {
               cout <<"Base class Window"<<endl;
          }
     };
     class CommandButton : public Window
     {
       public:
          void Create()
          {
              cout<<"Derived class Command Button - Overridden C++ virtual function"<<endl;
          }
     };

     int main()
     {
         Window  *x, *y;
  
         x = new Window();
         x->Create();

         y = new CommandButton();
         y->Create();
     }

You call from pointer Window new CommandButton and Window. This is OK.
But what makes different this from this class:

CPP / C++ / C Code:
class base_class 
{
	public:		
    			(virtual) void printit() {
       						cout << "Print from base class\n";
       			} 
};

class first_class : public base_class  
{
	public:
    			void printit() {
				cout << "Print from first class\n";	
			} 
}; 
int main() 
{
base_class base;
first_class first1;

base.printit();	
first1.printit();	
}

If I write with or without virtual in base class ( virtual void printiti() ) it still does the job.
It writes when in code first1.printit(); " Print form first class " . So what is different if I add virtual or leave it without virtual . In that case does it go to overloaded. But It can't because the have the same type and arguments.
Last edited by LuciWiz : 01-Dec-2006 at 08:21. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 30-Nov-2006, 16:18
trickhat trickhat is offline
New Member
 
Join Date: Nov 2006
Posts: 7
trickhat is on a distinguished road

Re: Difference between Virtual and Overloaded


Overloaded:
The base class contains the default function to be called. If any derived classes have the same function, with the same return type and args, then it is overloaded and the program uses that function as the exception. (Other derived classes that don't contain the overloaded function will use the func of the base-class).

Virtual:
The base class contains a vague format of a function that MUST be overloaded in every derived class. A derived class that does not overload the virtual function of the base class should cause some complaining from the compiler...
  #3  
Old 01-Dec-2006, 10:35
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 1,032
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough

Re: Difference between Virtual and Overloaded


Quote:
Originally Posted by trickhat
Overloaded:
Virtual:
The base class contains a vague format of a function that MUST be overloaded in every derived class. A derived class that does not overload the virtual function of the base class should cause some complaining from the compiler...

That is true for pure virtual functions, but not for plain virtual ones:

CPP / C++ / C Code:
virtual void f() = 0; // pure virtual
virtual void g();     // plain virtual

Plain virtual functions in base classes don't have to be implemented in the derived classes, but they can be.

Quote:
Originally Posted by Mofix
So what is different if I add virtual or leave it without virtual . In that case does it go to overloaded. But It can't because the have the same type and arguments.

In your example it doesn't really make a difference.
Virtual functions are useful when implementing polymorphism
for hierarchies of classes.

For instance, building on your example, let's say you have one more class:

CPP / C++ / C Code:
     class ListBox : public Window
     {
       public:
          void Create()
          {
              cout<<"Derived class ListBox - Overridden C++ virtual function"<<endl;
          }
     };

And you have a function that will call your create for any objects of type Window or derived from it:

CPP / C++ / C Code:
void DoMakeObject(Window * const pWindow)
{
    pWindow->Create();
}

You could use this function on any of the 2 types CommadButton and ListBox and it will yield the expected results.
If you simply override the function in the derived classes without using the virtual functionality, you will have to build 2 functions DoMakeCommandButton and DoMakeListBox and add some checks to see which of them you need to call. I think it is obvious the polymorphic approach is preferable for considerably more complicated hierarchies of classes.

Best regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:43.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.