继承 派生类构造过程 虚函数 隐藏 覆盖(重写) 虚析构函数

class A {
public:
	A(int a = 0)
		:ma(a)
	{}
private:
	int ma;
	static int mb;
};
int A::mb = 20;
int main() {
	A a;
	cout << sizeof(a) << endl;
	return 0;
}
class Base {
public:
	Base(int data)
		:ma(data)
	{
		cout << "Base(int data)" << endl;
	}
	~Base() { cout << "~Base()" << endl; }
protected:
	int ma;//想要派生类访问 不想要外部访问
};
class Derive :public Base {
public:
	Derive(int data)
		:mb(data)
		,Base(data)//ma(data) 不允许这样直接初始化
	{
		cout << "Derive(int data)" << endl;
	}
	~Derive() { cout << "~Derive()" << endl; }
private:
	int mb;
};
int main() {
	Derive d(20);
	return 0;
}
class Base {
public:
	Base(int data = 10)
		:ma(data)
	{}
	void show() { cout << "void Base::show() " << endl; }
	void show(int) { cout << "void Base::show(int)" << endl; }
protected:
	int ma;
};
class Derive :public Base {
public:
	Derive(int data=20)
		:Base(data)//调用基类的构造函数初始化基类继承过来的成员变量
		,mb(data)
	{}
	void show() { cout << "void Derive::show()" << endl; }
	//show将基类的show()和show(int)隐藏了
private:
	int mb;
};
int main() {
	Derive d;
	d.show();
	//d.show(10);//“Derive::show”: 函数不接受 1 个参数
	//基类的show(int)被隐藏了
	d.Base::show(10);
	Base b;
	b = d;//从下(派生类)到上(基类)OK
	//d = b;//从上(基类)到下(派生类)NO
	Base* pb = &d;
	pb->show();
	pb->show(10);
	((Derive*)pb)->show();
	//Derive* pd = &b;
	Derive* pd = (Derive*)&b;//不安全存在内存非法访问
	//pd->show();//不安全
	return 0;
}
class Base {
public:
	Base(int data = 10)
	:ma(data){}
	void show() { cout << "void Base::show()" << endl; }
	void show(int) { cout << "void Base::show(int)" << endl; }
protected:
	int ma;
};
class Derive :public Base {
public:
	Derive(int data = 20)
		:Base(data)
		,mb(data)
	{}
	void show() { cout << "void Derive::show()" << endl; }
private:
	int mb;
};
int main() {
	Derive d(20);
	Base* pb = &d;
	pb->show();//静态绑定 编译时期call Base::show()
	pb->show(10);//静态绑定 编译时期 call Base::show(10)
	cout << sizeof(Base) << endl;
	cout << sizeof(Derive) << endl;
	cout << typeid(pb).name() << endl;
	cout << typeid(*pb).name() << endl;
	return 0;
}
#include <typeinfo>
class Base {
public:
	Base(int data = 10)
		:ma(data)
	{}
	virtual void show() { cout << "void Base::show()" << endl; }
	virtual void show(int) { cout << "void show(int )" << endl; }
protected:
	int ma;
};
class Derive :public Base {
public:
	Derive(int data = 20)
		:Base(data)//ma(data)不允许
		,mb(data)
	{}
	//virtual void show() 重写 覆盖
	void show() { cout << "void Derive::show()" << endl; }
private:
	int mb;
};
int main() {
	Derive d(20);
	Base* pb = &d;
	/*
	pb->Base Base::show如果发现show()是普通函数 就进行静态绑定
	如果发现show()是虚函数 就进行动态绑定
	mov eax ,dword ptr[pb]
	mov ecx, dword ptr[eax]
	call ecx(虚函数地址)动态(运行时期)绑定(函数调用)
	*/
	pb->show();
	/*
	pb->Base Base::show如果发现show(int)是普通函数 进行静态绑定
	如果发现show()是虚函数 就进行动态绑定
	mov eax,dword ptr[pb]
	mov ecx,dword ptr[eax]
	call ecx(虚函数地址)动态(运行时期)绑定(函数调用)
	*/
	pb->show(30);
	cout << sizeof(Base) << endl;
	cout << sizeof(Derive) << endl;
	cout << typeid(pb).name() << endl;
	/*
	*p类型的判定:Base有没有虚函数
	Base没有选虚函数 *pb的类型就是编译时期的类型
	*p<<==>>Base类型
	如果Base有虚函数 *pb的类型就是运行时期的RTTI类型
	pb->d(vfptr)->Derive vftable class Derive
	*/
	cout << typeid(*pb).name() << endl;
	return 0;
}
class Base {
public:
	Base(int data=10)
		:ma(data)
	{
		cout << "Base(int data=10)" << endl;
	}
	virtual void show() { cout << "void Base::show()" << endl; }
	virtual ~Base() { cout << "~Base()" << endl; }
protected:
	int ma;
};

class Derive :public Base {
public:
	Derive(int data=10)
		:Base(data)
		,mb(data)
		,ptr(new int[data])
	{
		cout << "Derive(int data=10)" << endl;
	}
	void show() { cout << "void Derive::show()" << endl; }
	~Derive() { delete[] ptr; cout << "~Derive()" << endl; }
private:
	int mb;
	int* ptr;
};
int main() {
	/*Derive d(20);
	Base* p = &d;
	p->show();*/
	Base* pb = new Derive(20);
	pb->show();
	delete pb;
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yyycqupt

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值