简要咨询咨询QQ网站导航网站搜索手机站点联系我们设为首页加入收藏 

C++多态特性例

来源:易贤网   阅读:993 次  日期:2014-09-05 10:13:50

温馨提示:易贤网小编为您整理了“C++多态特性例”,方便广大网友查阅!

string color;

double price;

};

class apple:public fruit

{

public:

apple();

void how_to_eat()const;

void show_priority()const;

};

class bnana:public fruit

{

public:

bnana();

void show_priority()const;

void how_to_eat()const;

};

fruit::fruit(string a,string b,double c):name(a),color(b),price(c){}

apple::apple():fruit("apple","red",1.23){}

bnana::bnana():fruit("bnana","yellow",0.34){}

void apple::how_to_eat()const

{

cout<<"直接吃"<<endl;

}

void apple::show_priority()const

{

cout<<name<<endl<<color<<endl<<price<<endl;

}

void bnana::how_to_eat()const

{

cout<<"去了皮再吃"<<endl;

}

void bnana::show_priority()const

{

cout<<name<<endl<<color<<endl<<price<<endl;

}

int main()

{

fruit *p[2]={NULL};

p[0]=new apple;

p[1]=new bnana;

for(int i=0;i<2;i++)

{

p[i]->show_priority();p[i]->how_to_eat();

}

return 0;

}

更多信息请查看IT技术专栏

更多信息请查看网络编程
点此处就本文及相关问题在本站进行非正式的简要咨询(便捷快速)】     【点此处查询各地各类考试咨询QQ号码及交流群
上一篇:Shell命令读取数据与输出数据
下一篇:Java正则表达式校验邮箱和手机号
易贤网手机网站地址:C++多态特性例
由于各方面情况的不断调整与变化,易贤网提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!