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 Full Stack Developer - MERN Stack. In addition to this, we have also made a special selection of courses available for free for all the learners. 

FAQs

1. What is an abstract class in C++?

An abstract class in C++ is a class that cannot be instantiated on its own and is designed to be a base class for other classes. It contains at least one pure virtual function, which is declared by assigning 0. For example:

class AbstractClass {
public:
    virtual void pureVirtualFunction() = 0; // Pure virtual function
};

Classes derived from the abstract class must implement the pure virtual function to be instantiated.

2. Why do we use abstract classes in C++?

Abstract classes are used in C++ to define interfaces and enforce a contract for derived classes. They allow you to specify a set of methods that must be implemented by any subclass, ensuring a consistent interface and promoting code reuse and polymorphism.

3. How do you define and implement a pure virtual function in an abstract class?

A pure virtual function is defined in an abstract class by assigning 0 to the virtual function declaration. It must be overridden by any derived class. Here is an example:

class AbstractClass {
public:
    virtual void pureVirtualFunction() = 0; // Pure virtual function
};

class DerivedClass : public AbstractClass {
public:
    void pureVirtualFunction() override {
        // Implementation of the pure virtual function
    }
};

4. Can an abstract class have non-pure virtual functions and member variables?

Yes, an abstract class can have non-pure virtual functions and member variables. Non-pure virtual functions can provide default behavior inherited or overridden by derived classes. At the same time, member variables can store common data derived classes share. For example:

class AbstractClass {
public:
    virtual void pureVirtualFunction() = 0; // Pure virtual function
    virtual void nonPureVirtualFunction() {
        // Default implementation
    }
    void concreteFunction() {
        // Concrete function implementation
    }
protected:
    int memberVariable;
};

5. What happens if a derived class does not override all pure virtual functions of an abstract class?

If a derived class does not override all pure virtual functions of an abstract class, it becomes an abstract class and cannot be instantiated. It must provide concrete implementations for all inherited pure virtual functions to instantiate the derived class. For example:

class AbstractClass {
public:
    virtual void pureVirtualFunction1() = 0;
    virtual void pureVirtualFunction2() = 0;
};

class PartiallyDerivedClass : public AbstractClass {
public:
    void pureVirtualFunction1() override {
        // Implementation of pureVirtualFunction1
    }
    // pureVirtualFunction2 is not overridden, so PartiallyDerivedClass is abstract
};

class FullyDerivedClass : public PartiallyDerivedClass {
public:
    void pureVirtualFunction2() override {
        // Implementation of pureVirtualFunction2
    }
};

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: 5 Aug, 2024

6 Months$ 8,000
Full Stack Java Developer

Cohort Starts: 30 Jul, 2024

6 Months$ 1,449
Full Stack Developer - MERN Stack

Cohort Starts: 30 Jul, 2024

6 Months$ 1,449
Automation Test Engineer

Cohort Starts: 7 Aug, 2024

11 Months$ 1,499