C++ 继承深入理解(一)

  1. 继承:子类继承父类,子类的对象可以直接访问父类公有保护成员

      继承最大特点提高代码重用,也体现代码的共性与个性
      派生类:继承父类的子类
      基类:被继承类(父类)
      继承语法:
      派生类 : [继承方式] 基类

      继承方式:
       1.private:私有继承.默认为此继承方式.不能继承基类的私有成员.
            继承基类公有成员,在派生类中相当于是私有成员.
            继承基类保护成员,在派生类中相当于是私有成员.

       2.public:公有继承.不能继承基类的私有成员.
            继承基类公有成员,在派生类中相当于是公有成员.
            继承基类保护成员,在派生类中相当于是公有成员.

       3.protected:保护继承,不能继承基类的私有成员.
             继承基类公有成员,在派生类中相当于是保护成员.
             继承基类保护成员,在派生类中相当于是保护成员.

  • 继承基础验证
#include  <iostream>
#include  <stdio.h>
using namespace std;
class Base
{
public:
	void fun()
	{
		cout<<"Base fun"<<endl;
	}
};

//私有继承
class Child : Base
{
private:
	//被私有继承时,基类的方法相当于是如下定义形式
	// void fun()
	// {
	// 	cout<<"Base fun"<<endl;
	// }
};
//公有继承方式
class Child1 : public Base
{

};
int main(int argc, char const *argv[])
{
	Base b1;
	b1.fun();

	Child1 c;
	c.fun();//基类里面的公有fun函数被继承啦
	return 0;
}
  • 继承结构
#include  <iostream>
#include  <stdio.h>
using namespace std;
class Base
{
private:
	int a;
	int b;
};

class Child : Base
{
private:
	int c;
};
int main(int argc, char const *argv[])
{
	cout<<sizeof(Base)<<endl;//输出 8
	cout<<sizeof(Child)<<endl;//输出 12
	//结论:
	// 从sizeof(Child)大小看出,派生类必定存在一个基类对象
	//与继承方式 基类成员访问模式无关
	return 0;
}
  • 实例化派生类对象,首先调用基类构造函数实例化一个匿名基类对象
    如果是系统默认无参构造可以不写
#include  <iostream>
#include  <stdio.h>
using namespace std;
class Base
{
public:
	Base(int _a)
	{
		a=_a;
		cout<<"Base constructor"<<endl;
	}
private:
	int a;
	int b;
};

class Child : Base
{
public:
	Child(int _b):Base(1)
	{
		cout<<"Child constructor"<<endl;
	}
	//错误写法
	// Child(int _b)
	// {
	// 	Base(1);//错在这样写。
	// 	cout<<"Child constructor"<<endl;
	// }
private:
	int c;
};
int main(int argc, char const *argv[])
{
	Child c1(1);//输出 Base constructor
                //     Child constructor 可以看出先构造了基类对象再构造子类对象。
	return 0;
}
  • 继承后用 实例化对象后 类的成员变量内存排列情况
  • 用C指针的方式访问对象的私有成员。
#include  <iostream>
#include  <stdio.h>
using namespace std;
class Base
{
public:
	Base(int _a,int _b)
	{
		a=_a;
		b=_b;
		cout<<"Base constructor"<<endl;
	}
	void fun()
	{
		cout<<"fun()"<<endl;
	}
private:
	int a;
	int b;
};

class Child : public Base
{
public:
	Child(int _c,int _a,int _b):Base(_a,_b)
	{
		c=_c;
		cout<<"Child constructor"<<endl;
	}
	//错误写法
	// Child(int _b)
	// {
	// 	Base(1);//错在这样写。
	// 	cout<<"Child constructor"<<endl;
	// }
private:
	int c;
};
int main(int argc, char const *argv[])
{
	Child c(100,200,300);
	//如何用c指针的方式去访问对象的成员变量值:
	int * ptr_int = (int*)&c;
	cout<<*ptr_int<<endl;     //输出200
	cout<<*(ptr_int+1)<<endl; //输出300
	cout<<*(ptr_int+2)<<endl; //输出100
	//从上面的代码输出结果我们一目了然的看出 基类的成员变量和子类的成员变量排列顺序
	//基类的成员变量在前面,子类的

	c.fun();//这个访问可以看做:c.匿名对象.fun() 因为前面说过了,先构造一个基类对象。
	return 0;
}
  • 继承后 析构问题
#include  <iostream>
#include  <stdio.h>
using namespace std;
class Base
{
public:
	Base(int _a,int _b)
	{
		a=_a;
		b=_b;
		cout<<"Base constructor"<<endl;
	}
	void fun()
	{
		cout<<"fun()"<<endl;
	}
	~Base()
	{
		cout<<"base disconstructor"<<endl;
	}
private:
	int a;
	int b;
};

class Child : public Base
{
public:
	Child(int _c,int _a,int _b):Base(_a,_b)
	{
		c=_c;
		cout<<"Child constructor"<<endl;
	}
	//错误写法
	// Child(int _b)
	// {
	// 	Base(1);//错在这样写。
	// 	cout<<"Child constructor"<<endl;
	// }
	~Child()
	{
		cout<<"Child disconstructor"<<endl;
	}
private:
	int c;
};
int main(int argc, char const *argv[])
{
	Child c(100,200,300);//输出 Child disconstructor
						 //     base disconstructor
	//由此可以看出 首先调用派生类析构函数,再调用基类析构函数
	//同时也满足以前原理 先构造的后释放。
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值