8.1 Ð麯Êý
8.1.1 Ð麯ÊýµÄÉùÃ÷Ó붨Òå
ÐèÒª½èÖú¹Ø¼ü×Ö virtual¹Ø¼ü×ÖʵÏÖ
¸ñʽ£º
class ˈ̞
{
public£º
virtual º¯Êý·µ»ØÖµÀàÐÍ º¯ÊýÃû(²ÎÊýÁбí); //Ö»ÐèÒªÔÚº¯ÊýµÄÉùÃ÷Ç°¼Ó£¬±íʾÉùÃ÷¸Ãº¯ÊýΪÐ麯Êý
}
ÒÔ¸ÃÀàÅÉÉú³öµÄÅÉÉúÀ࣬ÅÉÉúÀàÖض¨Ò常ÀàµÄÐ麯ÊýµÄʱºò£¬¼´Ê¹²»¼Óvirtual ¹Ø¼ü×Ö£¬´ËʱÔÚ×ÓÀàÖиú¯ÊýÒ²ÊÇÐ麯Êý£¬ÒÔËüµÄ×ÓÀàΪ»ùÀà¼ÌÐøÅÉÉú³öеÄÅÉÉúÀàÖض¨ÒåÐé·½·¨£¬ÔÚеÄÅÉÉúÀàÖÐÒ²ÊÇÐ麯Êý
ÅÉÉúÀàÖض¨Ò常ÀàµÄÐé·½·¨ÐèÒªÂú×ãÒ»¶¨µÄ¸ñʽ£¬²ÅÄÜÔÚÅÉÉúÀàÖÐÒ²ÊÇÐ麯Êý
1£©ÅÉÉúÀàÖÐÐèÒªÓë»ùÀàµÄ×ÊԴͬÃû£¬ÓÐÏàͬµÄº¯ÊýÃû
2£©Óë»ùÀàÓÐÏàͬµÄ²ÎÊýÁÐ±í£¨²ÎÊýµÄ¸öÊý£¬²ÎÊýµÄλÖ㬲ÎÊýµÄÀàÐÍ£©
3£©Óë»ùÀàÐèÒªÓÐÏàͬµÄº¯Êý·µ»ØÖµÀàÐÍ
Èç¹û»ùÀà·µ»ØÖµÀàÐÍΪ»ùÀàµÄÖ¸Õë»òÕßÊÇ»ùÀàµÄÒýÓã¬ÔòÔÚÅÉÉúÀàÖУ¬ÅÉÉúÀàÖÐÐ麯Êý·µ»ØÖµÀàÐÍΪÅÉÉúÀàµÄÖ¸Õë»òÕßÅÉÉúµÄÀàÒýÓÃ
8.1.2 Ð麯ÊýµÄÓ÷¨
1£©ÀàÐÍÊÊÓ¦ £¬º¯ÊýµÄ²ÎÊý´«µÝʵÏÖ¶àÑù»¯£¬ÅÉÉúÀàÊÊÓ¦»ùÀ࣬ɥʧ¶à̬ÐÔ
2£©Ð麯Êý £¬ ʹÓûùÀàµÄÖ¸Õë»òÕß»ùÀàµÄÒýÓ÷ÃÎÊͬÃû×ÊԴʱºò£¬²»È¡¾öÓÚ»ùÀàµÄÀàÐͶøÊÇÓÉÀàÐÍÖ¸ÕëÖ¸ÏòµÄ¶ÔÏóµÄÀàÐÍ»òÕßÒýÓÃÒýÓÃÄ¿±êµÄÀàÐ;ö¶¨·ÃÎÊÊÇÄĸöº¯ÊýʵÌ壬
3£©»ùÀàµÄÖ¸Õ룬»ùÀàµÄÒýÓÃ
8.2 Ð麯ÊýµÄ·ÃÎÊ
¶ÔÐ麯ÊýµÄ·ÃÎÊ·½Ê½²»Í¬£¬³ÌÐò¾ßÌåµ÷ÓÃÄĸöº¯Êý¿ÉÄÜÒ²»áÓÐËù²»Í¬
8.2.1 ¶ÔÏóÃû·ÃÎÊ
ͨ¹ý¶ÔÏó·ÃÎÊÐ麯Êý»¹ÊÇ·ÇÐ麯Êý£¬ÓɶÔÏó±¾ÉíµÄÀàÐ;ö¶¨£¬----¡·²ÉÓþ²Ì¬Áª±àµÄÐÎʽ
#include <iostream>
using namespace std;
class Base { public: void show(){cout << "Base Ààshowº¯Êý"<<endl;} //ÔÚº¯ÊýµÄÉùÃ÷Ç°¼Ó virtual int fun(int x){cout << "Base Ààfunº¯Êý" <<endl;return x ;} virtual void test(){cout << "BaseÀàµÄ testº¯Êý"<<endl;} };
class Child:public Base { public: void show(){cout << "Child Ààshowº¯Êý"<<endl;} //¼´Ê¹²»¼Óvirtual¹Ø¼ü×Ö£¬¸Ãº¯ÊýÈÔÈ»ÊÇÐ麯Êý int fun(int x){cout << "Child Ààfunº¯Êý" <<endl;return x ;} };
class grandChild:public Child { public: void show(){cout << "grandChild Ààshowº¯Êý"<<endl;} //ÈÔÈ»ÊÇÐ麯Êý int fun(int x){cout << "grandChild Ààfunº¯Êý" <<endl;return x ;} void test(){cout << "grandChildÀàµÄ testº¯Êý"<<endl;} };
int main() { Child ch; Base base; grandChild grand;
base = ch; base.show(); base.fun(10);
base = grand; base.show(); base.fun(10);
ch = grand; ch.show(); ch.fun(10); //ÅÉÉúÀà¶ÔÏó¸³Öµ¸ø»ùÀà¶ÔÏó£¬ //ÎÞÂÛÊǵ÷Ó÷ÇÐ麯Êý»¹ÊÇÐ麯Êý£¬µ÷Óö¼ÊÇ»ùÀàµÄº¯Êý } |
8.2.2 Ö¸Õë·ÃÎÊ
Ö¸Õë·ÃÎʵÄÊÇ·ÇÐ麯Êý£¬ÓÉÖ¸Õë±¾ÉíµÄÀàÐÍ£¨¶¨ÒåʱָÕëÖ¸ÏòµÄ¶ÔÏóµÄÀàÐÍ£©¾ö¶¨·ÃÎʵÄÊÇÄĸöº¯Êý
Ö¸Õë·ÃÎʵÄÊÇÐ麯Êý£¬ÔòÓÉʵ¼ÊÉÏÖ¸ÕëÖ¸ÏòµÄ¶ÔÏóµÄÀàÐ;ö¶¨£¬¶ø²»ÊǶ¨ÒåÖ¸ÕëÖ¸ÕëÖ¸ÏòµÄ¶ÔÏóµÄÀàÐÍ
int main() { Child ch,*pch; Base *base; grandChild grand;
base = &ch; //ÅÉÉúÀà¶ÔÏó¸³Öµ¸ø»ùÀàÖ¸Õë base->show(); //»ùÀàÖ¸Õë·ÃÎÊ·ÇÐ麯Êý base->fun(10);//»ùÀàÖ¸Õë·ÃÎÊÐ麯Êý
cout << "========================="<<endl;
base = &grand; base->show(); //»ùÀàÖ¸Õë·ÃÎÊ·ÇÐ麯Êý base->fun(10);//»ùÀàÖ¸Õë·ÃÎÊÐ麯Êý
cout << "========================="<<endl; pch = &grand; pch->show();//»ùÀàÖ¸Õë·ÃÎÊ·ÇÐ麯Êý pch->fun(20); //»ùÀàÖ¸Õë·ÃÎÊ·ÇÐ麯Êý } |
8.2.3 ÒýÓ÷ÃÎÊ
ÒýÓõı¾ÖÊÊÇÖ¸Õ룬ÒýÓ÷ÃÎÊ·ÇÐ麯ÊýºÍÐ麯ÊýµÄ·ÃÎÊ·½·¨Ò»Ñù
ÒýÓÃÒ»µ©³õʼ»¯Ö®ºó£¬ÔòÒýÓõĶÔÏó¾Í¹Ì¶¨²»¿É¸Ä±ä£¬ºóÆÚ¶ÔÒýÓýøÐи³Öµ²»ÄܸıäÒýÓõĶÔÏó
ÒýÓ÷ÃÎÊÐ麯Êý£¬ÓÉʵ¼ÊÉÏÒýÓõÄÒýÓöÔÏóµÄÀàÐ;ö¶¨
ÒýÓ÷ÃÎʵÄÊÇ·ÇÐ麯Êý£¬ÔòÓÉÒýÓñ¾ÉíµÄÀàÐ;ö¶¨
int main() { Child ch; grandChild grand;
Base &base = ch; //»ùÀàhaseµÄÒýÓöÔÏóÊÇÅÉÉúÀàch¶ÔÏó base.show(); //ÒýÓ÷ÃÎÊ·ÇÐ麯Êý base.fun(10);//ÒýÓ÷ÃÎÊÐ麯Êý
cout << "========================="<<endl;
base = grand; //¶ÔÒýÓø³Öµ base.show(); //ÒýÓ÷ÃÎÊ·ÇÐ麯Êý base.fun(10); //ÒýÓ÷ÃÎÊÐ麯Êý
cout << "========================="<<endl; Child &pch = grand; // pch.show(); //ÒýÓ÷ÃÎÊ·ÇÐ麯Êý pch.fun(20);//ÒýÓ÷ÃÎÊÐ麯Êý
pch = ch; pch.show(); //ÒýÓ÷ÃÎÊ·ÇÐ麯Êý pch.fun(20);//ÒýÓ÷ÃÎÊÐ麯Êý } |
8.2.4 ÀàÄÚ·ÃÎÊ
ÔÚÀàµÄ³ÉÔ±º¯ÊýÄÚ²¿·ÃÎÊ£¬³ÉÔ±º¯ÊýÄÚ²¿ÓÐthisÖ¸Õ룬·ÖÇåthis Ö¸Õë±¾ÉíÀàÐÍ£¨³ÉÔ±º¯ÊýËùÔÚµÄÀàÐÍ£©
thisÖ¸Õëʵ¼ÊÉÏÖ¸ÏòµÄ¶ÔÏóµÄÀàÐÍ
-------¨¤Ö¸Õë·ÃÎÊÐ麯ÊýºÍ·ÇÐ麯Êý
#include <iostream>
using namespace std;
class Base { public: void show(){cout << "Base Ààshowº¯Êý"<<endl;} virtual void fun(){cout << "Base Ààfunº¯Êý" <<endl;} void base_1() { //this Ö¸Õë±¾ÉíÀàÐÍ£ºBase this->show(); this->fun(); cout << "Base Ààbase_1º¯Êý" <<endl; } void base_2() { Base::show(); Base::fun(); cout << "Base Ààbase_2º¯Êý" <<endl; } };
class Child:public Base { public: void show(){cout << "Child Ààshowº¯Êý"<<endl;} //¼´Ê¹²»¼Óvirtual¹Ø¼ü×Ö£¬¸Ãº¯ÊýÈÔÈ»ÊÇÐ麯Êý void fun(){cout << "Child Ààfunº¯Êý" <<endl;} };
int main() { Child ch; Base base;
ch.base_1(); ch.base_2(); cout << "===============" << endl; base.base_1(); base.base_2(); cout << "===============" << endl; base = ch; base.base_1(); base.base_2() } |
8.2.5 ÔÚ¹¹Ô캯Êý»òÎö¹¹º¯ÊýÖзÃÎÊ
ÔÚ¹¹Ô캯ÊýºÍÎö¹¹º¯ÊýÖзÃÎÊÐ麯ÊýºÍ·ÇÐ麯Êý£¬Êµ¼ÊÉϲÉÓþ²Ì¬Áª±àµÄÐÎʽ£¬ÓÉ×Ô¼º±¾ÀàµÄÀàÐ;ö¶¨
ÔÚÄĸöÀàÖÐÔò·ÃÎʵÄÊÇÄĸöÀàµÄÐ麯ÊýºÍ·ÇÐ麯Êý
#include <iostream>
using namespace std;
class Base { public: void show(){cout << "Base Ààshowº¯Êý"<<endl;} virtual void fun(){cout << "Base Ààfunº¯Êý" <<endl;} Base(){show(); fun(); cout << "BaseÀàÐ͵Ĺ¹Ô캯Êý" <<endl;} ~Base(){show(); fun(); cout << "BaseÀàÐ͵ÄÎö¹¹º¯Êý" <<endl;} }; class Child:public Base { public: void show(){cout << "Child Ààshowº¯Êý"<<endl;} void fun(){cout << "Child Ààfunº¯Êý" <<endl;} Child():Base(){show(); fun(); cout << "ChildÀàÐ͵Ĺ¹Ô캯Êý" <<endl;} ~Child(){show(); fun(); cout << "ChildÀàÐ͵ÄÎö¹¹º¯Êý" <<endl;} }; int main() { Child ch;
} |
½«ÅÉÉúµÄÀàÐͶÔÏó¸³Öµ¸ø»ùÀàµÄÖ¸Õë»òÕß»ùÀàµÄÒýÓ㬷ÃÎÊÐ麯Êý£¬ÓÉÒýÓûòÕßÖ¸Õëʵ¼ÊÉÏÖ¸ÏòµÄ¶ÔÏóµÄÀàÐ;ö¶¨