Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize Polymorphism in C++

Shulou Source: shulou.com Published: 2022-05-31 22:39:32 10月03日 Update

This article mainly introduces "how to achieve polymorphism in C++". In daily operation, I believe many people have doubts about how to achieve polymorphism in C++. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "how to achieve polymorphism in C++"! Next, please follow the editor to study!

The polymorphism of C++ was realized.

# include

< iostream>

Using namespace std; class A {public: virtual void fun1 (int I) {cout

< < i< < endl; } public: virtual void fun2(int j) { cout< < j< < endl; } public: void fun(A &a) { a.fun1(1); fun2(5); } }; class B:public A { public: void fun1(int i) { i++; cout< < i< < endl; } public: void fun2(int j) { j++; cout< < j< < endl; } }; main() { A a; B b; b.fun(a); a.fun(b); return 0; } C++多态未实现 1 5 1 6 #include < iostream>

Using namespace std; class A {public: virtual void fun1 (int I) {cout < < I < < endl;} public: virtual void fun2 (int j) {cout < j < < endl;} public: void fun (A & a) {a.fun1 (1); fun2 (5);}}; class B:public A {public: void fun1 (int I) {ionization; cout < < I < endl;} public: void fun2 (int j) {jigsaw + Cout < j < < endl;}}; main () {An a; B b; b.fun (a); a.fun (b); return 0;} this ends the study of "how to achieve polymorphism in C++". I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Polymorphisms Clearing learning more help practical next articles methods theories knowledge articles websites materials follow questions easy to use practice solutions Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Redmi macOS Shulou Information vpn