Javatpoint Logo
Javatpoint Logo

What is an abstract class in C++?

By definition, a C++ abstract class must include at least one pure virtual function. Alternatively, put a function without a definition. Because the subclass would otherwise turn into an abstract class in and of itself, the abstract class's descendants must specify the pure virtual function.

Broad notions are expressed using abstract classes, which can then be utilized to construct more specific classes. You cannot make an object of the abstract class type. However, pointers and references can be used to abstract class types. When developing an abstract class, define at least one pure virtual feature. A virtual function is declared using the pure specifier (= 0) syntax.

Consider the example of the virtual function. Although the class's objective is to provide basic functionality for shapes, elements of type shapes are far too general to be of much value. Because of this, the shape is a good candidate for an abstract class:

Code

What are the characteristics of abstract class?

Although the Abstract class type cannot be created from scratch, it can have pointers and references made to it. A pure virtual function can exist in an abstract class in addition to regular functions and variables. Upcasting, which lets derived classes access their interface, is the main usage of abstract classes. Classes that descended from an abstract class must implement all pure virtues.

What are the restrictions to abstract class?

The following uses of abstract classes are not permitted:

  1. Conversions made consciously
  2. Member data or variables
  3. Types of function output
  4. Forms of debate

It is unknown what happens when a pure virtual method is called explicitly or indirectly by the native code function Object () of an abstract class. Conversely, abstract group constructors and destructors can call additional member functions.

Although the constructors of the abstract class are allowed to call other member functions, if they either directly or indirectly call a pure virtual function, the outcome is unknown. But hold on! What exactly is a pure virtual function?

Let's first examine virtual functions in order to comprehend the pure virtual function.

A member function that has been redefined by a derived class from a base class declaration is referred to as a virtual function.

A virtual function that lacks definition or logic is known as an abstract function or a pure virtual function. At the time of declaration, 0 is assigned to it.

How Important Abstraction Is in Daily Life?

The ATM machine is another example of abstraction in everyday life;. However, we all use the ATM to do tasks like cash withdrawal, money transfers, generating mini-statements, and so on, and we have no access to the ATM's internal data. Data protection techniques like data abstraction can prevent unauthorized access to data.

What is the difference between abstract class and interface?

Interface Abstract class
An interface can only inherit from another interface. With the Extended keyword, an abstract class can enforce an interface and inherit from another class.
Use of the implements keyword is required in order to implement an interface. Use the extends keyword to inherit from an abstract class.

What is an example of an abstract class?

Consider developing a calculator that will output the shape's perimeter when it is entered. Consider the type of programming you would use to create such a calculator. By creating distinct functions inside the Shape class, you may start with a few basic forms and hardcode the perimeter.

This is how the class might appear:

What is an example of an abstract class?

Consider developing a calculator that will output the shape's perimeter when it is entered. Consider the type of programming you would use to create such a calculator. By creating distinct functions inside the Shape class, you may start with a few basic forms and hardcode the perimeter.

This is how the class might appear:

Output:

What is an abstract class in C++

While OOP advises that we should attempt to adhere to real-world reasoning, this will still function. As a result, we can create the class Shape as the parent class, with the classes Square and Rectangle acting as the children. If you want to add something new later, you can do it in the child class, which will make the program simpler to maintain. We must use Abstract Classes to put this feature into practice. At least one pure virtual function is required for abstract classes in C++. In order to prevent the subclass from becoming an abstract class, the abstract class's inheriting classes must specify the pure virtual function.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA