您好,登錄后才能下訂單哦!
[TOC]
#虛函數和多態
###代碼示例:
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;
};
##多態
###定義:一個對象呈現多種形態
###代碼示例:
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)//一個函數實現兩種形態
{
p.Buyticket();
}
int main()
{
Person p;
Student s;
Fun(p);
Fun(s);
system("pause");
return 0;
}
看看多態的結果:
多態的的特性:
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。