虚表里面只有父类的虚函数。

#include<iostream>
using namespace std;

class Base{
public:
	Base():x(0){

	}
	~Base(){  //这个改成虚函数,字节大小不会变,因为只有一个vfptr,只会增加4个字节

	}
public:
	virtual void show(){
		cout<<"This is Base show()"<<endl;
	}
	virtual void print(){
		cout<<"This is Base print()"<<endl;
	}
	void fun(){
		cout<<"This is Base fun()"<<endl;
	}
private:
	int x;
};

class D : public Base{
public:
	D():y(0){

	}
	~D(){

	}
public:
	void show(){
		cout<<"This is D show()"<<endl;
	}
	void fun(){
		cout<<"This is D fun()"<<endl;
	}
	virtual void list(){
		cout<<"This is D list()"<<endl;
	}
private:
	int y;
};

void main(){
	D d;
	Base *pb = &d;
	pb->show();  //调用的是子类D
	pb->print();  //调用的是父类Base
	pb->fun();  //调用的是父类Base

	//pb->list();  //不能访问,通过父类的指针只能访问父类范围的所有函数
}

重载

针对一个类里面,函数同名但是参数不一样。

class Base{
public:
	Base():x(0){

	}
	~Base(){

	}
public:
	virtual void show(){
		cout<<"This is Base show()"<<endl;
	}
	virtual void show(int a){  //这两个函数构成了重载
		cout<<"This is Base show()"<<endl;
	}
	virtual void print(){
		cout<<"This is Base print()"<<endl;
	}
	void fun(){
		cout<<"This is Base fun()"<<endl;
	}
private:
	int x;
};

隐藏

子类方法隐藏了所有同名的父类方法。父类不能是虚方法。

class D : public Base{
public:
	D():y(0){

	}
	~D(){

	}
public:
	void show(){  //子类函数名和父类相同,就构成了隐藏,但是父类里面不能加virtual
		cout<<"This is D show()"<<endl;
	}
	void fun(){
		cout<<"This is D fun()"<<endl;
	}
	virtual void list(){
		cout<<"This is D list()"<<endl;
	}
private:
	int y;
};

覆盖

父类一定要是虚函数。即多态。

class Base{
public:
	Base():x(0){

	}
	~Base(){  //这个改成虚函数,字节大小不会变,因为只有一个vfptr,只会增加4个字节

	}
public:
	virtual void show(){
		cout<<"This is Base show()"<<endl;
	}
	void show(int a){  //这两个函数构成了重载
		cout<<"This is Base show()"<<endl;
	}
	virtual void print(){
		cout<<"This is Base print()"<<endl;
	}
	void fun(){
		cout<<"This is Base fun()"<<endl;
	}
private:
	int x;
};

分析

情况一:这个会生成一个虚表,包括6个地址。因为这里没有发生覆盖,子类的虚函数不会覆盖父类的虚函数,而是跟在父类的虚函数后面。

#include<iostream>
using namespace std;

class Base{
public:
	virtual void f(){
		cout<<"This is Base f"<<endl;
	}
	virtual void g(){
		cout<<"This is Base g"<<endl;
	}
	virtual void h(){
		cout<<"This is Base h"<<endl;
	}
};

class D : public Base{
public:
	virtual void f1(){
		cout<<"This is D f1"<<endl;
	}
	virtual void g1(){
		cout<<"This is D g1"<<endl;
	}
	virtual void h1(){
		cout<<"This is D h1"<<endl;
	}
};

void main(){
	D d;
}

情况二:发生子类 f 函数覆盖父类 f 函数。即虚表先是子类的 f 函数,然后是父类的两个虚函数 g 和 h ,再是子类的两个虚函数 g1 和 h1。

#include<iostream>
using namespace std;

class Base{
public:
	virtual void f(){
		cout<<"This is Base f"<<endl;
	}
	virtual void g(){
		cout<<"This is Base g"<<endl;
	}
	virtual void h(){
		cout<<"This is Base h"<<endl;
	}
};

class D : public Base{
public:
	virtual void f(){
		cout<<"This is D f"<<endl;
	}
	virtual void g1(){
		cout<<"This is D g1"<<endl;
	}
	virtual void h1(){
		cout<<"This is D h1"<<endl;
	}
};

void main(){
	D d;
}

情况三:多继承。

没有覆盖。其实会产生三个虚表,第一个虚表有父类 Base 的三个虚函数,和子类的三个虚函数,而其他两个虚表分别是不带子类的父类 Base1 和 Base2 。

#include<iostream>
using namespace std;

class Base{
public:
	virtual void f(){
		cout<<"This is Base f"<<endl;
	}
	virtual void g(){
		cout<<"This is Base g"<<endl;
	}
	virtual void h(){
		cout<<"This is Base h"<<endl;
	}
};

class Base1{
public:
	virtual void f(){
		cout<<"This is Base1 f"<<endl;
	}
	virtual void g(){
		cout<<"This is Base1 g"<<endl;
	}
	virtual void h(){
		cout<<"This is Base1 h"<<endl;
	}
};

class Base2{
public:
	virtual void f(){
		cout<<"This is Base2 f"<<endl;
	}
	virtual void g(){
		cout<<"This is Base2 g"<<endl;
	}
	virtual void h(){
		cout<<"This is Base2 h"<<endl;
	}
};

class D : public Base, public Base1, public Base2{
public:
	virtual void f1(){
		cout<<"This is D f1"<<endl;
	}
	virtual void g1(){
		cout<<"This is D g1"<<endl;
	}
	virtual void h1(){
		cout<<"This is D h1"<<endl;
	}
};

void main(){
	D d;
}

情况四:多继承。

有覆盖。输出都是子类D。因为子类覆盖了所有父类的同名函数。

#include<iostream>
using namespace std;

class Base{
public:
	virtual void f(){
		cout<<"This is Base f"<<endl;
	}
	virtual void g(){
		cout<<"This is Base g"<<endl;
	}
	virtual void h(){
		cout<<"This is Base h"<<endl;
	}
};

class Base1{
public:
	virtual void f(){
		cout<<"This is Base1 f"<<endl;
	}
	virtual void g(){
		cout<<"This is Base1 g"<<endl;
	}
	virtual void h(){
		cout<<"This is Base1 h"<<endl;
	}
};

class Base2{
public:
	virtual void f(){
		cout<<"This is Base2 f"<<endl;
	}
	virtual void g(){
		cout<<"This is Base2 g"<<endl;
	}
	virtual void h(){
		cout<<"This is Base2 h"<<endl;
	}
};

class D : public Base, public Base1, public Base2{
public:
	virtual void f(){
		cout<<"This is D f"<<endl;
	}
	virtual void g1(){
		cout<<"This is D g1"<<endl;
	}
	virtual void h1(){
		cout<<"This is D h1"<<endl;
	}
};

void main(){
	D d;
	Base *pb = &d;
	pb->f();

	Base1 *pb1 = &d;
	pb1->f();

	Base2 *pb2 = &d;
	pb2->f();
}

 版权声明:本文为博主原创文章,如有错误,恳请大家在评论区指出,在下不胜感激~如要转载注明出处即可~