子类和基类对象函数调用过程中vitual关键字的作用
感觉有些文档写得很不明确,简要总结一下
基类对象调用函数
1、如果没有virtual函数修饰
直接调用基类的函数
2、如果有virtual函数修饰
1>、如果是是通过将子类的指针或者引用传给基类的指针或者引用,例如pBase = pDerive 或者 Base &base = derive,
根据实际情况调用
I、如果子类重写了这个函数,那么执行子类的函数
II、如果子类没有重写这个函数,那么执行基类的函数
2>、如果不是第一种情况,那么执行基类的对应的函数
子类对象调用函数
1、如果子类重写了某个基类的函数
直接调用子类的这个成员函数
2、如果子类没有重写这个基类的函数
调用基类对应的函数
对应函数代码
Base.h
#pragma once
#include <iostream>
using namespace std;
class Base
{
public:
Base(void);
~Base(void);
/*
基类对象调用函数
1、如果没有virtual函数修饰
直接调用基类的函数
2、如果有virtual函数修饰
1>、如果是是通过将子类的指针或者引用传给基类的指针或者引用,例如pBase = pDerive 或者 Base &base = derive,
根据实际情况调用
I、如果子类重写了这个函数,那么执行子类的函数
II、如果子类没有重写这个函数,那么执行基类的函数
2>、如果不是第一种情况,那么执行基类的对应的函数
子类对象调用函数
1、如果子类重写了某个基类的函数
直接调用子类的这个成员函数
2、如果子类没有重写这个基类的函数
调用基类对应的函数
*/
void printMsg() // 子类重写了
{
cout<<"Base Class"<<endl;
}
void display() // 子类么有重写
{
cout<<"Base display"<<endl;
}
virtual void showMsg()
{
cout<<"Base showMsg"<<endl; // 子类有重写
}
virtual void childNotHaveVir() // 子类无重写
{
cout<<"Base childNotHaveVir"<<endl;
}
};
Base.cpp
#include "Base.h"
Base::Base(void)
{
}
Base::~Base(void)
{
}
Derive.h
#pragma once
#include <iostream>
#include "Base.h"
using namespace std;
class Derive : public Base
{
public:
Derive(void);
~Derive(void);
virtual void printMsg()
{
cout<<"Derive Class"<<endl;
}
//void printMsg()
//{
// //cout<<"not virtual derived Class"<<endl;
//}
void showMsg()
{
cout<<"Derive showMsg"<<endl;
}
};
Derive.cpp
#include "Derive.h"
Derive::Derive(void)
{
}
Derive::~Derive(void)
{
}
主函数
#include <iostream>
#include <stdlib.h>
#include <string>
#include "Base.h"
#include "Derive.h"
using namespace std;
void printMenu();
void main()
{
while(1)
{
printMenu();
int choice = 0;
cout<<"请选择";
cin >> choice;
cin.clear();
switch(choice)
{
case 0:
{
exit(0);
}
break;
case 1:
{
cout<<"函数名称\t子类有无\t是否为virtual"<<endl;
cout<<"printMsg\t有\t不是 "<<endl;
cout<<"display\t无\t不是 "<<endl;
cout<<"showMsg\t有\t是 "<<endl;
cout<<"childNotHaveVir\t无\t是 "<<endl;
Base base, *pBase;
Derive derive, *pDerive;
pBase = new Base;
pDerive = new Derive;
cout<<"base.printMsg()\t";
base.printMsg();
cout<<"base.display()\t";
base.printMsg();
cout<<"base.showMsg()\t";
base.showMsg();
cout<<"base.childNotHaveVir()\t";
base.childNotHaveVir();
cout<<"pBase->printMsg()\t";
pBase->printMsg();
cout<<"pBase->display()\t";
pBase->display();
cout<<"pBase->showMsg()\t";
pBase->showMsg();
cout<<"pBase->childNotHaveVir()\t";
pBase->childNotHaveVir();
cout<<"derive.printMsg()\t";
derive.printMsg();
cout<<"derive.display()\t";
derive.display();
cout<<"derive.showMsg()\t";
derive.showMsg();
cout<<"derive.childNotHaveVir()\t";
derive.childNotHaveVir();
cout<<"pDerive->printMsg()\t";
pDerive->printMsg();
cout<<"pDerive->display()\t";
pDerive->display();
cout<<"pDerive->showMsg()\t";
pDerive->showMsg();
cout<<"pDerive->childNotHaveVir()\t";
pDerive->childNotHaveVir();
Base notRefBase = derive;
cout<<"notRefBase.printMsg()\t";
notRefBase.printMsg();
cout<<"notRefBase.display()\t";
notRefBase.display();
cout<<"notRefBase.showMsg()\t";
notRefBase.showMsg();
cout<<"notRefBase.childNotHaveVir()\t";
notRefBase.childNotHaveVir();
Base &refBase = derive;
cout<<"refBase.printMsg()\t";
refBase.printMsg();
cout<<"refBase.display()\t";
refBase.display();
cout<<"refBase.showMsg()\t";
refBase.showMsg();
cout<<"refBase.childNotHaveVir()\t";
refBase.childNotHaveVir();
pBase = &derive;
cout<<"pBase->printMsg()\t";
pBase->printMsg();
cout<<"pBase->display()\t";
pBase->display();
cout<<"pBase->showMsg()\t";
pBase->showMsg();
cout<<"pBase->childNotHaveVir()\t";
pBase->childNotHaveVir();
}
break;
default:
{
cout<<"输入有误,请重新输入"<<endl;
}
break;
}
}
system("pause");
}
void printMenu()
{
cout<<"[0] 退出"<<endl;
cout<<"[1] 继承virtual用法"<<endl;
}
大家可以用上面的代码验证一下