深入探讨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++ 中extern关键字详解

    C/C++ 中extern关键字详解

    这篇文章主要介绍了C/C++ 中extern关键字详解的相关资料,需要的朋友可以参考下
    2017-06-06
  • C++利用多态实现职工管理系统(项目开发)

    C++利用多态实现职工管理系统(项目开发)

    这篇文章主要介绍了C++利用多态实现职工管理系统(项目开发),本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-01-01
  • C++实现四则运算器(无括号)

    C++实现四则运算器(无括号)

    这篇文章主要为大家详细介绍了C++实现四则运算器,无括号的计算器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-11-11
  • C++ 单链表的基本操作(详解)

    C++ 单链表的基本操作(详解)

    下面小编就为大家带来一篇C++ 单链表的基本操作(详解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-12-12
  • 使用UDP协议实现单词翻译服务器

    使用UDP协议实现单词翻译服务器

    这篇文章主要为大家详细介绍了如何使用UDP协议实现英文单词翻译服务器,文中的示例代码讲解详细,具有一定的学习价值,感兴趣的小伙伴可以了解下
    2023-08-08
  • 教你使用Matlab制作图形验证码生成器(app designer)

    教你使用Matlab制作图形验证码生成器(app designer)

    这篇文章主要和大家分享如何利用Matlab制作一款图形验证码生成器,文中的实现步骤讲解详细,感兴趣的小伙伴可以跟随小编动手试一试
    2022-02-02
  • boost.asio框架系列之定时器Timer

    boost.asio框架系列之定时器Timer

    这篇文章介绍了boost.asio框架系列之定时器Timer,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-06-06
  • 运用指针在不用加号的情况进行加法运算的讲解

    运用指针在不用加号的情况进行加法运算的讲解

    今天小编就为大家分享一篇关于运用指针在不用加号的情况进行加法运算的讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-01-01
  • 使用C语言打印月历

    使用C语言打印月历

    这篇文章主要为大家详细介绍了使用C语言打印月历,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-06-06
  • C语言实现求解素数的N种方法总结

    C语言实现求解素数的N种方法总结

    哈喽各位友友们,今天又学到了很多有趣的知识,现在迫不及待的想和大家分享一下!本文将手把手带领大家探讨利用试除法、筛选法求解素数的n层境界!都是精华内容,可不要错过哟
    2023-01-01

最新评论