0% found this document useful (0 votes)
1 views

Abstract Classes

In C++, an abstract class is a class that contains at least one pure virtual function and cannot be instantiated on its own, serving as a blueprint for derived classes. It provides an interface for derived classes to implement specific behaviors while allowing for polymorphism. Characteristics of abstract classes include having both concrete and virtual functions, the ability to contain data members, and being incomplete due to the presence of pure virtual functions.

Uploaded by

Sneha Soni
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Abstract Classes

In C++, an abstract class is a class that contains at least one pure virtual function and cannot be instantiated on its own, serving as a blueprint for derived classes. It provides an interface for derived classes to implement specific behaviors while allowing for polymorphism. Characteristics of abstract classes include having both concrete and virtual functions, the ability to contain data members, and being incomplete due to the presence of pure virtual functions.

Uploaded by

Sneha Soni
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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;
}
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.

You might also like