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;
}