【C++】虚继承内存分配验证

子类内存分配

一.证明虚继承的子对象按上表分配成员:

测试1:

#include <iostream>
using namespace std;

typedef void(*func_t)(void);
#define NON_STATIC_DATA_NUM 2	 //继承的和自己的
#define VIRTUAL_SELF_FUNC_NUM 4	 //继承的虚函数与自己的任何函数总量
class Father {
public:
	Father() :x(100), y(200) {}
	virtual void func1() {
		cout << __FUNCTION__ << endl;
	}
	virtual void func2() {
		cout << __FUNCTION__ << endl;
	}
	virtual void func3() {
		cout << __FUNCTION__ << endl;
	}

	static int z;
	int x = 300;
	int y = 400;
};

class Mother {
public:
	Mother() :m(300), n(400) {}
	virtual void handle1() {
		cout << __FUNCTION__ << endl;
	}
	void handle2() {
		cout << __FUNCTION__ << endl;
	}
	void handle3() {
		cout << __FUNCTION__ << endl;
	}
public:
	int m = 100;
	int n = 200;
};

class Son :public Mother, public Father {
public:
	Son() :s(500) {}
	void son() {
		cout << __FUNCTION__ << endl;
	}
	int s = 500;
};

int main() {
	Son father;
	int* vptr = (int*)*(int*)(&father);
	//证明:对象中最先存储vptr
	cout << "对象地址:" << (int*)&father << "中存的是虚函数指针地址:" << vptr << endl;

	((func_t) * ((int*)*(int*)&father + 0))();
	((func_t) * ((int*)*(int*)&father + 1))();
	((func_t) * ((int*)*(int*)&father + 2))();

	//通过vptr访问所有虚函数=>vptr指向虚函数表首地址
	for (int i = 0; i < VIRTUAL_SELF_FUNC_NUM; i++) {
		((func_t) * ((int*)*(int*)&f + i))();
	}

	//通过对象地址访问对象的三个数据成员地址和值(静态数据成员存储在类中,不能通过对象地址查看)
	for (int i = 0; i < NON_STATIC_DATA_NUM; i++) {
		cout << "第" << i + 1 << "个非静态数据成员的十六进制地址:" << hex << int(&f) + 4 + 4 * i << " = " << &(f.m) << endl;
	}

	cout << "及值:" << dec << *(int*)(int(&f) + 4 * 1) << " = " << f.m << endl;
	cout << "及值:" << dec << *(int*)(int(&f) + 4 * 2) << " = " << f.n << endl;
	cout << "及值:" << dec << *(int*)(int(&f) + 4 * 3) << " = " << f.x << endl;
	cout << "及值:" << dec << *(int*)(int(&f) + 4 * 4) << " = " << f.y << endl;
	cout << "及值:" << dec << *(int*)(int(&f) + 4 * 5) << " = " << f.s << endl;

	//通过

	system("pause");
	return 0;
}

测试2:

#include <iostream>
using namespace std;

class Father {
public:
	virtual void func1() { cout << "Father::func1" << endl; }
	virtual void func2() { cout << "Father::func2" << endl; }
	virtual void func3() { cout << "Father::func3" << endl; }
	void func4() { cout << "非虚函数:Father::func4" << endl; }
public:  //为了便于测试main函数调取数据成员,特别改用public
	int x = 100;
	int y = 200;
	static int z;
};
int Father::z = 0;
class Mother {
public:
	virtual void handle1() { cout << __FUNCTION__ << endl; }
	void handle2() { cout << __FUNCTION__ << endl; }
	void handle3() { cout << __FUNCTION__ << endl; }

};
class Son :public Father, public Mother {
public:
	void func1() { cout << "Son::func1" << endl; }
	void handle1() { cout << __FUNCTION__ << endl; }
	virtual void boy() { cout << __FUNCTION__ << endl; }
	int m = 400;
	int n = 500;
};
typedef void (*func_t)(void);

int main(void) {
	Father father;

	// 含有虚函数的对象的内存中,最先存储的就是“虚函数表”
	cout << "对象地址:" << (int*)&father << endl;

	int* vptr = (int*)*(int*)&father;
	cout << "虚函数表指针vptr:" << vptr << endl;

	cout << "调用第1个虚函数: ";
	((func_t) * (vptr + 0))();

	cout << "调用第2个虚函数:";
	((func_t) * (vptr + 1))();

	cout << "调用第3个虚函数: ";
	((func_t) * (vptr + 2))();


	cout << "第1个数据成员的地址: " << endl;
	cout << &father.x << endl;
	cout << std::hex << (int)&father + 4 << endl;//位于虚函数表之战之后
	cout << "第1个数据成员的值:" << endl;
	cout << std::dec << father.x << endl;
	cout << *(int*)((int)&father + 4) << endl;

	cout << "第2个数据成员的地址: " << endl;
	cout << &father.y << endl;
	cout << std::hex << (int)&father + 8 << endl;
	cout << "第2个数据成员的值:" << endl;
	cout << std::dec << father.y << endl;
	cout << *(int*)((int)&father + 8) << endl;

	cout << "sizeof(father)==" << sizeof(father) << endl;

	Father father2;
	cout << "father的虚函数表:";
	cout << *(int*)(*(int*)&father) << endl;
	cout << *vptr << endl;
	cout << "father2的虚函数表:";
	cout << *(int*)(*(int*)&father2) << endl;

	//②使用继承的虚函数表
	Son son;
	int* vptr2 = (int*)*(int*)&son;
	//cout << "第一个虚函数表指针:" << vptr2 << endl;

	for (int i = 0; i < 4; i++) {
		cout << "调用第" << i + 1 << "个虚函数:";
		((func_t) * (vptr2 + i))();
	}

	for (int i = 0; i < 2; i++) {
		cout << *(int*)((int)&son + 4 + i * 4) << endl;
	}

	int* vptr3 = (int*)*((int*)&son + 3);
	for (int i = 0; i < 1; i++) {
		cout << "调用第" << i + 1 << "个虚函数:";
		((func_t) * (vptr3 + i))();
	}

	for (int i = 0; i < 2; i++) {
		cout << *(int*)((int)&son + 16 + i * 4) << endl;
	}

	return 0;
}

二.final与override用法

final终结版

override强调重写

#include <iostream>
using namespace std;
class BillGates final {
public:
	void assest() {
		cout << "我死后所有遗产捐献,不留给子代!" << endl;
	}
};
class Dad {
public:
	void eat() {
		cout << "爸比在吃饭" << endl;
	}
};
class Mom {
public:
	virtual void faceScore() {
		cout << "麻麻优雅端庄" << endl;
	}
	virtual void rules() final {
		cout << "家规:善良,无悔" << endl;
	}
};
class Me :public Dad, public Mom{
public:
	//void rules();不能再写!!
	void faceScore() override {//virtual函数顶替时可调自己的!子类的virtual不必写,写了的话自己的子类也可以修改
		cout << "我火辣辣" << endl;
	}
	void eat() {//普通函数可以再写!只是顶替时不能调用
		cout << "我在吃饭饭~" << endl;
	}
};
class Daughter :public Me {
public:
	void faceScore() override {
		cout << "女儿表示很娇柔" << endl;
	}
};
void party(Dad& dad, Mom& mom, Me& me) {
	dad.eat();
	mom.faceScore();
	me.faceScore();
}
int main() {
	Me me;
	Daughter daughter;
	party(me, daughter, daughter);//女儿表示很娇柔
	//只要同名函数一个是virtual了,那么继承的所有同名函数都是virtual都是自由的!

	return 0;
}

三.纯虚函数:

#include <iostream>
using namespace std;

class Shape {
public:
	Shape(string color = "WHITE") {
		this->color = color;
	}
	string getColor() { //公用功能,取得各自数据
		return color; 
	}
	virtual int area() = 0;//仅虚拟函数允许纯说明符,且纯说明符仅允许=0

private:
	string color;
};

class Circle:public Shape {
public:
	Circle(int radius = 0, string color = "WHITE") :Shape(color), radius(radius) {}//相当于this->radius = radius;

	int area() { //纯虚类在子类中实现这个功能
		return 3.14 * radius * radius; 
	}

private:
	int radius;
};
int main() {
	Circle c;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值