Understanding of C + + polymorphism

Source: Internet
Author: User

 

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
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <string>
  4. Using namespace std;
  5. Class IShape
  6. {
  7. Public
  8. //Polymorphism implementation requires virtual method
  9. virtual double get_area () = 0;
  10. virtual String get_name () = 0;
  11. };
  12. Defining Class 1 Ccircle
  13. Class Ccircle: Publicishape
  14. {
  15. //Declaration need to bring the virtual keyword to show polymorphism
  16. Public
  17. Ccircle (double radius) {This->c_radius = radius;}
  18. Ccircle () {}
  19. virtual double Get_area ();
  20. virtual string get_name ();
  21. Private
  22. double C_radius; //Define the radius of the circle
  23. };
  24. Defining methods
  25. Double Ccircle::get_area ()
  26. {
  27. return 3.14*c_radius*c_radius;
  28. }
  29. String Ccircle::get_name ()
  30. {
  31. return "Ccircle";
  32. }
  33. Class CRect: Publicishape
  34. {
  35. Public
  36. CRect (double length, double width) { this->m_length = length, this->m_width = width;}
  37. CRect () {};
  38. virtual double Get_area ();
  39. virtual string get_name ();
  40. Private
  41. double m_length;
  42. double m_width;
  43. };
  44. Double Crect::get_area ()
  45. {
  46. return m_length*m_width;
  47. }
  48. String Crect::get_name ()
  49. {
  50. return "Rectangle";
  51. }
  52. Pointing to different classes with pointers and using different methods
  53. #include "Text.h"
  54. void Main ()
  55. {
  56. <span style="WHITE-SPACE:PRE;" > </span>ishape *point = NULL; //Build pointer
  57. <span style="WHITE-SPACE:PRE;"  > </span>point = new Ccircle (10);
  58. <span style="WHITE-SPACE:PRE;" > </span>//round type
  59. <span style="WHITE-SPACE:PRE;"  > </span>cout << point->get_name () << "area is:" << point->get_area () << Endl;
  60. <span style="WHITE-SPACE:PRE;"  > </span>Delete point;
  61. <span style="WHITE-SPACE:PRE;" > </span>//Rectangle class
  62. <span style="WHITE-SPACE:PRE;"  > </span>point = new CRect (10, 20);
  63. <span style="WHITE-SPACE:PRE;"  > </span>cout << point->get_name () << "area is:" << point->get_area () << Endl;
  64. <span style="WHITE-SPACE:PRE;"  > </span>Delete point;
  65. <span style="WHITE-SPACE:PRE;"  > </span>system ("pause");
  66. }
  67. <span style="WHITE-SPACE:PRE;" > </span>
  68. <span style="WHITE-SPACE:PRE;" > </span>
  69. <span style="WHITE-SPACE:PRE;" > </span>
  70. <span style="WHITE-SPACE:PRE;" > </span>

Understanding of C + + polymorphism

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.