深入探讨C++父类子类中虚函数的应用
来源:本站原创|时间:2020-01-10|栏目:C语言|点击: 次
构造函数不能是虚函数,因为在调用构造函数创建对象时,构造函数必须是确定的,所以构造函数不能是虚函数。
析构函数可以是虚函数。
1.父类Father.h:
复制代码 代码如下:
#pragma once
class Father
{
public:
Father(void);
virtual ~Father(void);
virtual int getCount();
public:
int count;
};
Father.cpp
复制代码 代码如下:
#include "StdAfx.h"
#include "Father.h"
#include <iostream>
using namespace std;
Father::Father(void)
{
count = 1;
cout<<"Father is called. count = "<<count<<endl;
}
Father::~Father(void)
{
cout<<"~Father is called."<<endl;
}
int Father::getCount()
{
cout<<"Father::getCount() count = "<<count<<endl;
return count;
}
2.子类Child.h:
复制代码 代码如下:
#pragma once
#include "father.h"
class Child :
public Father
{
public:
Child(void);
virtual ~Child(void);
virtual int getCount();
int getAge();
public:
int age;
};
Child.cpp
复制代码 代码如下:
#include "StdAfx.h"
#include "Child.h"
#include <iostream>
using namespace std;
Child::Child(void)
{
count = 2;
age = 20;
cout<<"Child is called. count = "<<count<<", age = "<<age<<endl;
}
Child::~Child(void)
{
cout<<"~Child is called."<<endl;
}
int Child::getCount()
{
cout<<"Child::getCount() count = "<<count<<endl;
return count;
}
int Child::getAge()
{
cout<<"Child::getAge() age = "<<age<<endl;
return age;
}
3.测试类Test.cpp
复制代码 代码如下:
#include "stdafx.h"
#include <cstdlib>
#include <iostream>
#include "Child.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
Father *father1 = new Father();
cout<<"father1 count = "<<father1->getCount()<<endl;
delete father1;
cout<<"************** father1 end *****************"<<endl<<endl;
Father *father2 = new Child();
cout<<"father2 count = "<<father2->getCount()<<endl; // father2 don't have getAge() method
delete father2;
cout<<"************** father2 end *****************"<<endl<<endl;
Child *child = new Child();
cout<<"child count = "<<child->getCount()<<endl;
cout<<"child age = "<<child->getAge()<<endl;
delete child;
cout<<"************** child end *****************"<<endl<<endl;
getchar();
return 0;
}
4.输出结果:
Father is called. count = 1
Father::getCount() count = 1
father1 count = 1
~Father is called.
************** father1 end *****************
Father is called. count = 1
Child is called. count = 2, age = 20
Child::getCount() count = 2
father2 count = 2
~Child is called.
~Father is called.
************** father2 end *****************
Father is called. count = 1
Child is called. count = 2, age = 20
Child::getCount() count = 2
child count = 2
Child::getAge() age = 20
child age = 20
~Child is called.
~Father is called.
************** child end *****************
栏 目:C语言
下一篇:使用map实现单词转换的实例分析
本文标题:深入探讨C++父类子类中虚函数的应用
本文地址:https://www.xiuzhanwang.com/a1/Cyuyan/4433.html
您可能感兴趣的文章
- 04-02c语言没有round函数 round c语言
- 01-10深入理解约瑟夫环的数学优化方法
- 01-10深入二叉树两个结点的最低共同父结点的详解
- 01-10深入理解C++中常见的关键字含义
- 01-10使用C++实现全排列算法的方法详解
- 01-10深入Main函数中的参数argc,argv的使用详解
- 01-10深入第K大数问题以及算法概要的详解
- 01-10深入解析最长公共子串
- 01-10c++中inline的用法分析
- 01-10深入理解链表的各类操作详解
阅读排行
本栏相关
- 04-02c语言函数调用后清空内存 c语言调用
- 04-02func函数+在C语言 func函数在c语言中
- 04-02c语言的正则匹配函数 c语言正则表达
- 04-02c语言用函数写分段 用c语言表示分段
- 04-02c语言中对数函数的表达式 c语言中对
- 04-02c语言编写函数冒泡排序 c语言冒泡排
- 04-02c语言没有round函数 round c语言
- 04-02c语言分段函数怎么求 用c语言求分段
- 04-02C语言中怎么打出三角函数 c语言中怎
- 04-02c语言调用函数求fibo C语言调用函数求
随机阅读
- 01-10C#中split用法实例总结
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 04-02jquery与jsp,用jquery
- 01-10delphi制作wav文件的方法
- 01-10SublimeText编译C开发环境设置
- 01-11ajax实现页面的局部加载