纯虚函数、抽象基类

1. 纯虚函数 

    当在基类中时 : virtual 函数类型  函数名 (参数列表) = 0 ;

    当在派生类当中时 : 若是重写的是基类中已声明的虚函数则可不用杂开头加virtual,否则与在基类中一致。

class Person
{
   virtual void Display () = 0; // 纯虚函数
protected :
   string _name ; // 姓名
};
class Student : public Person
{};

注意:

 1. 我们可以对纯虚函数提供定义,不过函数体必须定义在类的外部。

 2. 最后面的 “ =0 ” 并不表示函数返回值等于0,它只起形式上的作用,告诉编译器这是 虚函数 ;

 3. 这是一个声明语句,最后有分号。

 

2. 抽象基类 (接口类)

含有纯虚函数的类是抽象基类,抽象基类负责接口定义,而后续其他类可以覆盖该接口,此外还有很重要的一点就是,我们不能直接创建一个抽象基类的对象,即不能实例化。

如果在一个类中声明了纯虚函数,在其派生类中没有对其函数进行定义,则该虚函数在派生类中仍然为纯虚函数。

 代码示例: 来自博客 https://blog.csdn.net/qq_36221862/article/details/61413619

#include <iostream>
using namespace std;

//声明抽象基类Shape
class Shape
{
public:
    virtual float area()const //虚函数
    {
        return 0.0;
    }


    virtual void shapeName()const  = 0;//纯虚函数
    //shapeNamea函数的作用是输出具体的形状,在派生类中定义,因此声明为纯虚函数
};

//声明Point类
class Point:public Shape
{
public:
    Point(float a = 0.0, float b = 0.0)
    {
        x = a;
        y = b;
    }

    void setPoint(float a, float b)
    {
        x = a;
        y = b;
    }

    float getX()const
    {
        return x;
    }

    float getY()const
    {
        return y;
    }

    virtual void shapeName()const
    {
        cout<<"Point:";
    }

    friend ostream & operator <<(ostream &_cout, const Point &p)
    {
        _cout<<"["<<p.x<<","<<p.y<<"]";
        return _cout;
    }

protected:
    float x;
    float y;
};

//声明Ciircle类
class Circle:public Point
{
public:
    Circle(float a = 0.0, float b = 0.0, float r = 0.0)
        :Point(a, b)
        ,radius(r)
    {}

    void setRadius(float r)

    {
        radius = r;
    }

    float getRadius()const
    {
        return radius;
    }

    virtual float area()const
    {
        return 3.1415926*radius*radius;
    }

    virtual void shapeName()const
    {
        cout<<"Circle:";
    }

    friend ostream & operator <<(ostream &_cout, const Circle &c)
    {
        _cout<<"["<<c.x<<","<<c.y<<"],r="<<c.radius;
        return _cout;
    }

protected:
    float radius;
};

int main()
{
    Point point(3.2, 4.5);  // 建立Point类对象point
    Circle circle(2.4, 1.2, 5.6);  //建立Circle类对象circle

    point.shapeName();        //静态关联
    cout<<point<<endl;

    circle.shapeName();       //静态关联
    cout<<circle<<endl;

    Shape* pt;    //定义基类指针
    pt = &point;
    pt->shapeName();
    cout<<"x="<<point.getX()<<",y="<<point.getY()<<endl;
    cout<<"area="<<pt->area()<<endl;

    pt = &circle;    //指针指向Circle类对象
    pt->shapeName();   //动态关联
    cout<<"x="<<circle.getX()<<",y="<<circle.getY()<<endl;
    cout<<"area="<<pt->area()<<endl;

    system("pause");
    return 0;
}

运行结果如下:

 

结论:
(1)一个基类如果包含一个或一个以上纯虚函数,就是抽象基类。抽象基类不能也没必要定义对象。
(2)在类的层次结构中,顶层或最上面几层可以是抽象基类。抽象基类体现了本类族中各类的共性,把各类中共有的成员函数集中在抽象基类中声明。
(3)抽象基类是本类族的共公共接口,即就是从同一基类中派生出的多个类有同一接口。 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值