Abstract classes are usually defined by defining pure virtual functions in this class.
It is too common to use a private Type constructor. It is not an abstract class. Any desired object must be created in the heap.
Abstract classes are mainly used for interfaces. They can also be understood as a communication and interaction method of classes. The concept of interfaces is widely used in advanced applications such as COM. This is also the second threshold for C ++.
Class interfacehavefun
{
Required Ural havefun () = 0;
}
Interfacehavefun is a pure signed Ural class, and you cannot generate its objects. However, its pointer can point to its subclass.
That is to say, this is wrong.
Interfacehavefun TMP; // definition Error
Interfacehavefun * TMP = new interfacehavefun (); // definition Error
Class A: Public interfacehavefun
{
Havefun () // unknown Ural is not defined. It is also a signed Ural function.
{
STD: cout <"A have fun" <STD: Endl;
};
}
Class B: Public interfacehavefun
{
Havefun ()
{
STD: cout <"B have fun" <STD: Endl;
};
}
You can use the following interface:
Interfacehavefun * TMP = new ()
TMP-> havefun ()
Result:
A have fun
Interfacehavefun * TMP = new B ();
TMP-> havefun ();
B have fun
Don't underestimate this method. You can use it to write beautiful programs.