class Base{ public: void counter(); .... } class Dervied: public Base{ public: .... } void main() { Base *ptr=new Derived; ptr->counter(); }
To identify that the base class pointer is pointing to derived class and using a derived member function, we make use of "virtual".
Similarly, can we make derived data members "virtual"? (the data member is public)
@andre: Just an idea: Virtual data members could be used for mixins or duck typing. When 2 classes A and B both define a virtual int count , then a derived class which inherits A and B could carry only a single (as it is virtual) count member.
A data member may be of any type, including classes already defined, pointers to objects of any type, or even references to objects of any type. Data members may be private or public, but are usually held private so that values may only be changed at the discretion of the class function members.
Virtual member functions are declared with the keyword virtual . They allow dynamic binding of member functions. Because all virtual functions must be member functions, virtual member functions are simply called virtual functions.
They are always defined in the base class and overridden in a derived class. It is not mandatory for the derived class to override (or re-define the virtual function), in that case, the base class version of the function is used. A class may have virtual destructor but it cannot have a virtual constructor.
virtual
is a Function specifier...
From standard docs,
7.1.2 Function specifiers Function-specifiers can be used only in function declarations. function-specifier: inline virtual explicit
So there is nothing called Virtual data member.
Hope it helps...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With