Abstract Class in c++

An abstract class in C++ has at least one pure virtual function by definition. In other words, a function that has no definition. The abstract class's descendants must define the pure virtual function; otherwise, the subclass would become an abstract class in its own right.

Abstract classes are used to express broad concepts from which more concrete classes can be derived. An abstract class type object cannot be created. To abstract class types, however, you can use pointers and references. Declare at least one pure virtual member feature when creating an abstract class. The pure specifier (= 0) syntax is used to declare a virtual function.

Take a look at the example in virtual functions. The aim of the class is to provide general functionality for shape, but objects of type shape are much too general to be useful. Shape is therefore a suitable candidate for an abstract class:

Syntax:      

C-lass classname //abstract class

{

//data members                                                        

public:

//pure virtual function

/* Other members */

};

Full Stack Web Developer Course

To become an expert in MEAN StackView Course
Full Stack Web Developer Course

Abstract Class Characteristics

The Abstract class type cannot be instantiated, but pointers and references to it can be generated.

In addition to normal functions and variables, an abstract class may have a pure virtual function.

Abstract classes are mostly used for up casting, allowing derived classes to access their interface. All pure virtues must be implemented by classes that inherit from an Abstract Class.

Why Can't We Make an Abstract Class Object?

When we construct a pure virtual function in Abstract, we reserve a slot in the VTABLE(discussed in the previous topic) for a function, but we don't put any address in that slot. As a result, the VTABLE will be unfinished.

Since the VTABLE for the Abstract class is missing, the compiler will refuse to let you create an object for it and will show an error message if you try.

class Shapes {

public:

virtualint Area() = 0; // Pure virtual function is declared as follows.

         // Function to set width.

voidsetval_width(int w) {

width = w;

         }

         // Function to set height.

voidsetval_height(int h) {

height = h;

         }

protected:

int width;

int height;

};

Area () is declared with the pure specifier (= 0), which is the only distinction between this declaration and the previous one.

Implementation of abstract class as follows

Input:

Abstract_Class_in_cpp_1Abstract_Class_in_cpp_2.  

Output:

Abstract_Class_in_cpp_3 

Restriction of Abstract Class

It is not possible to use abstract classes for the following purposes:

  • Member data or variables
  • Forms of debate
  • Types of function output
  • Conversions that are made explicitly

The outcome is unknown whether an abstract class's function Object () { [native code] } explicitly or indirectly calls a pure virtual method. Constructors and destructors for abstract groups, on the other hand, have the ability to call other member functions.

Definitions of Pure Virtuality

Pure virtual functions can be specified briefly in the Abstract class, which will be shared by all derived classes. You are still unable to construct objects of the Abstract class.

Furthermore, the Pure Virtual function must be specified separately from the class description. The compiler will throw an error if you define it within the class description. It's against the law to define pure virtual inline.

Input:

Abstract_Class_in_cpp_4.

Output:

Abstract_Class_in_cpp_5 

Add Another Star to Your Performance Evaluation

Learn from industry experts for FREEStart Learning
Add Another Star to Your Performance Evaluation

How Abstraction is Important in Daily Life

Another real-life example of abstraction is the ATM machine; we all use the ATM machine to conduct operations such as cash withdrawal, money transfer, retrieving mini-statements, and so on, but we have no access to the ATM's internal information. Data abstraction can be used to protect data from being accessed by unauthorized methods.

Difference Between Interface and Abstract Class

Interface

Abstract Class

Only an interface can be inherited by an interface

The Extended keyword allows an abstract class to inherit another class and enforce an interface.

The only way to implement an interface is to use the implements keyword.

The extends keyword may be used to inherit an abstract class

Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!

Conclusion

The abstract keyword must be used when declaring an abstract class. It can include both abstract and non-abstract methods. It may also include constructors and static methods. It can have final methods, which prevent the subclass from changing the method's body. An abstract class in C++ is one that has at least one pure virtual function by definition. In other words, a function that has no definition. The abstract class's descendants must define the pure virtual function; otherwise, the subclass would become an abstract class in its own right. Some important characters of abstract classes with suitable examples and also virtual functions have been discussed. An abstract class is one that has been declared to be abstract; it may or may not contain abstract methods. Subclasses of abstract classes can be formed, but they cannot be instantiated. When an abstract class is subclassed, the subclass typically offers implementations for all of its parent class's abstract methods.

It is impossible to instantiate an abstract class. Abstract methods and accesses can be found in abstract classes. Since the two modifiers have opposite definitions, it is not possible to change an abstract class with the sealed modifier.

In this article, you have learned about stack, implementation, operations with syntax along with the examples. We can really talk more about this topic. So, if you have any questions for us, leave them in the comments section of this article, and our experts will get back to you on them, as soon as possible!

What makes you really stand out as a successful professional is the expertise in any given area. So, to gain more expertise in C++ programming language, you can join our Simplilearn’s C++ certification training course. In addition to this, we have also made a special selection of courses available for free for all the learners. Enroll in our free courses and revolutionise your professional career with certificates, specialisations, and dozens of other courses.

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.