C++中虚函数的实例
2009-06-01 23:47
113 查看
/*
* File: main.cpp
* Author: yubao
*
* Created on June 1, 2009, 11:39 PM
*/
#include <iostream>
using namespace std;
class B0
{
public:
virtual void display()
{
cout<<"B0::display()"<<endl;
}
};
class B1:public B0
{
public:
void display()
{
cout<<"B1::diaplay()"<<endl;
}
};
class D1:public B1
{
public:
void display()
{
cout<<"D1::display()"<<endl;
}
};
void fun(B0 *ptr)
{
ptr->display();
}
/*
*
*/
int main(int argc, char** argv) {
B0 b0, *p;
B1 b1;
D1 d1;
p=&b0;
fun(p);
p=&b1;
fun(p);
p=&d1;
fun(p);
return 0;
}
* File: main.cpp
* Author: yubao
*
* Created on June 1, 2009, 11:39 PM
*/
#include <iostream>
using namespace std;
class B0
{
public:
virtual void display()
{
cout<<"B0::display()"<<endl;
}
};
class B1:public B0
{
public:
void display()
{
cout<<"B1::diaplay()"<<endl;
}
};
class D1:public B1
{
public:
void display()
{
cout<<"D1::display()"<<endl;
}
};
void fun(B0 *ptr)
{
ptr->display();
}
/*
*
*/
int main(int argc, char** argv) {
B0 b0, *p;
B1 b1;
D1 d1;
p=&b0;
fun(p);
p=&b1;
fun(p);
p=&d1;
fun(p);
return 0;
}
相关文章推荐
- C++中函数模板的实例
- php之readdir函数用法实例
- C++默认编写并调用了那些函数(构造,析构,赋值)!!!
- php之readdir函数用法实例
- php中simplexml_load_file函数用法实例
- C++ Template学习笔记之函数模板(6)——模板显式特化
- SQL查询排名函数实例
- sscanf()函数的使用及其实例
- pig函数以及关键字 的一些实例应用的总结(来自pig笔记)
- Linux信号:sigaction函数sa_flags各标志影响的实例讲解
- Json 自定义使用函数的简单实例
- PHP数组函数array_multisort()用法实例分析
- (Oracle)存储过程、存储函数和包的相关知识与实例
- python中操作文件函数open的简单操作实例
- jQuery 语法实例--文档就绪函数
- ASP 中 Split 函数的实例
- Javascript图像处理―阈值函数实例应用
- Javascript回调函数和匿名函数实例
- 使用scatter与random.normal函数绘制散点图简单实例
- calloc()函数实例