第14周程序阅读2(2)
来源:互联网 发布:matlab 矩阵变cell 编辑:程序博客网 时间:2024/05/16 20:30
/** Copyright (c)2013, 烟台大学计算机学院学生* All rightsreserved.* 文件名称: array.cpp* 作 者: 李中意* 完成日期:2014 年 5月26 日* 版本号: v1.0* 输入描述:无*/#include <iostream>using namespace std;class Mammal{public: Mammal( ){ cout<<"Mammal constructor called."<<endl; } virtual ~Mammal(){ cout<<"Mammal destructor called."<<endl; } virtual void Speak() const{ cout<<"Mammal speak."<<endl; }};class Dog : public Mammal{public: Dog(){ cout<<"Dog constructor called."<<endl; } ~Dog(){ cout<<"Dog destructor called."<<endl; } void Speak() const{ cout<<"Woof!"<<endl; }};int main(){ Mammal *p=new Dog; p->Speak(); delete p; return 0;}/*该程序运行时执行Dog的构造函数,但因为*p是基类的指针,所以第一行输出Mammal constructor called.又因为析构函数和Spenk函数都是虚函数,所以即使*p是基类的指针,但因为当基类的指针指向派生类时,用指针调用同名的虚成员函数时执行的是派生类的成员函数所以后两行输出的是Dog destructor called.和Woof!最后再执行析构函数,析构函数的执行顺序是先派生后基类。所以最后两行是Dog destructor called.和Mammal destructor called.*/
0 0
- 第14周程序阅读(2)
- 第14周-阅读程序(2)
- 第14周阅读程序(2)
- 第14周阅读程序(2)
- 第14周阅读程序(2)
- 第14周阅读程序(2)
- 第14周 阅读程序(2)
- 第14周程序阅读(2)
- 第14周程序阅读 例2
- 第2周阅读程序(2)
- 第13周程序阅读(2)
- 第2周阅读程序(1)
- 第13周-程序阅读(2)
- 第11周阅读程序(2)
- 第12周-阅读程序(2)
- 第12周阅读程序(2)
- 第13周-阅读程序(2)
- 第12周阅读程序(2)
- python2.5版本中实现简单的json模块
- 环形缓冲区,魔戒lordrings,boost的circular_buffer
- OCP 1Z0 051 129
- ZOJ-2386
- mysql explain的详解
- 第14周程序阅读2(2)
- 讲解游戏开发与项目下的hdpi 、mdpi与ldpi资源文件夹以及游戏高清版本的设置
- java参数传递机制浅析
- viewflipper实现幻灯片的简单播放
- python实现虎扑网站图片爬虫
- n&(n-1) 与 n&-n
- 深入理解DIP、IoC、DI以及IoC容器
- oracle SID详解
- struts2 ognl表达式详解