1. Say in front:
The project is largely resolved and now frees up time to optimize the project and learn new knowledge.
2.c++ polymorphism
1. Description: (polymorphic) polymorphism
For C + + polymorphism, this is a very flexible technology, the use of very smart, difficult; Simply put: polymorphism is the appropriate use of interface functions, through an interface to use a variety of methods, (equivalent to the superior said a command, a,b,c,d and other people are reacting, a command, multiple responses;
2. How to achieve polymorphism
1. Using virtual functions and pointers for polymorphism
A virtual function is used to define a method to be implemented, and a pointer to a base class that points to a virtual function method of a different subclass
2. The advantage of polymorphism is to implement interface reuse
3. Virtual function:
1. Virtual function uses the keyword virtual modification, virtual function allows subclasses to redefine the member function;
2. Rewrite (the function name is the same, but the content is inconsistent)//also known as overwrite (override)
3. Overloading (the function name is the same, but the parameter table column is inconsistent)
4. Polymorphism is typically done by rewriting virtual functions, then pointing to different sub-objects with a base-class pointer, calling different virtual functions
4. Code implementation
[CPP]View PlainCopy
- #include <iostream>
- #include <stdlib.h>
- #include <string>
- Using namespace std;
- Class IShape
- {
- Public
- //Polymorphism implementation requires virtual method
- virtual double get_area () = 0;
- virtual String get_name () = 0;
- };
- Defining Class 1 Ccircle
- Class Ccircle: Publicishape
- {
- //Declaration need to bring the virtual keyword to show polymorphism
- Public
- Ccircle (double radius) {This->c_radius = radius;}
- Ccircle () {}
- virtual double Get_area ();
- virtual string get_name ();
- Private
- double C_radius; //Define the radius of the circle
- };
- Defining methods
- Double Ccircle::get_area ()
- {
- return 3.14*c_radius*c_radius;
- }
- String Ccircle::get_name ()
- {
- return "Ccircle";
- }
- Class CRect: Publicishape
- {
- Public
- CRect (double length, double width) { this->m_length = length, this->m_width = width;}
- CRect () {};
- virtual double Get_area ();
- virtual string get_name ();
- Private
- double m_length;
- double m_width;
- };
- Double Crect::get_area ()
- {
- return m_length*m_width;
- }
- String Crect::get_name ()
- {
- return "Rectangle";
- }
- Pointing to different classes with pointers and using different methods
- #include "Text.h"
- void Main ()
- {
- <span style="WHITE-SPACE:PRE;" > </span>ishape *point = NULL; //Build pointer
- <span style="WHITE-SPACE:PRE;" > </span>point = new Ccircle (10);
- <span style="WHITE-SPACE:PRE;" > </span>//round type
- <span style="WHITE-SPACE:PRE;" > </span>cout << point->get_name () << "area is:" << point->get_area () << Endl;
- <span style="WHITE-SPACE:PRE;" > </span>Delete point;
- <span style="WHITE-SPACE:PRE;" > </span>//Rectangle class
- <span style="WHITE-SPACE:PRE;" > </span>point = new CRect (10, 20);
- <span style="WHITE-SPACE:PRE;" > </span>cout << point->get_name () << "area is:" << point->get_area () << Endl;
- <span style="WHITE-SPACE:PRE;" > </span>Delete point;
- <span style="WHITE-SPACE:PRE;" > </span>system ("pause");
- }
- <span style="WHITE-SPACE:PRE;" > </span>
- <span style="WHITE-SPACE:PRE;" > </span>
- <span style="WHITE-SPACE:PRE;" > </span>
- <span style="WHITE-SPACE:PRE;" > </span>
Understanding of C + + polymorphism