C++多態(tài)實(shí)現(xiàn)方法探討
作者:佚名
今天我們在這里將會(huì)通過一段代碼示例來為大家詳細(xì)講解有關(guān)C++多態(tài)的具體實(shí)現(xiàn)方法。希望朋友們可以通過這些內(nèi)容掌握其中的應(yīng)用技巧。
C++編程語言中的多態(tài)的實(shí)現(xiàn),在實(shí)際編程中是比較常見的一個(gè)操作技巧。我們今天將會(huì)在這篇文章中為大家詳細(xì)介紹一下有關(guān)C++多態(tài)的實(shí)現(xiàn)方法,以方便大家在今后的實(shí)際應(yīng)用中獲得一些幫助。
實(shí)現(xiàn)了C++多態(tài) 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++多態(tài)未實(shí)現(xiàn) 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;
- }
以上就是我們對C++多態(tài)的相關(guān)介紹。
【編輯推薦】
責(zé)任編輯:曹凱
來源:
博客園