c++继承

c++继承是c++的三大特性之一,目的是解决代码的复用性,子类继承基类的所有成员,除了构造函数和析构函数

还有operator=也不能被继承

继承有三种继承方式,最常用的是public继承

三种继承方式:

public :    公有继承

private :   私有继承

protected : 保护继承


不同方式继承时,继承的成员权限会改变,对子类来说继承过来的成员只要不是私有成员子类内部都可以访问,因为继承的成员拷贝了一份给子类,这些成员是属于子类的,子类自然可以访问,但是在多继承时成员访问权限改变了,有些可以访问的就访问不了了



改变权限如下:

公有派生

基类属性  派生类权限
私有     不可访问
保护 保护   

公有 公有



代码:

#include<iostream>
using namespace std;

//多继承公有派生
class grandfather
{
public:
	grandfather()
	{
		this->Gfa = 10;
		this->Gfb = 20;
		this->Gfc = 30;
	}
public:
	int Gfa;
protected:
	int Gfb;
private:
	int Gfc;

};

class father :public grandfather
{
public:
	void test()
	{
		cout << Gfa << endl;//公有可以访问,继承后成员变量还是公有
		cout << Gfb << endl;//保护可以访问,继承后成员变量还是保护
		//cout << Gfc << endl;私有不能访问
	}

};


class son :public father
{
public:
	void test()
	{
		cout << Gfa << endl;//公有可以访问,继承后成员变量还是公有
		cout << Gfb << endl;//保护可以访问,继承后成员变量还是保护
		//cout << Gfc << endl;私有不能访问
	}

};





int main()
{
	father F;
	son S;
	cout<<S.Gfa << endl;//类外只能访问公有属性
	cout << F.Gfa << endl;
	system("pause");
	
}




私有派生
基类属性 派生类权限
私有   不可访问
保护 私有 
公有 私有


代码

#include<iostream>
using namespace std;
//多继承私有派生
class grandfather
{
public:
	grandfather()
	{
	this->Gfa = 10;
	this->Gfb = 20;
	this->Gfc = 30;
	}
public:
	int Gfa;
protected:
	int Gfb;
private:
	int Gfc;

};

class father :private grandfather
{
public:
	void test()
	{
	cout << Gfa << endl;//私有继承后公有变私有
	cout << Gfb << endl;//保护继承后保护变私有
	//cout << Gfc << endl;私有不能访问
	}

};


class son :public father
{
public:
	void test()
	{
	//cout << Gfa << endl;//私有继承后公有不可访问,继承后公有变私有
	//cout << Gfb << endl;//保护继承保护不可访问,继承后保护变私有
	//cout << Gfc << endl;私有不能访问
	}

};



int main()
{
	father F;
	F.test();
	//cout<<S.Gfa << endl;//类外只能访问公有属性
	//cout << F.Gfa << endl;
	system("pause");
	
}


保护派生
基类属性 派生类权限
私有   不可访问
保护 保护  
公有 保护

代码案例:

#include<iostream>
using namespace std;
//保护继承
class grandfather
{
public:
	grandfather()
	{
	this->Gfa = 10;
	this->Gfb = 20;
	this->Gfc = 30;
}
public:
	int Gfa;
protected:
	int Gfb;
private:
	int Gfc;

};

class father :protected grandfather
{
public:
	void test()
	{
	cout << Gfa << endl;//保护继承后公有变保护,相当与代码变成class father{protected:Gfa;Gfb;};
	cout << Gfb << endl;//保护继承后保护还是保护
	//cout << Gfc << endl;私有不能访问
	}

};


class son :public father
{
public:
	void test()
	{
	cout << Gfa << endl;//保护继承后公有变保护,保护子类可以访问
	cout << Gfb << endl;//保护继承后保护还是保护,保护子类可以访问
	//cout << Gfc << endl;私有不能访问
	}

};


int main()
{
	father F;
	son S;
	F.test();
	//cout << F.Gfa << endl;//权限变为保护的所以不能访问
	//cout << S.Gfa << endl;//权限变为保护的所以不能访问
	system("pause");
	
}


继承中的构造和析构的调用顺序:创建子类对象的时候先构造父类在构造子类,析构相反

#include<iostream>
using namespace std;
class father
{
public:
	father()
	{
		cout << "父类的构造函数" << endl;
	}
	~father()
	{
		cout << "父类的析构函数" << endl;
	}

};

class son:public father
{
public:
	son()
	{
		cout << "子类的构造函数" << endl;
	}
	~son()
	{
		cout << "子类的析构函数" << endl;
	}
};
void test()
{
	son S;
}
int main()
{
	test();
	system("pause");
}

结果如下:



继承中成员同名结果:子类和基类成员同名,在子类中,从基类继承过来的同名成员都会隐藏,子类外部不可访问,要想调用,必须显示指定调用

继承中重新定义或者重名一个基类的重载函数结果是基类所有的重载版本全部隐藏,子类外部不可访问,要想调用,必须显示指定调用

代码如下

#include<iostream>
using namespace std;
class father
{
public:
	father()
	{
		val = 10;
	}
	void test()
	{
		cout << val << endl;
	}

	void fun()
	{
		cout << "father::void fun()" << endl;
	}

	void fun(int n)
	{
		val = n;
		cout << "father::void fun(int n)" << endl;
	}

	
	
	int val;

};

class son:public father
{
public:
	son()
	{
		val = 100;
	}
	void test()
	{
		cout << val << endl;
		cout << father::val << endl;//子类和基类成员同名时,子类同样继承基类的同名成员,只是同名成员隐藏而已,想要访问必须显示调用
									
	}
	

	/*
	void fun(int a, int b)
	{
	cout << "son::void fun(int a,int b)" << endl;

	}
	*/

	
	int fun()//在子类中任何时候定义基类的重载函数,基类的重载版本的函数全部隐藏,外部能访问,调用只能显示调用;
	{
		cout << "son::int fun(int a, int b)" << endl;
		//father::fun(5);
		//father::fun();
		return 0;
	}
	
	
	int val;
};

int main()
{
	son S;
	cout << S.val << endl;//同名时,默认调用的是子类的成员,就近原则
	S.fun();
	system("pause");
}

结果如下:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值