1.1 ¸²¸ÇÒþ²ØÖØÔØ
ÖØÔØ£¬Òþ²Ø£¨Öض¨Ò壩£¬¸²¸Ç£¨ÖØд£©¡ªÕ⼸¸öÃû´Ê¿´×źÃÏñºÜÏñ£¬²»¹ýÆäʵһÑù¶¼²»Ò»Ñù£¡£¡
1.1.1 ÖØÔØ£º
£¨1£©
¸ÅÄÔÚͬһ¸ö×÷ÓÃÓòÄÚ£»º¯ÊýÃûÏàͬ£¬²ÎÊýÁÐ±í²»Í¬£¨²ÎÊý¸öÊý²»Í¬£¬»òÕß²ÎÊýÀàÐͲ»Í¬£¬»òÕß²ÎÊý¸öÊýºÍ²ÎÊýÀàÐͶ¼²»Í¬£©£¬·µ»ØÖµÀàÐÍ¿ÉÏàͬҲ¿É²»Í¬£»ÕâÖÖÇé¿ö½Ð×öc++µÄÖØÔØ£¡
×¢Ò⣺cÓïÑÔûÓк¯ÊýÖØÔصĻúÖÆ£»
£¨2£©¾ÙÀý£º
#include<iostream> using namespace std; int Add(int a,int b) { return a+b; } float Add(float a,float b) { return a+b; } int main() { cout<<Add(4,5)<<endl;//µ÷ÓÃ int Add(int a,int b) cout<<Add(2.5f,3.7f)<<endl;//µ÷ÓÃ float Add(float a,float b) return 0; } |
´Ëʱ£¬Á½¸öº¯ÊýAdd();ÔÚͬһ×÷ÓÃÓò£¬º¯ÊýÃûÏàͬ¶¼ÊÇAdd£¬²ÎÊýÀàÐͲ»Í¬£»¾Í¹¹³ÉÁËc++Öеĺ¯ÊýÖØÔØ£»
£¨3£©c++º¯ÊýÖØÔØ´ïµ½µÄЧ¹û£ºµ÷Óú¯ÊýÃûÏàͬµÄº¯Êý£¬»á¸ù¾Ýʵ²ÎµÄÀàÐͺÍʵ²Î˳ÐòÒÔ¼°Êµ²Î¸öÊýÑ¡ÔñÏàÓ¦µÄº¯Êý£»
£¨4£©c++º¯ÊýÖØÔØÊÇÒ»ÖÖ¾²Ì¬¶à̬£¨ÓÖ½Ð×ö¾²Ì¬Áª±à£¬¾²Ì¬°ó¶¨£¬¾²Ì¬¾öÒ飩
1.1.2 ¸²¸Ç£¨ÓÖ½ÐÖØд£©
£¨1£©Ëµ¸²¸Ç֮ǰÏÈ˵һ¸ö¸ÅÄ
Ð麯Êý£ºÀàµÄ³ÉÔ±º¯ÊýÇ°Ãæ¼Óvirtual¹Ø¼ü×Ö£¬ÔòÕâ¸ö³ÉÔ±º¯Êý³ÆΪÐ麯Êý
£¨2£©¸²¸Ç£¨ÖØд£©µÄÇ°ÌáÌõ¼þ£º¸¸ÀຯÊýΪÐ麯Êý£»
£¨3£©¸²¸Ç£¨ÖØд£©µÄ¸ÅÄµ±ÔÚ×ÓÀàÖж¨ÒåÁËÒ»¸öÓ븸ÀàÍêÈ«ÏàͬµÄÐ麯Êýʱ£¬Ôò³Æ×ÓÀàµÄÕâ¸öº¯ÊýÖØд£¨Ò²³Æ¸²¸Ç£©Á˸¸ÀàµÄÕâ¸öÐ麯Êý¡£
£¨4£©Ê²Ã´ÊÇÔÚ×ÓÀàÖж¨ÒåÁËÒ»¸öÓ븸ÀàÍêÈ«ÏàͬµÄÐ麯Êý£º
ÓÐÁ½ÖÖÇé¿ö£º
1.¾ÍÊÇ˵×ÓÀàÖеÄÐ麯ÊýºÍ¸¸ÀàÖеÄÐ麯Êý£¬º¯ÊýÃû£¬²ÎÊý¸öÊý£¬²ÎÊýÀàÐÍ£¬·µ»ØÖµÀàÐͶ¼Ïàͬ£»ÕâÖÖÇé¿öÏÂ×ÓÀàµÄÕâ¸öÐ麯ÊýÖØдµÄ¸¸ÀàÖеÄÐ麯Êý£¬¹¹³ÉÁËÖØд£»
2¡¢Ð±ä¡ªÊÇ˵×ÓÀàÖеÄÐ麯ÊýºÍ¸¸ÀàÖеÄÐ麯Êý£¬º¯ÊýÃû£¬²ÎÊý¸öÊý£¬²ÎÊýÀàÐͶ¼Ïàͬ£¬Ö»ÊÇ·µ»ØÖµÀàÐͲ»Í¬£»¸¸ÀàµÄÐ麯Êý·µ»Ø¸¸ÀàµÄÖ¸Õë»òÕßÒýÓã¬×ÓÀàÐ麯Êý·µ»Ø×ÓÀàµÄÖ¸Õë»òÕßÒýÓã»ÕâÖÖÇé¿öÏÂ×ÓÀàµÄÕâ¸öÐ麯ÊýÒ²ÖØдÁ˸¸ÀàÖеÄÐ麯Êý£¬Ò²¹¹³ÉÁËÖØд£»¡ª¡ªÎÒÃÇ°ÑÕâÖÖÌØÊâµÄÇé¿ö½Ð×öбä
×¢Ò⣺
ÔÚ×ÓÀàÖж¨ÒåÁËÒ»¸öÓ븸ÀàÐ麯ÊýÍêÈ«ÏàͬµÄº¯Êý£¬ÄÇôÕâ¸ö×ÓÀàµÄº¯Êý¾ÍÊÇÖØдÁ˸¸ÀàµÄÐ麯Êý£¬´ËʱÕâ¸ö×ÓÀàµÄº¯Êý¾ÍÊÇÐ麯Êý£¬Èç¹û²»ÏÔʾµÄ¼ÓÉÏvirtualÐÞÊΣ¬±àÒëÆ÷Ò²»áĬÈÏΪÐ麯Êý£»
£¨5£©¸²¸Ç£¨ÖØд£©´ïµ½µÄЧ¹û£º
1.ÔÚ×ÓÀàÖÐÖØдÁ˸¸ÀàµÄÐ麯Êý£¬ÄÇô×ÓÀà¶ÔÏóµ÷ÓøÃÖØдº¯Êý£¬µ÷Óõ½µÄÊÇ×ÓÀàÄÚ²¿ÖØдµÄÐ麯Êý£¬¶ø²¢²»ÊÇ´Ó¸¸Àà¼Ì³ÐÏÂÀ´µÄÐ麯Êý£»£¨ÕâÆäʵ¾ÍÊǶ¯Ì¬¶à̬µÄʵÏÖ£©£»
2.ÔÚ×ÓÀàÖÐÖØдÁ˸¸ÀàµÄÐ麯Êý£¬Èç¹ûÓÃÒ»¸ö¸¸ÀàµÄÖ¸Õ루»òÒýÓã©Ö¸Ïò£¨»òÒýÓã©×ÓÀà¶ÔÏó£¬ÄÇôÕâ¸ö¸¸ÀàµÄÖ¸Õë»òÓÃÒýÓõ÷ÓøÃÖØдµÄÐ麯Êý£¬µ÷ÓõÄÊÇ×ÓÀàµÄÐ麯Êý£»Ïà·´£¬Èç¹ûÓÃÒ»¸ö¸¸ÀàµÄÖ¸Õ루»òÒýÓã©Ö¸Ïò£¨»òÒýÓ㩸¸ÀàµÄ¶ÔÏó£¬ÄÇôÕâ¸ö¸¸ÀàµÄÖ¸Õë»òÓÃÒýÓõ÷ÓøÃÖØдµÄÐ麯Êý£¬µ÷ÓõÄÊǸ¸ÀàµÄÐ麯Êý
£¨6£©¾ÙÀý1£ºÆÕͨÖØд+º¯ÊýÖØÔØ
//ÆÕͨÖØд+º¯ÊýÖØÔØ
#include<iostream> using namespace std; class Person//¸¸Àà { public: virtual void BuyTickets()//¸¸ÀàÐ麯Êý { cout<<" ÂòƱ-ȫƱ"<< endl; } protected : string _name; // ÐÕÃû };
class Student : public Person//×ÓÀà { public: void BuyTickets()//×ÓÀàÐ麯Êý { cout<<" ÂòƱ-°ë¼Û"<<endl ; } protected : int _num ; //ѧºÅ }; void Fun (Person* p) { p->BuyTickets(); } void Fun (Person&p) { p.BuyTickets(); } void Test () { Person p ; Student s; Fun(p); Fun(s);
Fun(&p); Fun(&s); } int main() { Test(); return 0; } |
1.1.3 Òþ²Ø£¨Öض¨Ò壩
£¨1£©Òþ²Ø£¨Öض¨Ò壩¸ÅÄ
ÊÇÖ¸ÔÚ²»Í¬µÄ×÷ÓÃÓòÖУ¨·Ö±ðÔÚ¸¸ÀàºÍ×ÓÀàÖУ©£¬º¯ÊýÃûÏàͬ£¬²»Äܹ¹³ÉÖØдµÄ¶¼ÊÇÖض¨Òå¡£
£¨2£©Òþ²Ø£¨Öض¨Ò壩µÄʹÓ÷¶Î§£º
Öض¨ÒåµÄ²»¹âÊÇÀàµÄ³ÉÔ±º¯Êý£¬»¹¿ÉÒÔÊÇÀàµÄ³ÉÔ±±äÁ¿£»
£¨3£©Òþ²Ø£¨Öض¨Ò壩µÄÖ±½ÓЧ¹û£º
Èç¹ûÔÚ¸¸ÀàºÍ×ÓÀàÖÐÓÐÏàͬÃû×ֵijÉÔ±£»ÄÇôÔÚ×ÓÀàÖС£»á½«¸¸ÀàµÄ³ÉÔ±Òþ²Ø£»Òþ²ØÒÔºóµÄÖ±½ÓЧ¹û¾ÍÊÇ£ºÎÞÂÛÔÚ×ÓÀàµÄÄÚ²¿»òÕßÍⲿ£¨Í¨¹ý×ÓÀà³ÉÔ±£©·ÃÎʸóÉÔ±£»È«¶¼ÊÇ·ÃÎÊ×ÓÀàµÄͬÃû³ÉÔ±£»Èç¹ûÔÚ×ÓÀàÄÚ²¿»òÕßÍⲿ£¨Í¨¹ý×ÓÀà³ÉÔ±£©·ÃÎÊͬÃûµÄ³ÉÔ±º¯Êý£¬ÔòÐèÒª¸ù¾Ýº¯Êýµ÷ÓõĹæÔòÀ´µ÷ÓÃ×ÓÀàµÄͬÃû³ÉÔ±º¯Êý£»·ñÔòµ÷ÓÃʧ°Ü£»
#include<iostream> using namespace std; class A { protected: A(int x=2) :_a(x) {} //public: // //1 // void show() // { // cout<<"A::shou()"<<endl; // } //public: // //2 // virtual void show() // { // cout<<"A::shou()"<<endl; // } public: //3 void show(int a) { cout<<"A::shou()"<<endl; } public: int _a; }; class B:public A { public: B(int x=1) :_a(x) {} //public: // //1 // void show(int b) // { // cout<<"B::shou()"<<endl; // } //public: // //2 // void show(int a) // { // cout<<"B::shou()"<<endl; // } public: //3 void show(int a,int b) { cout<<"B::shou()"<<endl; cout<<_a<<endl; } public: int _a; };
int main() { B b; cout<<(b._a)<<endl; b.show(1); return 0; } |
³ÌÐòÖÐËù±êÃ÷µÄ1£¬2£¬3ÈýÖÖÇé¿ö¶¼ÊdzÉÔ±Òþ²Ø£¡