Abstract Class in C++: All You Need to Know

Abstract Class in C++: All You Need to Know

6 mins read654 Views Comment
clickHere
Updated on Feb 28, 2023 11:59 IST

Abstract class in C++ typically contains pure virtual functions, which are defined by the derived classes. This concept allows for better organization and hierarchy in large and complex software projects.

2023_02_Abstract-class-in-c.jpg

In this tutorial, we will discuss the abstract class in C++. In C++, abstract classes are classes that have at least one pure virtual function and cannot be directly instantiated. Their primary purpose is to facilitate Upcasting, which allows their derived classes to utilize their interface. 

We will be covering the following sections:

Introduction

When creating Object Oriented Code, a primary goal is to organize the code that is easily relatable to real-world situations. Two critical concepts that should be understood are Inheritance and Abstraction.

Let’s understand both these concepts with the help of examples:

What is Inheritance in C++?

In C++, inheritance is a mechanism where a new class (the derived class) is created from an existing class (the base class). The derived class inherits properties, such as attributes and methods, from the base class.

Here’s an example

 
class Shape { // Base class
public:
void setWidth(int w) {
width = w;
}
void setHeight(int h) {
height = h;
}
protected:
int width;
int height;
};
class Rectangle: public Shape { // Derived class
public:
int getArea() {
return (width * height);
}
};
int main() {
Rectangle rect;
rect.setWidth(5);
rect.setHeight(7);
// Print the area of the object.
cout << "Total area: " << rect.getArea() << endl;
return 0;
}
Copy code

In this example, we have a base class called Shape. It has two methods to set a shape’s width and height properties. The derived class, Rectangle, is created using the public keyword to indicate that it will inherit from the Shape class. The Rectangle class defines a method to calculate the area of the rectangle using the inherited width and height properties from the Shape class. In the main function, we create an instance of the Rectangle class and set its width and height using the inherited methods from the Shape class. We then call the getArea method on the Rectangle object to calculate its area and print the result.

What is Abstraction in C++?

In C++, abstraction allows you to hide the implementation details of a class. With it, you can only expose its essential features or interface. Abstraction allows you to focus on what an object does rather than how it does it.

Here’s an example:

 
class Shape { // Abstract base class
public:
virtual int getArea() = 0; // Pure virtual function
};
class Rectangle: public Shape { // Concrete derived class
public:
Rectangle(int w, int h) {
width = w;
height = h;
}
int getArea() {
return (width * height);
}
private:
int width;
int height;
};
int main() {
Shape *shape = new Rectangle(5, 7);
cout << "Total area: " << shape->getArea() << endl;
delete shape;
return 0;
}
Copy code

In this example, we have an abstract base class called Shape, which has one pure virtual function called getArea(). A pure virtual function is a virtual function that has no implementation in the base class. It must be implemented in the derived classes. The Rectangle class is a concrete derived class that inherits from the Shape class and provides an implementation for the getArea() function. The implementation details of how the area is calculated are hidden from the user of the Rectangle class.

In the main function, we create a pointer of type Shape that points to a new Rectangle object. We then call the getArea() method on the Shape pointer to calculate the area of the rectangle. Then print the result. Since Shape is an abstract class, it cannot be instantiated directly. But it can be used as a base class for other concrete derived classes like Rectangle.

What are Abstract Classes in C++?

In C++, an abstract class is a class that contains at least one pure virtual function. An abstract class cannot be instantiated on its own. It serves as a base class for other classes to inherit from. The purpose of an abstract class is to provide an interface or a blueprint for its derived classes to follow. Derived classes can implement the pure virtual function(s) defined in the abstract class. They can also define their own member functions and data members. The abstract class is used as a way to enforce certain behavior or structure in its derived classes.

Here’s an example of an abstract class in C++:

 
class Shape { // Abstract base class
public:
virtual double getArea() = 0; // Pure virtual function
virtual double getPerimeter() = 0; // Pure virtual function
};
class Rectangle: public Shape { // Concrete derived class
public:
Rectangle(double w, double h) {
width = w;
height = h;
}
double getArea() {
return (width * height);
}
double getPerimeter() {
return (2 * (width + height));
}
private:
double width;
double height;
};
int main() {
Shape *shape = new Rectangle(5, 7);
cout << "Area: " << shape->getArea() << endl;
cout << "Perimeter: " << shape->getPerimeter() << endl;
delete shape;
return 0;
}
Copy code

In this example, we have an abstract base class called Shape with two pure virtual functions, getArea() and getPerimeter(). The Rectangle class is a concrete derived class that inherits from Shape. It provides implementations for the two pure virtual functions. The Rectangle class has a width and height attribute and implements the formulas for calculating its area and perimeter.

In the main function, we create a pointer of type Shape that points to a new Rectangle object. We then call the getArea() and getPerimeter() methods on the Shape pointer to calculate and print the area and perimeter of the rectangle. Since Shape is an abstract class, it cannot be instantiated directly. But it can be used as a base class for other concrete derived classes like Rectangle.

Characteristics of Abstract Class in C++

In C++, an abstract class has the following characteristics:

  • It cannot be instantiated: An abstract class is a class that has at least one pure virtual function, which makes it impossible to create an instance of the class.
  • It can only be used as a base class: Abstract classes are used as base classes for creating derived classes that implement the pure virtual functions.
  • It can have both concrete and virtual functions: An abstract class can have both concrete and virtual functions. Concrete functions have an implementation, while virtual functions are declared with the “virtual” keyword and have no implementation. Pure virtual functions, on the other hand, have no implementation and are declared with “= 0”.
  • It provides an interface: An abstract class provides an interface that derived classes must implement. This interface defines the behavior that the derived classes must provide, but it doesn’t define the actual implementation of that behavior.
  • It can have data members: An abstract class can have data members, just like any other class. However, the data members are typically protected or private to prevent direct access from outside the class.
  • It can be used for polymorphism: An abstract class can be used for polymorphism, allowing you to write code that can work with objects of different derived classes through a common interface provided by the abstract base class.
  • It is incomplete: Since an abstract class has at least one pure virtual function with no implementation, it is an incomplete class and cannot be used until it is fully implemented by a derived class.

Why can’t we make an Abstract Class Object?

In C++, an abstract class cannot be instantiated because it contains at least one pure virtual function that has no implementation. Pure virtual functions are declared using “= 0” at the end of their declaration, indicating that the function has no implementation in the abstract class.

Since the abstract class has at least one pure virtual function with no implementation, the abstract class is considered incomplete and cannot be used to create an object. An object must have a complete definition, which means it must have all the functions implemented so that it can be created and used.

Therefore, to use an abstract class, we must first create a concrete class that derives from the abstract class and provides the implementation of the pure virtual functions. We can then create an object of the derived class and use it to access the virtual functions of the base class through the derived class. This is known as polymorphism, and it allows us to write code that works with objects of different derived classes through a common interface provided by the abstract base class. 

Endnotes

In conclusion, abstract classes are a key concept in C++ programming and are used to provide a common interface for derived classes. An abstract class contains one or more pure virtual functions that have no implementation, which makes it impossible to create an instance of the class. Instead, we must create a concrete derived class that inherits from the abstract base class and provides the implementation for the pure virtual functions.

Go for C++ courses to learn more about the topic.

Contributed by Prerna Singh

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio

Comments