In programming, 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 */

};

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

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 

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

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

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 Post Graduate Program in Full Stack Web Development. In addition to this, we have also made a special selection of courses available for free for all the learners. 

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 17 Jun, 2024

6 Months$ 8,000
Full Stack Developer - MERN Stack

Cohort Starts: 24 Apr, 2024

6 Months$ 1,449
Automation Test Engineer

Cohort Starts: 1 May, 2024

11 Months$ 1,499
Full Stack Java Developer

Cohort Starts: 14 May, 2024

6 Months$ 1,449