C++的使用小教程4——类的继承

面向对象编程噢!类的继承也很重要。
在这里插入图片描述

1、类的继承是什么

类的继承概念是怎么样的呢?它的概念就好像哺乳动物的关系,哺乳动物是一个类,人也是一个类,人属于哺乳动物,哺乳动物所具备的特征人都具备。因此,我们在“创建“人这个类的时候,不需要从头创建,它有许多特点可以从哺乳动物处获取。
换一种话说,当创建一个类时,您不需要重新编写新的数据类型和函数,只需指定新建的类继承了一个已有的类的成员即可,这就是类的继承。
这个已有的类称为基类,新建的类称为派生类。
在C++语言中,假设存在一个类,名为Box:

class Box {
protected:
	int width;
	int length;
public:
	void setWidth(int widthIn);
	void setLength(int lengthIn);
	int getWidth();
	int getLength();
};

如果有个类继承了Box类,他将拥有Box类的数据类型与函数,同时作为子类,它可以具备自己的特点,定义自己的属性与方法。

class smallBox:public Box {
public:
	int getArea() {
		return width * length;
	}
};

这个类的功能与直接定义如下代码相同:

class smallBox {
protected:
	int width;
	int length;
public:
	void setWidth(int widthIn);
	void setLength(int lengthIn);
	int getWidth();
	int getLength();
	int getArea() {
		return width * length;
	}
};

2、派生类继承的内容

派生类并不是可以继承基类的所有内容,在基类中定义为private的,属于基类所特有的内容,无法被派生类继承。
换一句话说:派生类可以访问基类中所有的非私有成员。
如果基类成员中存在一定的参数或者函数不想被派生类的成员函数访问,则应在基类中声明为 private。
对于派生类继承基类的内容,总结如下:

修饰符号publicprotectedprivate
基类可以访问可以访问可以访问
派生类可以访问可以访问不可以访问
外部类可以访问不可以访问不可以访问

其中,外部类指的是在其它地方调用该类。如在main函数中定义。

smallBox smallbox;

3、类的继承的应用示例

3.1、单继承

该例子讲述的是smallBox类继承Box类的例子。

#include <iostream>
#include <cstring>
using namespace std;

class Box {
protected:
	int width;
	int length;
public:
	void setWidth(int widthIn);
	void setLength(int lengthIn);
	int getWidth();
	int getLength();
};

void Box::setWidth(int widthIn){
	width = widthIn;
}

void Box::setLength(int lengthIn) {
	length = lengthIn;
}

int Box::getWidth() {
	return width;
}

int Box::getLength() {
	return length;
}

class smallBox:public Box {
public:
	int getArea() {
		return width * length;
	}
};


int main()
{
	smallBox smallbox;
	smallbox.setLength(5);
	smallbox.setWidth(5);
	cout << "The area of smallbox is " << smallbox.getArea();
	system("pause");
}

应用结果为:

The area of smallbox is 25
请按任意键继续. . .

3.2、多继承

多继承就好像爸爸妈妈生宝宝一样,宝宝会继承爸爸妈妈各自的特点,成为一个新的个体。

#include <iostream>
#include <cstring>
using namespace std;

/*Box基类*/
class Box 
{
protected:
	int width;
	int length;
public:
	void setWidth(int widthIn);
	void setLength(int lengthIn);
	int getWidth();
	int getLength();
};

void Box::setWidth(int widthIn){
	width = widthIn;
}

void Box::setLength(int lengthIn) {
	length = lengthIn;
}

int Box::getWidth() {
	return width;
}

int Box::getLength() {
	return length;
}

/*HowMuch基类*/
class HowMuch
{
public:
	int howMuchIsIt(int area)
	{
		return area * 5;
	}
};

/*继承上述两个类*/
class smallBox:public Box,public HowMuch {
public:
	int getArea() {
		return width * length;
	}
};


int main()
{
	smallBox smallbox;
	smallbox.setLength(5);
	smallbox.setWidth(5);
	cout << "The area of smallbox is " << smallbox.getArea() << endl;
	cout << "It need " << smallbox.howMuchIsIt(smallbox.getArea()) << " yuan" << endl;
	system("pause");
}

应用结果为:

The area of smallbox is 25
It need 125 yuan
请按任意键继续. . .
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Bubbliiiing

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

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

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

打赏作者

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

抵扣说明:

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

余额充值