![]() |
|
#1
|
|||
|
|||
Difference between Virtual and OverloadedVirtual functions are like this:
CPP / C++ / C Code:
You call from pointer Window new CommandButton and Window. This is OK. But what makes different this from this class: CPP / C++ / C Code:
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
|
|||
|
|||
Re: Difference between Virtual and OverloadedOverloaded:
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
|
||||
|
||||
Re: Difference between Virtual and OverloadedQuote:
That is true for pure virtual functions, but not for plain virtual ones: CPP / C++ / C Code:
Plain virtual functions in base classes don't have to be implemented in the derived classes, but they can be. Quote:
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:
And you have a function that will call your create for any objects of type Window or derived from it: CPP / C++ / C Code:
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 GIDBlog
Problems with the Navy (Chiefs) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The