虚函数

本文详细介绍了C++中的虚函数及其在实现多态机制中的作用。通过基类指针调用子类重写的虚函数,展示了多态的概念。示例代码中,基类`HUMAN`和派生类`MAN`都包含虚函数`Show()`. 当`HUMAN`的指针指向`MAN`对象时,调用`Show()`将执行`MAN`的版本,体现了多态性。若去掉虚函数声明,则会调用指针类型对应类的函数,强调了虚函数的重要性。
摘要由CSDN通过智能技术生成

C++中的虚函数的作用主要是实现了多态的机制。关于多态,简而言之就是用父类型别的指针指向其子类的实例,然后通过父类的指针调用实际子类的成员函数。这种技术可以让父类的指针有“多种形态”

经常在基类中,将派生类重写基类的方法 定义为虚函数

方法在基类中声明为虚函数之后,那么他会自动在派生类中声明这个方法,当然在派生类中显示声明,是一个更好的选择

例子

①基类 HUMAN
virtual void Show(void) const;

class HUMAN
{
private:
	int Age;
	int Height;
	string Name;

public:
	HUMAN(int age, int height, string & name);
	HUMAN(const HUMAN & Temp);
	~HUMAN(void);
	void SetAge(const int age);
	void SetHeight(const int height);
	void SetName(const string &Temp);
	virtual void Show(void) const;

	int GetAge(void) const;
	int GetHeight(void) const;
	string & GetName(void);

};

②派生类
virtual void Show(void) const;

class MAN : public HUMAN
{
private:
	bool Tall;
	bool Strong;
	bool Wealth;

public:
	
	MAN(bool tall, bool strong, bool wealth,
	int age, int height, string & name);
	MAN(bool tall, bool strong, bool wealth,
	 const HUMAN &Temp);
	~MAN(void);

	void SetTall(bool tall);
	void SetStrong(bool strong);
	void SetWealth(bool wealth);
	virtual void Show(void) const;

	bool GetTall(void) const;
	bool GetStrong(void) const;
	bool GetWealth(void) const;
};

③implement.cpp

void HUMAN::Show(void) const
{
	cout<<" Age: "<<Age<<" Height :"
	<<Height<<" Name: "<<Name<<endl;
}

void MAN::Show(void) const
{
	cout<<" Tall: "<<Tall<<" Strong :"<<Strong
	<<" Wealth: "<<Wealth<<endl;
}

定义完虚函数以后,程序将根据对象类型 来执行方法

#include "implement.h"
#include <string>

int main(int argc, char* argv[])
{
	string Input;
	cin>>Input;

	MAN A(true, true, true, 18, 180, Input);
	HUMAN *p = &A;
	(*p).Show();


	return 0;
}

在这里插入图片描述
结果说明:父类HUMAN的指针 指向了 子类MAN的对象,因为为虚函数,执行子类对象的方法,实现了多态。

如果去掉了虚函数virtual的声明

void Show(void) const;

在这里插入图片描述
则调用了HUMAN的show方法
说明: 未加虚函数,则会根据指针或者引用的类型,来调用方法。

完整代码

implement.h

#ifndef __IMPLEMENT_H__
#define __IMPLEMENT_H__

#include <string>

class HUMAN
{
private:
	int Age;
	int Height;
	string Name;

public:
	HUMAN(int age, int height, string & name);
	HUMAN(const HUMAN & Temp);
	~HUMAN(void);
	void SetAge(const int age);
	void SetHeight(const int height);
	void SetName(const string &Temp);
	virtual void Show(void) const;

	int GetAge(void) const;
	int GetHeight(void) const;
	string & GetName(void);

};

class MAN : public HUMAN
{
private:
	bool Tall;
	bool Strong;
	bool Wealth;

public:
	
	MAN(bool tall, bool strong, bool wealth,
	int age, int height, string & name);
	MAN(bool tall, bool strong, bool wealth, const HUMAN &Temp);
	~MAN(void);

	void SetTall(bool tall);
	void SetStrong(bool strong);
	void SetWealth(bool wealth);
	virtual void Show(void) const;

	bool GetTall(void) const;
	bool GetStrong(void) const;
	bool GetWealth(void) const;
};

#endif

implement.cpp

#include "stdafx.h"
#include "implement.h"


HUMAN::HUMAN(int age, int height, string & name)
{
	cout<<"基类三个参数的构造函数"<<endl;
	Age = age; Height = height; Name = name;
}
HUMAN::HUMAN(const HUMAN & Temp)
{
	cout<<"基类复制构造函数"<<endl;
	Age = Temp.Age; Height = Temp.Height; Name = Temp.Name;
}
HUMAN::~HUMAN(void)
{
	cout<<"基类析构函数"<<endl;
}


void HUMAN::SetAge(const int age)
{
	Age = age;
}
void HUMAN::SetHeight(const int height)
{
	Height = height;
}
void HUMAN::SetName(const string &Temp)
{
	Name = Temp;
}

int HUMAN::GetAge(void) const
{
	return Age;
}
int HUMAN::GetHeight(void) const
{
	return Height;
}
string & HUMAN::GetName(void) 
{
	return Name;
}

void HUMAN::Show(void) const
{
	cout<<" Age: "<<Age<<" Height :"<<Height<<" Name: "<<Name<<endl;
}



MAN::MAN(bool tall, bool strong, bool wealth,
int age, int height, string & name):HUMAN(age, height, name)
{
	cout<<"派生类MAN六个参数的构造函数"<<endl;
	Tall = tall; Strong = strong; Wealth = wealth;
}
MAN::MAN(bool tall, bool strong, bool wealth, 
const HUMAN &Temp):HUMAN(Temp)
{
	cout<<"派生类MAN四个参数的构造函数"<<endl;
	Tall = tall; Strong = strong; Wealth = wealth;
}

MAN::~MAN(void)
{
	cout<<"派生类MAN析构函数"<<endl;
}
void MAN::SetTall(bool tall)
{
	Tall = tall;
}
void MAN::SetStrong(bool strong)
{
	Strong = strong;
}
void MAN::SetWealth(bool wealth)
{
	Wealth = wealth;
}

bool MAN::GetTall(void) const
{
	return Tall;
}
bool MAN::GetStrong(void) const
{
	return Strong;
}
bool MAN::GetWealth(void) const
{
	return Wealth;
}
void MAN::Show(void) const
{
	cout<<" Tall: "<<Tall<<" Strong :"<<Strong<<" Wealth: "<<Wealth<<endl;
}

main.cpp

#include "implement.h"
#include <string>

int main(int argc, char* argv[])
{
	string Input;
	cin>>Input;

	MAN A(true, true, true, 18, 180, Input);
	HUMAN &p = A;
	(p).Show();


	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值