深入探讨C++父类子类中虚函数的应用
2019/7/10 23:26:19
本文主要是介绍深入探讨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 *****************
析构函数可以是虚函数。
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++父类子类中虚函数的应用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-29易优CMS安装常见问题汇总-icode9专业技术文章分享
- 2024-06-28易优新手必读安装教程-icode9专业技术文章分享
- 2024-06-28忘记eyoucms后台密码怎么办?-icode9专业技术文章分享
- 2024-06-26终极指南:Scrum中如何设置需求优先级
- 2024-06-26AI大模型企业应用实战(25)-为Langchain Agent添加记忆功能
- 2024-06-26小白家庭 nas 搭建方案-icode9专业技术文章分享
- 2024-06-23AI大模型企业应用实战(14)-langchain的Embedding
- 2024-06-23AI大模型企业应用实战(15)-langchain核心组件
- 2024-06-23AI大模型企业应用实战(16)-langchain核心组件
- 2024-06-23AI 大模型企业应用实战(06)-初识LangChain