[TOC]
#虛函數(shù)和多態(tài)
創(chuàng)新互聯(lián)專注于新寧網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供新寧營銷型網(wǎng)站建設(shè),新寧網(wǎng)站制作、新寧網(wǎng)頁設(shè)計、新寧網(wǎng)站官網(wǎng)定制、微信小程序開發(fā)服務(wù),打造新寧網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供新寧網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
###代碼示例:
class Person
{
public:
virtual void Buyticket()
{
cout << "買票—全價" << endl;
}
protected:
char* _name;
};
class Person
{
public:
virtual void Buyticket()
{
cout << "買票—全價" << endl;
}
protected:
char* _name;
};
class Student : public Person
{
public:
virtual void Buyticket()
{
cout << "買票—半票" << endl;
}
protected:
char* _name;
};
##多態(tài)
###定義:一個對象呈現(xiàn)多種形態(tài)
###代碼示例:
class Person
{
public:
virtual void Buyticket()
{
cout << "買票—全價" << endl;
}
protected:
char* _name;
};
class Student : public Person
{
public:
virtual void Buyticket()
{
cout << "買票—半票" << endl;
}
protected:
char* _name;
};
void Fun(Person &p)//一個函數(shù)實現(xiàn)兩種形態(tài)
{
p.Buyticket();
}
int main()
{
Person p;
Student s;
Fun(p);
Fun(s);
system("pause");
return 0;
}
看看多態(tài)的結(jié)果:
多態(tài)的的特性: