深入探讨C++父类子类中虚函数的应用

 更新时间:2013年05月28日 12:07:32   作者:  
本篇文章是对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++实践排序函数模板项目的参考方法

    C++实践排序函数模板项目的参考方法

    今天小编就为大家分享一篇关于C++实践排序函数模板项目的参考方法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-02-02
  • C语言算法练习之数组元素排序

    C语言算法练习之数组元素排序

    这篇文章主要为大家介绍了C语言算法练习中数组元素排序的实现方法,文中的示例代码讲解详细,对我们学习C语言有一定帮助,需要的可以参考一下
    2022-09-09
  • C语言函数栈帧的创建和销毁介绍

    C语言函数栈帧的创建和销毁介绍

    大家好,本篇文章主要讲的是C语言函数栈帧的创建和销毁介绍,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下
    2021-12-12
  • C语言实现经典扫雷小游戏完整代码(递归展开 + 选择标记)

    C语言实现经典扫雷小游戏完整代码(递归展开 + 选择标记)

    这篇文章主要介绍了C语言小项目之扫雷游戏带递归展开 + 选择标记效果,本代码中,我们用字符 ! 来标识雷,文中附有完整代码,需要的朋友可以参考下
    2022-05-05
  • C语言之实现栈的基础创建

    C语言之实现栈的基础创建

    这篇文章主要介绍了C语言之实现栈的基础创建,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-07-07
  • 基于c语言知识点的补遗介绍

    基于c语言知识点的补遗介绍

    本篇文章是对c语言知识点的一些补遗进行详细的分析介绍,需要的朋友参考下
    2013-05-05
  • C++ namespace案例详解

    C++ namespace案例详解

    这篇文章主要介绍了C++ namespace案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • C语言的字符函数和字符串函数详解

    C语言的字符函数和字符串函数详解

    这篇文章主要为大家详细介绍了C语言的字符函数和字符串函数,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-03-03
  • c语言多进程tcp服务器示例

    c语言多进程tcp服务器示例

    这篇文章主要介绍了c语言多进程tcp服务器示例,多进程socket,epoll实现IO复用,需要的朋友可以参考下
    2014-03-03
  • C语言goto语句简单使用详解

    C语言goto语句简单使用详解

    C语言中提供了可以随意滥用的 goto语句和标记跳转的标号,本文主要介绍了C语言goto语句简单使用详解,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01

最新评论