ABSTRACT CLASSES IN C++

Introduction:

In this article, we are going to learn about abstract classes in c++. Abstract classes are essential to providing an abstraction to the code to make it reusable and extendable.

Abstract classes:

A class that contains at least one pure virtual function is known as an abstract class. it is designed only to act as a base class. The function will be declared in the Shape class; however, it cannot be defined there as the formula for the area is different for each shape. A non-specific shape does not have an area, but rectangles and triangles do.

Abstract classes are used to express broad concepts from which more concrete classes can be derived. An abstract class-type object cannot be created.

Characteristics of the abstract classes:

The Abstract class type cannot be instantiated.

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

Abstract classes are mostly used for upcasting.

Abstraction is allowing derived classes to access their interface.

Restrictions of Abstract Class:

The following are not possible to use the abstract classes.

Member data or variables

Forms of debate

Types of function output

Conversions that are made explicitly

Argument types

Pure Virtual Function:

Before knowing about the pure virtual function let us know about the virtual function first.

A virtual function is a member function that is declared within a base class and is redefined by the derived class.

See also  Module introduction & what we will learn throughout this module

A pure virtual function is a virtual function with no logic. It is declared by assigning 0 at the time of declaration.

Syntax:  

C-lass classname //abstract class
{
//data members                                                        
public:
//pure virtual function
/* Other members */
};

Program:

#include<iostream>
using namespace std;
class Shape {
  public:
    virtual int perimeter() = 0;
    void width(int w) {
      shape_width = w;
    }
    void height(int h) {
      shape_height = h;
    }
  protected:
    int shape_width;
    int shape_height;
};
class Rectangle: public Shape {
  public: 
    int perimeter() {
      return (2 * (shape_width + shape_height));
    }
};
class Square: public Shape {
  public: 
    int perimeter() {
      return (4 * shape_width);
    }
};
int main() {
  Rectangle R;
  Square S;
  R.width(10);
  R.height(5);
  S.width(10);
  cout << "Perimeter of the Rectangle: " << R.perimeter() << endl;
  cout << "Perimeter of the Square: " << S.perimeter() << endl;
  return 0;
}

Output:

The perimeter of the Rectangle: 30
The perimeter of the Square: 40

Code explanation: The function perimeter() is a pure virtual function, the “virtual” keyword is used and it is assigned a value of 0. In the derived classes, rectangle and shape redefine the pure virtual function.

Points to remember:

The abstract keyword must be used when declaring an abstract class.

Subclasses of abstract classes can be formed, but they cannot be instantiated.

An abstract class is one that has been declared to be abstract; it may or may not contain abstract methods.

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

Data abstraction can be used to protect data from being accessed by unauthorized methods.

The abstract class helps the user to avoid writing low-level code.

Leave a Comment

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

we provide projects, courses, and other stuff for free. in order for running we use Google ads to make revenue. please disable adblocker to support us.