1 Answers
Virtual Functions In Cpp
- The virtual functions must be members of some classes.
- They cannot be static members.
- They are accessed by using object pointers.
- A virtual function can be a friend of another class.
- A virtual function in a base class must be defined, even though it may not be used.
- The prototypes of the base class version of a virtual functions and all the derived class versions must be identical if two functions with the same name have different prototypes C++ considers them as overloaded functions, and the virtual function mechanism is ignored.
- We cannot have virtual constructors, but we can have virtual destructors.
- While a base pointer can point to any type of the derived object the reverse is not true that is to say we cannot use a pointer to a derived class to access an object of the base type.
- When a base pointer points to a derived class incrementing or decrementing it will not make it to point to the next object of the derived class .it is incremented or decremented only relative to its base type, therefore, we should not use this method to move the pointer to the next object.
- If a virtual function is defined in the base class, it need not be necessarily redefined in the derived class.in such cases, a call will invoke the base function.