面向对象程序设计中最重要的一个概念是继承。继承允许我们依据另一个类来定义一个类,这使得创建和维护一个应用程序变得更容易。这样做,也达到了重用代码功能和提高执行效率的效果。
当创建一个类时,您不需要重新编写新的数据成员和成员函数,只需指定新建的类继承了一个已有的类的成员即可。这个已有的类称为基类,新建的类称为派生类。
继承代表了is a关系。例如,哺乳动物是动物,狗是哺乳动物,因此,狗是动物,等等。
基类 & 派生类
一个类可以派生自多个类,这意味着,它可以从多个基类继承数据和函数。定义一个派生类,我们使用一个类派生列表来指定基类。类派生列表以一个或多个基类命名,形式如下:
class derived-class: access-specifier base-class
其中,访问修饰符 access-specifier 是public、protected或private其中的一个,base-class 是之前定义过的某个类的名称。如果未使用访问修饰符 access-specifier,则默认为 private。
假设有一个基类Shape,Rectangle是它的派生类,如下所示:
实例
1
2 //jet.h / jet.cpp //派生自基类airplan 继承airplan的接口和数据
3 #pragma once
4 #ifndef __jet_h__
5 #define __jet_h__
6 #include <iostream>
7 #include "airplane.h"
8 namespace apdance
9 {
10 class jet : public airplane //继承自airplane
11 {
12 public:
13 jet();
14 ~jet();
15 void set_weapons(int w);
16 int get_weapons();
17
18 private:
19 int weapons;
20 };
21
22 }
23 #endif
24
25 #include "stdafx.h"
26 #include <iostream>
27 #include "jet.h"
28
29 namespace apdance
30 {
31 jet::jet()
32 {
33 weapons = 1;
34 std::cout << "jet take off!\n";
35 }
36 jet::~jet()
37 {
38 std::cout << "jet down!may day may day!";
39 }
40 void jet::set_weapons(int w)
41 {
42 weapons = w;
43 }
44 int jet::get_weapons()
45 {
46 return weapons;
47 }
48 }//namespace apdance
49
50
51 //airplan.h / airplan.cpp
52 #pragma once
53 #ifndef __AIRPLANE_H__
54 #define __AIRPLANE_H__
55 #include <iostream>
56 namespace apdance //定义命名空间
57 {
58 class airplane //创建基类airplane
59 {
60 public:
61 airplane()
62 {
63 wheels = 3;
64 wings = 2;
65 engines = 1;
66 std::cout << "对象已创建\n";
67 }
68 ~airplane()
69 {
70 std::cout << "对象已删除\n";
71 }
72 public:
73 void set_wings(int w);
74 int get_wings();
75 void set_wheels(int w);
76 int get_wheels();
77 void set_engines(int w);
78 int get_engines();
79 void fly();
80 private:
81 int wings;
82 int wheels;
83 int engines;
84 };
85 }//namespace
86 #endif // !___AIRPLANE_H__
87 #include "stdafx.h"
88 #include <iostream>
89 #include "airplane.h"
90 namespace apdance
91 {
92 void airplane::set_wings(int w)
93 {
94 wings = w;
95 }
96 int airplane::get_wings()
97 {
98 return wings;
99 }
100 void airplane::set_wheels(int w)
101 {
102 wheels = w;
103 }
104 int airplane::get_wheels()
105 {
106 return wheels;
107 }
108 void airplane::set_engines(int w)
109 {
110 engines = w;
111 }
112 int airplane::get_engines()
113 {
114 return engines;
115 }
116 void airplane::fly()
117 {
118 std::cout << "iam fly now!\n";
119 }
120 }//namespace
121
122
123 // cpp_learning.cpp : 定义控制台应用程序的入口点。 测试 没问题
124 #include "stdafx.h"
125 #include "iostream"
126 #include "airplane.h"
127 #include "jet.h"
128 int main()
129 {
130 apdance::jet x;
131 x.set_engines(100);
132 std::cout << x.get_engines() << '\n';
133 return 0;
134 }
访问控制和继承
派生类可以访问基类中所有的非私有成员。因此基类成员如果不想被派生类的成员函数访问,则应在基类中声明为 private。
我们可以根据访问权限总结出不同的访问类型,如下所示:
访问 | public | protected | private |
---|---|---|---|
同一个类 | yes | yes | yes |
派生类 | yes | yes | no |
外部的类 | yes | no | no |
一个派生类继承了所有的基类方法,但下列情况除外:
-
基类的构造函数、析构函数和拷贝构造函数。
-
基类的重载运算符。
-
基类的友元函数。
继承类型
当一个类派生自基类,该基类可以被继承为public、protected或private几种类型。继承类型是通过上面讲解的访问修饰符 access-specifier 来指定的。
我们几乎不使用protected或private继承,通常使用public继承。当使用不同类型的继承时,遵循以下几个规则:
-
公有继承(public):当一个类派生自公有基类时,基类的公有成员也是派生类的公有成员,基类的保护成员也是派生类的保护成员,基类的私有成员不能直接被派生类访问,但是可以通过调用基类的公有和保护成员来访问。
-
保护继承(protected):当一个类派生自保护基类时,基类的公有和保护成员将成为派生类的保护成员。
-
私有继承(private):当一个类派生自私有基类时,基类的公有和保护成员将成为派生类的私有成员。
多继承
多继承即一个子类可以有多个父类,它继承了多个父类的特性。
C++ 类可以从多个类继承成员,语法如下:
class <派生类名>:<继承方式1><基类名1>,<继承方式2><基类名2>,…{<派生类类体>};
其中,访问修饰符继承方式是public、protected或private其中的一个,用来修饰每个基类,各个基类之间用逗号分隔,如上所示。现在让我们一起看看下面的实例:
实例
#include <iostream>
using namespace std;
// 基类 Shape
class Shape
{
public:
void setWidth(int w)
{
width = w;
}
void setHeight(int h)
{
height = h;
}
protected:
int width;
int height;
};
// 基类 PaintCost
class PaintCost
{
public:
int getCost(int area)
{
return area * 70;
}
};
// 派生类
class Rectangle: public Shape, public PaintCost
{
public:
int getArea()
{
return (width * height);
}
};
int main(void)
{
Rectangle Rect;
int area;
Rect.setWidth(5);
Rect.setHeight(7);
area = Rect.getArea();
// 输出对象的面积
cout << "Total area: " << Rect.getArea() << endl;
// 输出总花费
cout << "Total paint cost: $" << Rect.getCost(area) << endl;
return 0;
}
当上面的代码被编译和执行时,它会产生下列结果:
Total area: 35Total paint cost: $2450
原文链接: https://www.cnblogs.com/zeolim/p/12335338.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/193340
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!