6-1 多态性与虚函数
来源:互联网 发布:haoduofuli新域名 编辑:程序博客网 时间:2024/05/22 15:22
6-1 多态性与虚函数
Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic
Problem Description
通过本题目的练习可以掌握多态性的概念和虚函数的定义和使用方法
要求定义一个基类Pet,它有一个成员函数Speak()用于输出pet的叫声。;派生类Dog和Cat从基类Pet派生而来。他们从基类继承并重新改写了speak()函数,分别用于输出Dog类和Cat类的叫声。要求利用虚函数编写代码,使得程序能够输出下面的内容。
Input
无
Output
输出数据共有3行,本题目要求输出内容必须与示例中的相同。
Example Input
Example Output
How does a pet speak ?miao!miao!wang!wang!
Hint
Author
黄晶晶
#include<iostream>#include<algorithm>using namespace std;class Pet{public: virtual void speak()const=0;};class Cat:public Pet{public: virtual void speak()const { cout<<"miao!miao!"<<endl; }};class Dog:public Pet{public: virtual void speak()const { cout<<"wang!wang!"<<endl; }};void sspeack(Pet &p){ p.speak();}int main(){ ios::sync_with_stdio(false); cout<<"How does a pet speak ?"<<endl; Cat c; sspeack(c); Dog d; sspeack(d); return 0;}
0 0
- [2679]6-1 多态性与虚函数
- 6-1 多态性与虚函数
- 【2679】6-1 多态性与虚函数
- sdut 6-1 多态性与虚函数
- 6-1 多态性与虚函数
- 6-1 多态性与虚函数
- sdut 6-1 多态性与虚函数
- 6-1 多态性与虚函数
- 6-1 多态性与虚函数
- SDUT2679 6-1 多态性与虚函数
- 6-1 多态性与虚函数
- 6-1 多态性与虚函数
- 6-1 多态性与虚函数
- 6-1 多态性与虚函数
- 6-1 多态性与虚函数
- 6-1 多态性与虚函数
- 6-1 多态性与虚函数
- 多态性与虚函数
- HTML5学习_day01(3)--css样式引用方式
- 登录后,实现cookie信息和数据库信息的合并
- Android内核安全机制理解
- 说一说安卓如何快速搭建app架构
- 简单的Windows Webcam应用:Barcode Reader
- 6-1 多态性与虚函数
- memcached安装说明
- python数据质量分析小例子
- 7.Android基础:四大组件之----->ContentProvider
- 观察者设计模式
- 解决nginx+php(php-fpm)出现502 bad gateway问题
- 超Easy!!! Git入门
- 关于myeclipse10.7破解的问题(破解不成功显示只有五天可用可激活)
- 【web】js添加附件功能(显示进度条)——添加附件-demo01