After "= 0" is added to the parameter list declared by the virtual function, the function becomes a pure virtual function.
Class base
{
Virtual void function () = 0;
}
We do not need to provide any definition for the pure virtual function base: function (). classes that declare pure virtual functions are abstract classes. Any attempt to create an abstract class object will cause a compiler error.
If a base is derived and the base: function () function is rewritten, it becomes a specific class.
Class derived: Public basae
{
Void function ();
}
An abstract class is usually used as an interface declaration. In this case, you do not need to declare a complete implementation for this interface. This interface describes the operations that all objects derived from this class should support, the derived class must implement these abstract operations.
Class base
{
Public:
Virtual void print () = 0;
};
Class derived: public Base
{
Public:
Virtual void print ()
{
Cout <"derived: derived () \ n ";
}
~ Derived ()
{
Print ();
}
};
Int main ()
{
Derived D;
Base * PTR = & D;
D. Print ();
}
Running result:
Derived: derived ()
Derived: derived ()
The first derived: derived () is the output generated when print () is called directly through D, and the second is called at the end of the program ~ Output generated when derived () is used.
Although we cannot directly create abstract class objects, we can still use pointers and references pointing to base.
For example, base * PTR = & D;