One, virtual function
In the same class, it is not possible to define two functions with the same name, number of arguments, and the same type, or "duplicate definition". However, in a class's inheritance hierarchy, functions that have the same name, the same number of arguments, and different types can appear in different hierarchies. The function of virtual functions is to let us call functions with the same name in the base class and derived classes.
Instead of calling functions of the same name in different derived hierarchies through different object names in the program, they are called by pointers.
As an example:
If we define a base class shape
class shape{ public: void area ();
Shape () =default;
Shape (int x,int y,int z): Wid (x), Len (y), Rad (z) {}
~shape () =default; Private : int Wid,len,rad;}
Where area functions are used to calculate areas, such as rectangles and circles, then we need to define two derived class Circle,rectangle. However, because the area function in the base class needs to call three private elements in shape. cannot be called in a derived class, each time it is called, it is assigned another value in the derived class.
However, if you define an area as a virtual function, you can overload it in each derived class and customize its functionality.
classshape{ Public: Virtual DoubleArea () =0;//define abstract class and virtual funtionShape () {wid = Len = rad =0; } Shape (intAintBintc): Wid (a), Len (b), Rad (c) {}~shape () =default;protected: intwid, Len, Rad;};
class Rectangle: public shape{public: 0;} Rectangle (intint b): Wid (a), Len (b) {} virtualdouble return wid* len; protected : int wid, Len, Rad;};
class Circle: public shape{ public: 0;} Circle (int a): Rad (a) {} virtualdoublereturn ( double) Pi*rad*rad;} protected : int Rad;};
...
Rectangle A (WID, Len); *p = &A; // "The area was :" <<p-> area(); Circle C (RAD); *p = &C; " The Square is: " << P->area ();
Second, abstract class
What is an abstract class? The simple understanding of the individual is that it contains pure virtual functions in the class.
For example, in the example above, a pure virtual function is defined in class shape: Virtual double area () = 0;
Summarize:
- A base class is an abstract base class if it contains one or more pure virtual functions. Abstract base classes cannot and do not have to define objects.
- The abstract base class differs from the ordinary base class in that it is generally not an abstraction of a real object (such as circle (circle) is the abstraction of thousands of actual circles), which can have no physical or other practical meaning.
- In the hierarchy of classes, the top or topmost layers can be abstract base classes. Abstract base class embodies the generality of the various types in this class, and the member functions of all kinds of CCP are declared in the abstract base class.
- The abstract base class is the public interface for this class of families. Alternatively, multiple classes derived from the same base class have the same interface.
- Distinguishes between static and dynamic associations. If you call a virtual function by object name (such as P.area ()), you can determine which class's virtual function is called at compile time, so it is a static association. If a virtual function is called through a base-class pointer (such as P->area ()), it is not possible to determine from the statement itself the virtual function of which class to call from the statements themselves, but only at run time, when PT points to a class of objects, it is possible to determine the virtual function of the type being called and therefore is dynamically associated.
- If a virtual function is declared in a base class, all functions in the derived class that have the same function name, function type, number of arguments, and type are virtual functions (whether or not in a derived class are declared with virtual).
- The extensibility of the program is improved by using virtual function. Separates the declaration of a class from the use of a class. This is especially important for software developers who are designing class libraries.
Beginner C + + virtual function and abstract class