<Span style = "font-size: 24px;">/* this instance is used to rewrite a typical example. Use virtual functions and abstract base classes in programs. The hierarchy of classes is abstract base class Shape (Shape ). Point, Circle, and Cylinder are both direct and indirect Derived classes of the Shape class */
// The following is a complete program. For ease of reading, some text descriptions are inserted in segments.
The procedure is as follows:
Part (1)
# Include <iostream>
Using namespace std;
// Declare the Shape of the abstract base class
Class Shape
{
Public:
Virtual float area () const {return 0.0;} // virtual function
Virtual float volume () const {return 0.0;} // virtual function
Virtual void shapeName () const = 0; // pure virtual function
};
Part (2)
// Declare the Point class
Class Point: public Shape // Point is the public derived class of Shape.
{
Public:
Point (float = 0, float = 0 );
Void setPoint (float, float); float getX () const {return x;} float getY () const {return y ;}
Virtual void shapeName () const {cout <"Point:" ;}// redefine the virtual function
Friend ostream & operator <(ostream &, const Point &);
Protected:
Float x, y;
};
// Define a Point-class member function
Point: Point (float a, float B)
{X = a; y = B ;}
Void Point: setPoint (float a, float B)
{X = a; y = B ;}
Ostream & operator <(ostream & output, const Point & p)
{Output <"[" <p. x <"," <p. y <"]";
Return output;
}
Part (3)
// Declare the Circle class
Class Circle: public Point
{
Public:
Circle (float x = 0, float y = 0, float r = 0 );
Void setRadius (float );
Float getRadius () const;
Virtual float area () const;
Virtual void shapeName () const {cout <"Circle:" ;}// redefinition of virtual functions
Friend ostream & operator <(ostream &, const Circle &);
Protected:
Float radius;
};
// Declare a member function of the Circle class
Circle: Circle (float a, float B, float r): Point (a, B), radius (r ){}
Void Circle: setRadius (float r): radius (r ){}
Float Circle: getRadius () const {return radius ;}
Float Circle: area () const {return 3.14159 * radius ;}
Ostream & operator <(ostream & output, const Circle & c) {output <"[" <c. x <"," <c. y <"], r =" <c. radius;
Return output ;}
Part (4)
// Declare the Cylinder class
Class Cylinder: public Circle
{
Public:
Cylinder (float x = 0, float y = 0, float r = 0, float h = 0 );
Void setHeight (float );
Virtual float area () const;
Virtual float volume () const;
Virtual void shapeName () const {cout <"Cylinder:" ;}// redefinition of virtual functions
Friend ostream & operator <(ostream &, const Cylinder &);
Protected:
Float height;
};
// Define the Cylinder class member function
Cylinder: Cylinder (float a, float B, float r, float h): Circle (a, B, r), height (h ){}
Void Cylinder: setHeight (float h) {height = h ;}
Float Cylinder: area () const {return 2 * Circle: area () + 2*3.14159 * radius * height ;}
Float Cylinder: volume () const {return Circle: area () * height ;}
Ostream & operator <(ostream & output, const Cylinder & cy)
{Output <"[" <cy. x <"," <cy. y <"], r =" <cy. radius <", h =" <cy. height; return output ;}
Part (5)
// Main Function
Int main ()
{
Point point (3.2, 4.5); // create a Point-Class Object
Circle circle (2.4, 1.2, 5.6 );
// Create a Circle object
Cylinder cylinder (3.5, 6.4, 5.2, 10.5 );
// Create Cylinder Class Object cylinder
Point. shapeName ();
// Static Association
Cout <point <endl;
Circle. shapeName (); // static Association
Cout <circle <endl;
Cylinder. shapeName (); // static Association
Cout <cylinder <endl;
Shape * pt; // defines the base class pointer
Pt = & point; // pointer to Point Class Object
Pt-> shapeName (); // Dynamic Association
Cout <"x =" <point. getX () <", y =" <point. getY () <"\ narea =" <pt-> area ()
<"\ Nvolume =" <pt-> volume () <"\ n ″;
Pt = & circle; // pointer to a Circle Class Object
Pt-> shapeName (); // Dynamic Association
Cout <"x =" <circle. getX () <", y =" <circle. getY () <"\ narea =" <pt-> area ()
<"\ Nvolume =" <pt-> volume () <"\ n ″;
Pt = & cylinder; // pointer to Cylinder Class Object
Pt-> shapeName (); // Dynamic Association
Cout <"x =" <cylinder. getX () <", y =" <cylinder. getY () <"\ narea =" <pt-> area ()
<"\ Nvolume =" <pt-> volume () <"\ n ″;
Return 0 ;}
The program running result is as follows.
Please analyze it against the program.
Point: [3.2, 4.5] (Point Object point data: Coordinate of a Point)
Circle: [2.4, 1.2], r = 5.6 (data of Circle objects: center and radius)
Cylinder: [3.5, 6.4], r = 5.5, h = 10.5 (Cylinder Class Object cylinder data: Center, radius and height)
Point: x = 3.2, y = 4.5 (output Point object point data: coordinates of points)
Area = 0 (area of the point)
Volume = 0 (volume of points)
Circle: x = 2.4, y = 1.2 (output data of Circle objects: center coordinates)
Area = 98.5203 (area of the circle)
Volume = 0 (volume of the circle)
Cylinder: x = 3.5, y = 6.4 (output Cylinder Class Object cylinder data: Center Coordinate)
Area = 512.595 (area of the circle)
Volume = 891.96 (cylindrical volume)
The following conclusions can be further clarified from this example:
/* (1) If a base class contains one or more pure virtual functions, it is the abstract base class.
Abstract base classes cannot and do not need to define objects.
(2) abstract base classes are different from ordinary base classes. They are generally not abstract objects that exist in reality (for example, circles are the abstraction of thousands of actual circles ), it does not have any physical or other practical meanings.
(3) In the hierarchy of classes, the top or top layers can be abstract base classes.
Abstract base classes reflect all kinds of commonalities in this class family, and all types of member functions are declared in abstract base classes.
(4) The abstract base class is the public interface of this class family.
Or, multiple classes derived from the same base class have the same interface.
(5) differences between static and dynamic associations.
(6) If a virtual function is declared in the base class, all functions in the derived class that have the same function name, function type, number of parameters, and type as the function, all are virtual functions (whether or not virtual declaration is used in the derived class ).
(7) using virtual functions improves program scalability.
Separates the declaration of a class from the use of a class.
This is especially important for software developers of design libraries.
Developers have designed a variety of classes, but do not provide source code to users. users do not know how classes are declared, but they can use these classes to generate their own classes.
With virtual functions and polymorphism, programmers focus on processing universality, while making the execution environment special.
Polymorphism leaves the operation details to the class designers (most of whom are professionals) to complete, and the programmers (class users) only need to do some macro work and tell the system what to do, without considering how to do it, it greatly simplifies the coding of applications, greatly reduces the burden on programmers, and reduces the difficulty of learning and using C ++ programming, so that more people can quickly enter the door of C ++ programming. */
From wwj's dream path