C++入门——“继承”

一、引入

面相对象的计算机语言有三大特性:“封装”、“继承”、“多态”。今天来讲解一下C++的一大重要特性——继承。

        通俗理解来讲,继承就和现实生活一样,子辈继承父辈的一些特性,C++中的继承也可以这样理解。它允许我们在保持原有类(父类/基类)特性的基础上进⾏扩展,增加⽅法(成员函数)和属性(成员变量),这样产⽣新的类,称⼦类(派生类)。它的语法是:子类:继承方式 父类

        比如以下代码示例:

//动物
class animal
{
protected:
	int leg;//腿
	int skin;//皮肤
	int head;//头
	//......
};

//马,继承了动物类
class horse : public  animal
{
protected:
	int strong;//强壮
};

//驴,继承了动物类
class donkey : public animal
{
protected:
	int endurance;//耐力
};

        以上代码中,动物类是一个父类,而马类和驴类就是子类,他们继承了动物类的特点,并且子类拥有动物类的参数。

二、父类成员继承到子类后的访问限制和继承关系的关系

        父类在子类的继承方式遵循如下:

        细心的朋友就发现了如下规律:在继承关系和父类的访问限定符中,按照访问权限最小的那个限定符执行。其中,访问权限:public > protected > private。

        虽然父类的private成员无法在子类中使用,但这并不意味着该成员没有被继承到子类中。

        在struct类和class类的默认继承方式和他们的默认访问限定符一样,如果不显式的写出继承方式,struct默认是public继承,class默认是private继承。

三、父类和子类对象的赋值兼容转换

        在继承当中,公有继承(public)的子类的对象、指针、引用可以赋值给父类的对象、指针、引用。这种行为叫做切片(切割),代表将子类对象中属于父类的那一部分切割给父类。但是父类的对象不能赋值给子类的对象,其因为子类的对象的他值没有得到父类对象初始化。

        在父类指针或者引用指向子类对象的前提下,通过强制类型转换赋值给子类对象。

int main()
{
	 
	horse h;//子类对象
	animal* aptr = &h;//父类指针指向子类

	horse* hptr = (horse *)aptr;//强制类型转换赋值给子类对象

	return 0;

        但是在父类指针指向父类对象的时候,就不能通过强转赋值给子类对象:


int main()
{
	 
	animal a;//父类对象
	animal* aptr = &a;//父类指针指向父类

	horse* hptr = (horse *)a;//强制类型转换赋值给子类对象

	return 0;
}

以上代码在VS中会出现如下的报错:

四、隐藏

        父类和子类拥有独立的作用域,另外在子类中,如果出现了和父类成员及其变量名字一模一样的情况,那么语法上会自动隐藏掉父类的同名成员及其变量,即使参数不一样,也不会构成重载,如果想要使用父类的那个函数或者变量,就需要显式的写出父类中的成员变量:“父类类名::父类成员”来使用隐藏掉的父类成员。因此为了避免隐藏的发生,尽量不要在子类中定义和父类成员名字一样的新成员。

//动物
class animal
{
public:
	void print()
	{
		cout << "class animal" << endl;
	}
protected:
	int leg;//腿
	int skin;//皮肤
	int head;//头
	//......
};

//马
class horse : public  animal
{
public:
	void print()
	{
		cout << "class horse : public  animal" << endl;
	}

protected:
	int strong;//强壮
};

int main()
{
	 
	horse h;
	h.print();



	return 0;
}

调用的是子类的print:

        若要调用父类print,则需要指定类域:

int main()
{
	 
	horse h;
	h.animal::print();



	return 0;
}

运行结果如下:

五、子类的默认成员函数

        子类的默认成员函数和普通类大差不差,只是在调用子类的默认成员函数之前,都需要先调用父类的默认成员函数(除开析构函数)。

        1.构造函数

        如果父类没有默认的构造函数,就需要在子类的构造函数中显式的调用父类构造,如果父类有默认的构造函数,则不需要显式的调用。

父类有默认构造:

//动物
class animal
{
public:
	animal()
		:leg (1)
		,skin (2) 
		, head(3)
	{
		cout << "class animal" << endl;
	}


protected:
	int leg;//腿
	int skin;//皮肤
	int head;//头
	//......
};

//马
class horse : public  animal
{
public:
	horse()
		:strong(1)
	{
		cout << "class horse : public  animal" << endl;
	}
protected:
	int strong;//强壮
};


int main()
{
	 
	horse h;


	return 0;
}

父类没有默认构造:

//动物
class animal
{
public:
	animal(int l,int s,int h)
		:leg (l)
		,skin (s) 
		, head(h)
	{
		cout << "class animal" << endl;
	}


protected:
	int leg;//腿
	int skin;//皮肤
	int head;//头
	//......
};

//马
class horse : public  animal
{
public:
	horse()
		:animal(1,2,3)//显示调用父类的构造
		,strong(1)
	{
		cout << "class horse : public  animal" << endl;
	}
protected:
	int strong;//强壮
};


int main()
{
	 
	horse h;


	return 0;
}

运行结果:

        2.析构函数

        析构函数不用我们显式的调用,编译器会自动帮我们调用,不过调用的顺序是先调用子类的析构函数,再调用父类的析构函数,满足栈先进后出的特点。

//动物
class animal
{
public:
	~animal()
	{
		cout << "~animal()" << endl;
	}


protected:
	int leg;//腿
	int skin;//皮肤
	int head;//头
	//......
};

//马
class horse : public  animal
{
public:
	~horse()
	{
		cout << "~horse()" << endl;
	}
protected:
	int strong;//强壮
};

int main()
{
	 
	horse h;


	return 0;
}

运行结果:

        3.拷贝构造

        子类的拷贝构造需要显式调用父类的拷贝构造(拷贝构造也是一种构造),如果不显式调用,编译器将会报错。

//动物
class animal
{
public:
	animal()
	{}

	animal(const animal& copy)
	{
		leg = copy.leg;
		skin = copy.skin;
		head = copy.head;

		cout << "animal(const animal& copy)" << endl;
	}

protected:
	int leg;//腿
	int skin;//皮肤
	int head;//头
	//......
};

//马
class horse : public  animal
{
public:
	horse()
	{}

	horse(const horse& copy)
		:animal(copy)//显式调用父类拷贝构造,这里构成切片的条件
		, strong (copy.strong)
	{
		cout << "horse(const horse& copy)" << endl;
	}
protected:
	int strong;//强壮
};

int main()
{
	 
	horse h1;
	horse h2 = h1;


	return 0;
}

运行结果:

        值得注意的是:最好在初始化列表中显式的调用父类的拷贝构造,如果在子类的大括号里调用,比如:

	horse(const horse& copy)
	{
		animal(copy);
		strong = copy.strong;

		cout << "horse(const horse& copy)" << endl;
	}

        这样的方式是错误的,编译器会认为你定义了一个匿名对象,并重新定义了一个“copy”变量,这不仅违背了我们想调用拷贝构造的初心,还导致了重定义“copy”变量的错误。

        在大括号内部调用以下方式也是错误的:

animal::animal(copy);

       拷贝构造也是构造,类成员的变量是在初始化列表就已经被初始化,在初始化列表之后才是大括号内部的内容。并且基类的构造优先于派生类,因此以上方式达不到初始化基类的效果。

4.赋值运算符重载

        因为赋值运算符的函数名都是一样,所以在在子类的重载函数中需要指定类域:

//动物
class animal
{
public:
	animal& operator=(const animal& a)
	{
		leg = a.leg;
		skin = a.skin;
		head = a.skin;
		cout << "animal& operator=(const animal& a)" << endl;
		return *this;
	}

//protected:
	int leg;//腿
	int skin;//皮肤
	int head;//头
	//......
};

//马
class horse : public  animal
{
public:
	horse& operator=(const horse& a)
	{
		animal::operator=(a);
		cout << "horse& operator=(const horse& a)" << endl;
		return *this;
	}

protected:
	int strong;//强壮
};


int main()
{
	 
	horse h1;
	horse h2;
	h1.leg = 1000;
	h2 = h1;

	cout << h2.leg << endl;
	return 0;
}

运行结果:

六、多继承和钻石继承(菱形继承)

        C++的继承方式有两种——单继承,多继承,以上代码为单继承,马类和驴类又可以派生出骡子类,那么这个骡子类就是多继承。

//动物
class animal
{
public:
	animal& operator=(const animal& a)
	{
		leg = a.leg;
		skin = a.skin;
		head = a.skin;
		cout << "animal& operator=(const animal& a)" << endl;
		return *this;
	}

//protected:
	int leg;//腿
	int skin;//皮肤
	int head;//头
	//......
};

//马
class horse : public  animal
{
public:
	horse& operator=(const horse& a)
	{
		animal::operator=(a);
		cout << "horse& operator=(const horse& a)" << endl;
		return *this;
	}

//protected:
	int strong;//强壮
};

//驴
class donkey : public animal
{

protected:
	int endurance;//耐力
};

//骡子
class mule : public horse, public donkey
{
public:
	int hard_working;//努力工作
};

int main()
{
	mule m;


	cout <<23 << endl;
	return 0;
}

        多继承的父类,每一个类对应着一个继承方式,不可省略。

        多继承衍生出了一个继承方式“菱形继承”,马和驴都继承了动物类,那么驴继承了马类和驴类,那么我如果想访问驴类中的“head”,那么访问的是马类的head还是驴类的head呢?这种菱形继承在一定程度上浪费了空间,还会在访问上造成一定程度的歧义(编译器一般会报错)。

        解决菱形继承的方式有两种:一种是使用关键字“virtual”(虚拟的),即在驴类和马类继承的父类前加上这个关键字,这种继承方式叫“虚继承”,不过这种继承牺牲了一定的性能为代价如果发生了多个类的菱形继承,我们需要整理关系网,在继承了同一个父类的子类处加上该关键字。(不建议这种方式)

        第二种是非必要的情况下避免菱形继承,直接从源头避免这种继承方式。

补充:

        在类中的静态成员变量也会被继承到子类中,它和普通类一样无论实例化多少子类,都公用一个静态成员变量。

        父类的友元关系不会被继承。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值