Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed explanation and function of Abstract classes in C language or C++

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains the "detailed explanation and function introduction of abstract classes in C language or C++". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. let's study and learn the detailed explanation and function of abstract classes in C language or C++.

Catalogue

Overview

Abstract class vs concrete class

Case

The role of abstract classes

Summary

Overview

Abstract classes (abstract class) are classes that are inherited as base classes instead of defining objects. Because an abstract class is often used as a base class, it is often called an abstract base class (abstract base class).

The only purpose of defining an abstract class is to create a derived class. We need to define derived classes with different functions on the basis of abstract classes, and then use these derived classes to create objects.

Abstract class vs concrete class

All classes that contain pure virtual functions are abstract. Pure virtual functions do not need to be implemented, so they cannot be called, and abstract classes cannot create objects. The function of abstract class is to serve as the common base class of a class cluster, that is, to provide a common interface for a class cluster.

In many good object-oriented systems, the top of the hierarchy is an abstract class, and even several layers are abstract classes. If all pure imaginary numbers of the base class are defined in a new class derived from the abstract class. Then these functions are given specific functions and can be called. This derived class is not an abstract class, but a concrete class (concrete class) that can be used to define objects.

If all pure functions are defined in a derived class, the derived class is still an abstract class and cannot be used to define objects.

Case

Create the base class Shape (shape) as an abstract class. Point (point), Circle (circle) are the direct and indirect derivative classes of Shape class, and Cylinder (cylinder) is the indirect derivative class of Shape.

Shape class:

# ifndef PROJECT6_SHAPE_H#define PROJECT6_SHAPE_Hclass Shape {public: virtual double area () const {return 0.0;} virtual double volume () const {return 0.0;} virtual void shapeName () const = 0;}; # endif / / PROJECT6_SHAPE_H

Point:

# ifndef PROJECT6_POINT_H#define PROJECT6_POINT_H#include # include "Shape.h" using namespace std;class Point: public Shape {private: double a, double public: Point (double p1, double p2): a (p1), b (p2) {}; virtual void shapeName () const {/ / a pair of virtual functions to redefine cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report