10、【C++基础】继承和多态

C++ 继承

  面向对象程序设计中最重要的一个概念是继承。继承允许我们依据另一个类来定义一个类,这使得创建和维护一个应用程序变得更容易。这样做,也达到了重用代码功能和提高执行时间的效果。

  当创建一个类时,您不需要重新编写新的数据成员和成员函数,只需指定新建的类继承了一个已有的类的成员即可。这个已有的类称为基类,新建的类称为派生类

  继承代表了 is a 关系。例如,哺乳动物是动物,狗是哺乳动物,因此,狗是动物,等等。

基类 & 派生类

  一个类可以派生自多个类,这意味着,它可以从多个基类继承数据和函数。定义一个派生类,我们使用一个类派生列表来指定基类。类派生列表以一个或多个基类命名,形式如下:

1 class derived-class: access-specifier base-class

  其中,访问修饰符 access-specifier 是 public、protectedprivate 其中的一个,base-class 是之前定义过的某个类的名称。如果未使用访问修饰符 access-specifier,则默认为 private。

假设有一个基类 ShapeRectangle 是它的派生类,如下所示:

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 // 基类
 6 class Shape
 7 {
 8    public:
 9       void setWidth(int w)
10       {
11          width = w;
12       }
13       void setHeight(int h)
14       {
15          height = h;
16       }
17    protected:
18       int width;
19       int height;
20 };
21 
22 // 派生类
23 class Rectangle: public Shape
24 {
25    public:
26       int getArea()
27       {
28          return (width * height);
29       }
30 };
31 
32 int main(void)
33 {
34    Rectangle Rect;
35 
36    Rect.setWidth(5);
37    Rect.setHeight(7);
38 
39    // 输出对象的面积
40    cout << "Total area: " << Rect.getArea() << endl;
41 
42    return 0;
43 }

执行结果:

1 Total area: 

访问控制和继承

  派生类可以访问基类中所有的非私有成员。因此基类成员如果不想被派生类的成员函数访问,则应在基类中声明为 private。我们可以根据访问权限总结出不同的访问类型,如下所示:

访问publicprotectedprivate
同一个类 yes yes yes
派生类 yes yes no
外部的类 yes no no

一个派生类继承了所有的基类方法,但下列情况除外:

  • 基类的构造函数、析构函数和拷贝构造函数。
  • 基类的重载运算符。
  • 基类的友元函数。

继承类型

  当一个类派生自基类,该基类可以被继承为 public、protected private 几种类型。继承类型是通过上面讲解的访问修饰符 access-specifier 来指定的。

  我们几乎不使用 protected private 继承,通常使用 public 继承。当使用不同类型的继承时,遵循以下几个规则:

  • 公有继承(public):当一个类派生自公有基类时,基类的公有成员也是派生类的公有成员,基类的保护成员也是派生类的保护成员,基类的私有成员不能直接被派生类访问,但是可以通过调用基类的公有保护成员来访问。
  • 保护继承(protected): 当一个类派生自保护基类时,基类的公有保护成员将成为派生类的保护成员。
  • 私有继承(private):当一个类派生自私有基类时,基类的公有保护成员将成为派生类的私有成员。

多继承

多继承即一个子类可以有多个父类,它继承了多个父类的特性。C++ 类可以从多个类继承成员,语法如下:

1 class <派生类名>:<继承方式1><基类名1>,<继承方式2><基类名2>,2 {
3   <派生类类体>
4 };

  其中,访问修饰符继承方式是 public、protectedprivate 其中的一个,用来修饰每个基类,各个基类之间用逗号分隔,如上所示。现在让我们一起看看下面的实例:

 1 #include <iostream>
 2  
 3 using namespace std;
 4  
 5 // 基类 Shape
 6 class Shape 
 7 {
 8    public:
 9       void setWidth(int w)
10       {
11          width = w;
12       }
13       void setHeight(int h)
14       {
15          height = h;
16       }
17    protected:
18       int width;
19       int height;
20 };
21  
22 // 基类 PaintCost
23 class PaintCost 
24 {
25    public:
26       int getCost(int area)
27       {
28          return area * 70;
29       }
30 };
31  
32 // 派生类
33 class Rectangle: public Shape, public PaintCost
34 {
35    public:
36       int getArea()
37       { 
38          return (width * height); 
39       }
40 };
41  
42 int main(void)
43 {
44    Rectangle Rect;
45    int area;
46  
47    Rect.setWidth(5);
48    Rect.setHeight(7);
49  
50    area = Rect.getArea();
51    
52    // 输出对象的面积
53    cout << "Total area: " << Rect.getArea() << endl;
54  
55    // 输出总花费
56    cout << "Total paint cost: $" << Rect.getCost(area) << endl;
57  
58    return 0;
59 }

执行结果:

Total area: 35
Total paint cost: $2450

C++ 多态

  多态按字面的意思就是多种形态。当类之间存在层次结构,并且类之间是通过继承关联时,就会用到多态。C++ 多态意味着调用成员函数时,会根据调用函数的对象的类型来执行不同的函数。下面的实例中,基类 Shape 被派生为两个类,如下所示:

 1 #include <iostream> 
 2 using namespace std;
 3  
 4 class Shape {
 5    protected:
 6       int width, height;
 7    public:
 8       Shape( int a=0, int b=0)
 9       {
10          width = a;
11          height = b;
12       }
13       int area()
14       {
15          cout << "Parent class area :" <<endl;
16          return 0;
17       }
18 };
19 class Rectangle: public Shape{
20    public:
21       Rectangle( int a=0, int b=0):Shape(a, b) { }
22       int area ()
23       { 
24          cout << "Rectangle class area :" <<endl;
25          return (width * height); 
26       }
27 };
28 class Triangle: public Shape{
29    public:
30       Triangle( int a=0, int b=0):Shape(a, b) { }
31       int area ()
32       { 
33          cout << "Triangle class area :" <<endl;
34          return (width * height / 2); 
35       }
36 };
37 // 程序的主函数
38 int main( )
39 {
40    Shape *shape;
41    Rectangle rec(10,7);
42    Triangle  tri(10,5);
43  
44    // 存储矩形的地址
45    shape = &rec;
46    // 调用矩形的求面积函数 area
47    shape->area();
48  
49    // 存储三角形的地址
50    shape = &tri;
51    // 调用三角形的求面积函数 area
52    shape->area();
53    
54    return 0;
55 }

执行结果:

Parent class area
Parent class area

  导致错误输出的原因是,调用函数 area() 被编译器设置为基类中的版本,这就是所谓的静态多态,或静态链接 - 函数调用在程序执行前就准备好了。有时候这也被称为早绑定,因为 area() 函数在程序编译期间就已经设置好了。但现在,让我们对程序稍作修改,在 Shape 类中,area() 的声明前放置关键字 virtual,如下所示:

 1 class Shape {
 2    protected:
 3       int width, height;
 4    public:
 5       Shape( int a=0, int b=0)
 6       {
 7          width = a;
 8          height = b;
 9       }
10       virtual int area()
11       {
12          cout << "Parent class area :" <<endl;
13          return 0;
14       }
15 };

修改后的执行结果:

Rectangle class area
Triangle class area

  此时,编译器看的是指针的内容,而不是它的类型。因此,由于 tri 和 rec 类的对象的地址存储在 *shape 中,所以会调用各自的 area() 函数。正如您所看到的,每个子类都有一个函数 area() 的独立实现。这就是多态的一般使用方式。有了多态,您可以有多个不同的类,都带有同一个名称但具有不同实现的函数,函数的参数甚至可以是相同的。

虚函数

  虚函数 是在基类中使用关键字 virtual 声明的函数。在派生类中重新定义基类中定义的虚函数时,会告诉编译器不要静态链接到该函数。我们想要的是在程序中任意点可以根据所调用的对象类型来选择调用的函数,这种操作被称为动态链接,或后期绑定

纯虚函数

  您可能想要在基类中定义虚函数,以便在派生类中重新定义该函数更好地适用于对象,但是您在基类中又不能对虚函数给出有意义的实现,这个时候就会用到纯虚函数。我们可以把基类中的虚函数 area() 改写如下:

 1 class Shape {
 2    protected:
 3       int width, height;
 4    public:
 5       Shape( int a=0, int b=0)
 6       {
 7          width = a;
 8          height = b;
 9       }
10       // pure virtual function
11       virtual int area() = 0;
12 };

= 0 告诉编译器,函数没有主体,上面的虚函数是纯虚函数

原文地址:https://www.cnblogs.com/Long-w/p/8970550.html