In the past few days, I have focused on the inheritance and Polymorphism of C ++;
Through continuous optimization of one question, the effect of understanding is achieved. The Inheritance focuses on the differences between the three inheritance methods.
Public private protected
Public inheritance: the access attributes of the public and protected members of the base class remain unchanged in the derived class, but the Private Members of the base class are not accessible.
Private inheritance: both the public and protected members of the base class appear in the derived class as private, but the Private Members of the base class are not accessible.
Protected inheritance: both the public and protected members of the base class appear in the derived class as protected, but the Private Members of the base class cannot access
Public inheritance: The member functions of the derived class can directly access the public and protected members in the base class, but cannot access the Private Members of the base class.
Private inheritance: The member functions of the derived class can directly access the public and protected members in the base class, but cannot access the Private Members of the base class.
Protected inheritance: The member functions of the derived class can directly access the public and protected members in the base class, but cannot access the Private Members of the base class.
Public inheritance: only the public members of the base class can be accessed through the object of the derived class.
Private inheritance: Objects of derived classes cannot access any member of the base class.
Protected inheritance: Objects in Derived classes cannot access any member of the base class.
The three methods are the same for the member functions of the derived class. Both the public and protected members in the base class can be accessed, but the Private Members of the base class cannot be accessed.
You can access the public members of the base class in the public mode of the object of the derived class. The private mode and the protected mode cannot access any members of the base class.
The horizontal access of protected is the same as that of private, and the vertical access is the same as that of public members, that is, data is hidden and inheritance is convenient.
Polymorphism is mainly a virtual function: to define a function in the base class as a virtual function, it is to tell the compiler, "I don't know how the function is implemented, please wait until it is called in the program and then obtain the function implementation from the object instance"
The following is the question I mentioned, where inheritance and polymorphism are used. Please thoroughly understand this abstract thinking.
One program:
Parent class: graphic, method: Area
Subclass: Circle, square, rectangular
In the main function, calculate the area of a graph (circle, square, rectangle ).
First, define the basic class of graphics, which includes the area calculation method and set this method as a virtual function. However, I don't know what images will be implemented in the future, I only know that any image should have an area method, and then the subclass (such as a circle) inherits the base class of the image, therefore, it also has the virtual function of area (inherited from the parent class ). In the subclass, I want to rewrite the internal implementation of this method. In the main function, I define a reference (pointer) of the base type, which is a base class, therefore, when you assign the object address of the subclass to it, you should call the area calculation method inherited from the subclass (base class, but in the subclass, I implemented this method as needed, so it will execute the method I wrote in The subclass to calculate the area.
Code:
# Include <iostream> using namespace STD; Class shape {public: Virtual void getarea () ;}; class circle: Public shape {PRIVATE: Double R; public: circle (Double R); void getarea () ;}; class square: Public shape {PRIVATE: Double A; public: Square (double A); void getarea ();}; class rectangle: Public shape {PRIVATE: Double X, Y; public: rectangle (Double X, Double Y); void getarea () ;}; void shape: getarea () {cout <"getarea function of the base class" <Endl;} circle: Circle (double M_r) {r = M_r;} void circle: getarea () {cout <"radius:" <r <"The area of the circle is:" <3.1415926 * r <Endl;} square :: square (double M_a) {A = M_a;} void square: getarea () {cout <"side length:" <A <"the square area is: "<A * A <Endl;} rectangle: rectangle (double m_x, double m_y) {x = m_x; y = m_y;} void rectangle: getarea () {cout <"rectangular area:" <x * Y <Endl ;}int main () {shape * P; Double R, A, X, Y; cout <"enter the circle radius:"; CIN> r; Circle CIR (r); P = & CIR; P-> getarea (); cout <"Enter the side length of a square:"; CIN> A; Square squ (a); P = & squ; P-> getarea (); cout <"enter an edge of the rectangle:"; CIN> X; cout <"enter an edge of the rectangle:"; CIN> Y; rectangle Rec (x, y); P = & REC; P-> getarea (); Return 0 ;}
=========== Over