纯虚函数和抽象类

纯虚函数和抽象类

注意:

  • 多态实现的关键是虚函数表。(每一个有虚函数的类,在该类对象的内存的第一块就是存放的虚函数表的地址)

  • 在使用基类指针指向派生类对象时,需要将基类的析构函数添加virtual关键字,将其变为虚析构函数。(否则在删除基类指针指向的额派生类对象时,只会调用基类的析构函数,会造成内存泄漏)

  • 在抽象类的成员函数内部可以调用纯虚函数(多态),而在构造和析构函数内部不能调用纯虚函数(非多态,抽象类中的该函数无函数体,不可调用)

1.纯虚函数与抽象类

C++中的纯虚函数(或抽象函数)是我们没有实现的虚函数!我们只需声明它! 通过声明中赋值0来声明纯虚函数!

// 抽象类
Class A {
public: 
    virtual void show() = 0; // 纯虚函数
    /* Other members */
}; 
  • 纯虚函数:没有函数体的虚函数
  • 抽象类:包含纯虚函数的类

抽象类只能作为基类来派生新类使用,不能创建抽象类的对象,抽象类的指针和引用->由抽象类派生出来的类的对象!

代码样例:test.cpppure_virtual.cpp

2.实现抽象类

抽象类中:在成员函数内可以调用纯虚函数,在构造函数/析构函数内部不能使用纯虚函数。

如果一个类从抽象类派生而来,它必须实现了基类中的所有纯虚函数,才能成为非抽象类。

// A为抽象类
class A {
public:
    virtual void f() = 0;  // 纯虚函数
    void g(){ this->f(); }
    A(){}  // 构造函数
};

class B : public A{
public:
    void f(){ cout<<"B:f()"<<endl;}  // 实现了抽象类的纯虚函数
};

代码样例:abstract.cpp

3.重要点

// 抽象类至少包含一个纯虚函数
class Base{
public: 
    virtual void show() = 0; // 纯虚函数
    int getX() { return x; } // 普通成员函数

private:
     int x; 
}; 
class Derived : public Base { 
public: 
    void show() { cout << "In Derived \n"; } // 实现抽象类的纯虚函数
    Derived(){} // 构造函数
}; 

int main(void) 
{ 
    //Base b;  // error! 不能创建抽象类的对象
    //Base *b = new Base(); error!
    
    Base *bp = new Derived(); // 抽象类的指针和引用 -> 由抽象类派生出来的类的对象
    bp->show();
    return 0; 
}
// Derived为抽象类
class Derived: public Base 
{ 
public: 
//    void show() {}
}; 
// 抽象类
class Base { 
    protected: 
        int x; 
    public: 
        virtual void fun() = 0; 
        Base(int i) { x = i; }  // 构造函数
}; 
// 派生类
class Derived: public Base 
{ 
    int y; 
public: 
    Derived(int i, int j) : Base(i) { y = j; } // 构造函数
    void fun() { cout << "x = " << x << ", y = " << y; }
}; 
// 抽象类
class Base  {
public:
    Base(){ cout << "Constructor: Base" << endl; }
    virtual ~Base(){ cout << "Destructor : Base" << endl; }
    
    virtual void func() = 0;
};

class Derived: public Base {
public:
    Derived(){ cout << "Constructor: Derived" << endl; }
    ~Derived(){ cout << "Destructor : Derived" << endl;}
    
    void func(){cout << "In Derived.func()." << endl;}
};

当基类指针指向派生类对象并删除对象时,我们可能希望调用适当的析构函数。
如果析构函数不是虚拟的,则只能调用基类析构函数。

### 使用 C++ 纯虚函数抽象类实现图像面积计算 #### 定义抽象基类 `Shape` 为了实现不同形状的图像面积计算,可以创建一个名为`Shape`的抽象基类。该类包含了一个纯虚函数`calculateArea()`用于定义接口。 ```cpp #include <iostream> using namespace std; class Shape { public: virtual double calculateArea() const = 0; // 纯虚函数 }; ``` 此部分代码声明了`Shape`作为一个无法实例化的抽象类,并规定任何派生自它的具体类都需要提供自己的`calculateArea`版本[^1]。 #### 创建具体的形状类 Circle Rectangle 接下来分别定义圆形(Circle)矩形(Rectangle),它们都是从`Shape`继承而来: ```cpp // 圆形类 class Circle : public Shape { private: double radius; public: Circle(double r):radius(r){} double calculateArea() const override { return 3.14 * radius * radius; } }; // 矩形类 class Rectangle : public Shape { private: double width, height; public: Rectangle(double w, double h):width(w),height(h){} double calculateArea() const override { return width * height; } }; ``` 这里实现了两个具体的类——Circle(圆)Rectangle(矩形),每个都有各自的参数以及重写了来自父类`Shape`中的`calculateArea`方法[^2]。 #### 多态的应用场景展示 下面通过一段简单的测试程序来验证上述结构的有效性: ```cpp int main(){ Shape* shapes[] = { new Circle(5), new Rectangle(4,7) }; cout << "Areas of different shapes:" << endl; for(int i=0;i<2;++i){ cout << "Shape#" << (i+1) << ": Area=" << shapes[i]->calculateArea()<<endl; } // 清理内存 for(auto shape:shapes){ delete shape; } } ``` 这段代码展示了如何利用指针数组存储不同类型的对象地址,从而可以在不知道确切类型的条件下调用对应的成员函数完成特定操作。这正是C++多态性的体现之一。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Tech沉思录

点赞加投币,感谢您的资瓷~

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

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

打赏作者

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

抵扣说明:

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

余额充值