(二十三)C++篇-继承与多态

2022-07-07  本文已影响0人  GoodTekken

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

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

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

多继承即一个子类可以有多个父类,它继承了多个父类的特性。

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

测试代码如下:

#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 Reatangle:public Shape,public PaintCost
{
public:
    int getArea()
    {
        return(width*height);
    }
};

int main(void)
{
    Reatangle 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;
}

输出结果:

tekken@tekken:~/C++WS$ ./a.out 
Total area: 35
Total paint cost:$ 2450

多态
C++ 多态意味着调用成员函数时,会根据调用函数的对象的类型来执行不同的函数。
有了多态,我们可以有多个不同的类,都带有同一个名称但具有不同实现的函数,函数的参数甚至可以是相同的。
虚函数 是在基类中使用关键字 virtual 声明的函数。在派生类中重新定义基类中定义的虚函数时,会告诉编译器不要静态链接到该函数。
我们想要的是在程序中任意点可以根据所调用的对象类型来选择调用的函数,这种操作被称为动态链接,或后期绑定

注意:基类的 int area()前面需要加上virtual修饰符

测试代码如下:

#include<iostream>
using namespace std;

class Shape
{
protected:
    int width,height;
public:
    Shape(int a,int b=0)
    {
        width=a;
        height=b;
    }
    virtual int area() // Shape 类中,area() 的声明前放置关键字 virtual
    {
        cout<<"Parent class area: "<<endl;
        return 0;
    }
};

class Rectangle:public Shape
{
public:
    Rectangle(int a=0,int b=0):Shape(a,b){}
    int area()
    {
        cout<<"Rectangle class area: "<<endl;
        return(width*height);
    }
};

class Triangle:public Shape
{
public:
    Triangle(int a=0,int b=0):Shape(a,b){}
    int area()
    {
        cout<<"Triangle class area: "<<endl;
        return(width*height/2);
    }
};

int main()
{
    Shape *shape;
    Rectangle rec(10,7);
    Triangle tri(10,5);
    
    // 存储矩形的地址
    shape = &rec;
    // 调用矩形的求面积函数 area
    cout<<shape->area()<<endl;
    
    // 存储三角形的地址
    shape=&tri;
    // 调用三角形的求面积函数 area
    cout<<shape->area()<<endl;
    
    return 0;
}

输出结果:

tekken@tekken:~/C++WS$ ./a.out 
Rectangle class area: 
70
Triangle class area: 
25

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

class Shape {
   protected:
      int width, height;
   public:
      Shape( int a=0, int b=0)
      {
         width = a;
         height = b;
      }
      // pure virtual function
      virtual int area() = 0;
};

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

上一篇 下一篇

猜你喜欢

热点阅读