Lesson 49 of C + + -- the concept and significance of polymorphism

This paper studies the C + + course of Mr. Tang zuolin from Ditai Software Institute ...

This paper studies the C + + course of Mr. Tang zuolin from Ditai Software Institute

When the function rewriting meets the assignment compatibility with the analysis of the last example in the above section, the compiler will call the member function of the pointer or reference type class directly for safety, regardless of the type pointed to, which does not meet our actual requirements, so we introduce the concept of polymorphism, adding the member function that may be rewritten in the parent class to the virtual virtual function In this way, virtual tells the compiler that print() may be rewritten in the subsequent inherited process. Therefore, it prompts the compiler to pay attention to whether to display polymorphic behavior when calling this function, so that the compiler will not call member functions of pointer or reference type classes directly for safety, but will call those pointed to by pointer p Member function from the actual object type.

Experiment 1: initial experience of polymorphism
Experiment 2: static binding and dynamic binding

Experiment 1: initial experience of polymorphism

#include <iostream> #include <string> using namespace std; class Parent { public: /*virtual function virtual It tells the compiler that print() may be rewritten in the subsequent inherited process. Therefore, it prompts the compiler to pay attention to whether to display polymorphic behavior when calling this function, so that the compiler will not call member functions of pointer or reference type classes directly for safety. */ virtual void print() { cout << "I'm Parent." << endl; } }; class Child : public Parent { public: //virtual void print() can omit virtual of subclass void print() { cout << "I'm Child." << endl; } }; void how_to_print(Parent* p) { /* At this time, p - > print(); shows polymorphism, and judges which member function exactly according to the actual object type pointed to by pointer p If p points to a parent object, the member function print() in the parent is called If p points to a subclass object, the member function print() in the subclass is called */ p->print(); // Show polymorphic behavior } int main() { Parent p; Child c; how_to_print(&p); // Expected to print: I'm Parent. how_to_print(&c); // Expected to print: I'm Child. return 0; } mhr@ubuntu:~/work/c++$ mhr@ubuntu:~/work/c++$ g++ 49-1.cpp mhr@ubuntu:~/work/c++$ ./a.out I'm Parent. I'm Child. mhr@ubuntu:~/work/c++$

virtual void print()

virtual tells the compiler that print() may be rewritten in the subsequent inheritance process. Therefore, it prompts the compiler to pay attention to whether to display polymorphic behavior when calling this function, so that the compiler will not call member functions of pointer or reference type classes directly for safety, but will call the actual object type pointed to by pointer p Member functions.

Note that the virtual function only needs to add virtual attribute in the parent class, while the overriding function in the subclass does not need to add virtual attribute, and it doesn't matter whether it is added or not.

Experiment 2: static binding and dynamic binding

#include <iostream> #include <string> using namespace std; class Parent { public: virtual void func() { cout << "void func()" << endl; } virtual void func(int i) { cout << "void func(int i) : " << i << endl; } virtual void func(int i, int j) { cout << "void func(int i, int j) : " << "(" << i << ", " << j << ")" << endl; } }; class Child : public Parent { public: void func(int i, int j) { cout << "void func(int i, int j) : " << i + j << endl; } void func(int i, int j, int k) { cout << "void func(int i, int j, int k) : " << i + j + k << endl; } }; void run(Parent* p) { p->func(1, 2); // Show polymorphism // Dynamic linking } int main() { Parent p; p.func(); // Static linking p.func(1); // Static linking p.func(1, 2); // Static linking cout << endl; Child c; c.func(1, 2); // Static linking cout << endl; run(&p); run(&c); return 0; } mhr@ubuntu:~/work/c++$ g++ 49-2.cpp mhr@ubuntu:~/work/c++$ ./a.out void func() void func(int i) : 1 void func(int i, int j) : (1, 2) void func(int i, int j) : 3 void func(int i, int j) : (1, 2) void func(int i, int j) : 3 mhr@ubuntu:~/work/c++$

Dairy farm pony 223 original articles published, 100 praised, 80000 visitors+ Private letter follow

4 February 2020, 23:39 | Views: 2207

Add new comment

For adding a comment, please log in
or create account

0 comments