6-1 多态性与虚函数
来源:互联网 发布:淘宝流失金额怎么办 编辑:程序博客网 时间:2024/05/23 02:03
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!
#include <iostream>using namespace std;class Pet{ public: virtual void speak() { cout<<"How does a pet speak ?"<<endl; }};class Dog:public Pet{ public: void speak() { cout<<"wang!wang!"<<endl; }};class Cat:public Pet{ public: void speak() { cout<<"miao!miao!"<<endl; }};int main(){ Pet p; Cat c; Dog d; Pet *t; t=&p; t->speak(); t=&c; t->speak(); t=&d; t->speak(); 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 多态性与虚函数
- 多态性与虚函数
- Python 函数
- CSDN如何转载别人的文章
- 线程池创建的4种方式
- property和attribute的区别
- Python实现简单的HTTP服务器(支持文件下载)
- 6-1 多态性与虚函数
- SpringMvc 静态页面的访问
- 进程管理API之find_task_by_pid_ns
- mt6735 [Audio Common] 通话时如何播放声音给对方听
- JavaWeb笔记之Tomcat虚拟主机技术
- 技术分享连载(六十四)
- leetcode_41. First Missing Positive ? 待解决
- xml封装成对象(1-1)
- [转]Shadowsocks RCE分析