Home > Backend Development > C++ > Detailed explanation of C++ function inheritance: How to define and use abstract base classes?

Detailed explanation of C++ function inheritance: How to define and use abstract base classes?

WBOY
Release: 2024-05-03 13:21:02
Original
1060 people have browsed it

Function inheritance allows derived classes to reuse the function definitions of the base class, which is achieved through the following steps: Define an abstract base class, including pure virtual functions. Use the override keyword in the derived class to inherit and implement the functions of the base class. Practical case: Create an abstract base class Shape, and derive classes Circle and Rectangle to calculate the areas of different shapes.

C++ 函数继承详解:如何定义和使用抽象基类?

C Detailed explanation of function inheritance: defining and using abstract base classes

What is function inheritance?

Function inheritance is a C feature that allows derived classes to inherit the function definitions of a base class, thereby reusing the functionality of the base class in subclasses.

Define abstract base class

An abstract base class is a base class that is not intended to be instantiated, it only serves as a base class for other classes. It contains pure virtual functions (that is, functions without a function body) that must be overridden in derived classes. To declare an abstract base class, use virtual and = 0, for example:

class Shape {
public:
    virtual double area() const = 0;
};
Copy after login

Function inheritance in derived classes

A derived class can inherit the function definition of an abstract base class by using the override keyword and providing an implementation of the function. For example:

class Circle : public Shape {
public:
    override double area() const {
        return M_PI * radius * radius;
    }
private:
    double radius;
};
Copy after login

Practical case: Shape area calculation

Let us take the calculation of shape area as an example for a practical demonstration. We create a Shape abstract base class and create Circle and Rectangle derived classes to calculate the area of ​​circles and rectangles:

#include <iostream>
#include <cmath>

using namespace std;

class Shape {
public:
    virtual double area() const = 0;
};

class Circle : public Shape {
public:
    Circle(double radius) : radius(radius) {}
    override double area() const {
        return M_PI * radius * radius;
    }
private:
    double radius;
};

class Rectangle : public Shape {
public:
    Rectangle(double width, double height) : width(width), height(height) {}
    override double area() const {
        return width * height;
    }
private:
    double width;
    double height;
};

int main() {
    Circle circle(5);
    cout << "Circle area: " << circle.area() << endl;

    Rectangle rectangle(3, 4);
    cout << "Rectangle area: " << rectangle.area() << endl;

    return 0;
}
Copy after login

Run output:

Circle area: 78.5398
Rectangle area: 12
Copy after login

The above is the detailed content of Detailed explanation of C++ function inheritance: How to define and use abstract base classes?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template