The implementation of polymorphism in C ++ programming languages is a common operation technique in actual programming. We will introduce the implementation of C ++ polymorphism in detail in this article to help you get some help in future practical applications.
C ++ polymorphism 2 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)
- {
- 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 ++ polymorphism is not implemented 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)
- {
- 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;
- }
The above is our introduction to the C ++ polymorphism.