I am a little acquainted with C++, however the virtual keyword still atmosphere me. Just what will it mean? If your function is understood to be virtual, would be that the just like pure virtual?
"An online function or virtual technique is a function or method whose behavior could be overridden inside an getting class with a function with similar signature" - wikipedia
"A pure virtual function or pure virtual technique is an online function that's needed to become implemented with a derived class that's not abstract" - Wikipedia
So, the virtual function could be overriden and also the pure virtual should be implemented.
Also, a great link for questions such as this is http://world wide web.parashift.com/c++-faq-lite/
Within an C++ class, virtual may be the keyword that designates that the method could be overridden (i.e. implemented by) a subclass. For instance:
class Shape title void setName( const std::string&lifier title ) // not overridable title = title protected: virtual void initShape() // overridable private: std::string m_title
Within this situation a subclass can override the the initShape function to complete some specialized work:
class Square : public Shape
The word pure virtual describes virtual functions that should be implemented with a subclass and haven't been implemented through the base class. You designate a technique as pure virtual using the virtual keyword and adding a = in the finish from the method declaration.
So, should you desired to make Shape::initShape pure virtual you'd perform the following:
With the addition of a pure virtual approach to your class you are making the category an abstract base class that is very handy for separating connects from implementation.
The virtual keyword gives C++ its' capability to support polymorphism. If you have a pointer for an object of some class for example:
class Animal class Duck : public Animal class Equine : public Animal void SomeFunction(Animal * pAnimal)
Within this (silly) example, the GetNumberOfLegs() function returns the right number in line with the class from the object that it's known as for.
Now, think about the function 'SomeFunction'. It does not care which kind of animal object is passed into it, as lengthy because it is based on CAnimal. The compiler will automagically cast any CAnimal-derived class to some CAnimal because it is basics class.
As we do that:
Duck d SomeFunction(&d)
it'd output '2'. As we do that:
Equine h SomeFunction(&h)
it'd output '4'. We can not do that:
Animal a SomeFunction(&a)
since it will not compile because of the GetNumberOfLegs() virtual function being pure, meaning it should be implemented by drawing classes (subclasses).
Pure Virtual Functions are mainly accustomed to define:
a) abstract classes
They are base classes where you need to be a consequence of them after which implement the pure virtual functions.
They are 'empty' classes where all functions are pure virtual and therefore you need to derive after which implement all the functions.