class Base
{
public:
virtual void sound() { cout << "Bass" << endl; }
};
class Derived : public Base
{
public:
void sound() { cout << "Guitar" << endl; }
};
Is it correct, that virtual functions only make sense if there
is a derived class?
And also only if there is a function with the same name in the derived class?
Is there any application, if there is no derived class or the sound function has different names?
Virtual functions are used in Object Oriented Programming and implement methods that can be overridden.
I do not understand what you mean. Could you please explain what you mean?
I think what kbw means is that you may create a virtual function for a class even if there is no current derived class. You might make it virtual so that someone can override it at a future time with a derived class.
Overloaded virtual functions are disambiguated using the same rules as other functions. In other words, it isn't just the name that matters, it's the name and the arguments. You can have
Which one gets called depends on the arguments you pass. The exact rules for this are complex because there are whacky cases and it must be certain which function should get called. If it isn't pretty obvious for a given call, I recommend adding a comment so anyone reading your code can easily tell which one is called.
Additionally, C++ supports procedural programming. This typically uses Abstract Data Types. Procedural programming does not use virtual functions (or inheritance).
In addition to virtual functions, Object Oriented Programming in C++ must use indirection. That is, you access objects thru pointers (or references).
In C++ there are also pure virtual functions which actually force derived classes to implement the member function. When the class has pure virtual member function it is called abstract class and you can't create an instance of it.
#include <iostream>
using std::cout;
class Instrument {
public:
virtualvoid sound() = 0; // this makes Instrument an abstract class
};
class Guitar: public Instrument {
public:
void sound() { cout << "Guitar sound\n"; }
};
int main() {
//Instrument instrument; - error: cannot declare variable 'instrument' to be of abstract type 'Instrument'
Guitar guitar;
guitar.sound();
//We can also use guitar as an instrument that's why we use virtual function in the first place right?
Instrument* instrument;
instrument = &guitar;
instrument->sound();
return 0;
}
If the Guitar class didn't implement sound() function you would not be able to create an instance of it. I hope it will brighten things up a bit.
Virtual functions are member functions whose behavior can be overridden in derived classes. As opposed to non-virtual functions, the overridden behavior is preserved even if there is no compile-time information about the actual type of the class. If a derived class is handled using pointer or reference to the base class, a call to an overridden virtual function would invoke the behavior defined in the derived class. This behavior is suppressed if the function is selected using qualified name lookup (that is, if the function's name appears to the right of the scope resolution operator ::)
Virtual functions come of handy when you have to deal with polymorphism.