#include"A.h"#include<iostream>usingnamespace std;void A::show(int a){
cout <<"A::show(int a) = "<< a << endl;}void A::show(int a,int b){
cout <<" A::show(int a, int b) = "<< a << endl;}
#include"A.h"classB:public A {public:voidshow(int a);voidvirtualshow(int a1,int b1);};
#include"B.h"#ifndef _B_H_#define _B_H_#include<iostream>usingnamespace std;void B::show(int a){
cout <<"B::show(int a) = "<< a << endl;}void B::show(int a,int b){
cout <<" B::show(int a, int b) = "<< a << endl;}#endif
#include"A.h"#include"B.h"/*重写(覆盖):是指派生类中存在重新定义的函数。其函数名,参数列表,返回值类型,所有都必须同基类中被重写的函数一致。只有函数体不同(花括号内),派生类调用时会调用派生类的重写函数,不会调用被重写函数。重写的基类中被重写的函数必须有virtual修饰。*/voidfun(){
B b;
A *p =&b;//基类保存子类地址,若出现重写,根据保存对象来调用
p->show(1,2);
b.show(3,4);}//当父类的指针指向子类的对象的时候,我们若是想要//根据不同的对象类调用不同的同名函数的话,我们需要//把同名函数设置为虚函数.intmain(){fun();return0;}