C++ Interfaces (Abstract Classes)
An interface describes the behavior and functionality of a class without requiring a specific implementation of the class.
C++ interfaces are implemented usingabstract classes, and abstract classes should not be confused with data abstraction, which is a concept that separates implementation details from related data.
If a class has at least one function declared as a pure virtual function, then the class is an abstract class. A pure virtual function is specified by using "= 0" in the declaration, as shown below:
class
Box
{
public
:
// Pure virtual function
virtual
double
getVolume
(
)
=
0
;
private
:
double
length
;
// length
double
breadth
;
// width
double
height
;
// height
}
;
Designingabstract classes(usually called ABC) is intended to provide a suitable base class for other classes to inherit from. An abstract class cannot be used to instantiate objects; it can only serve asan interfaceto be used. Attempting to instantiate an object of an abstract class will cause a compilation error.
Therefore, if a subclass of an ABC needs to be instantiated, every pure virtual function must be implemented, which also means C++ supports declaring interfaces using ABCs. If you try to instantiate an object of the class without overriding the pure virtual function in the derived class, it will cause a compilation error.
A class that can be used to instantiate objects is called aconcrete class.
Example of an abstract class
Please see the example below; the base class Shape provides an interface getArea(), which is implemented differently in the two derived classes Rectangle and Triangle getArea():
Example
#include
<
iostream
using
namespace
std;
// Base class
class
Shape
{
public
:
// Pure virtual function providing the interface framework
virtual
int
getArea
(
)
=
0
;
void
setWidth
(
int
w
)
{
width
=
w
;
}
void
setHeight
(
int
h
)
{
height
=
h
;
}
protected
:
int
width
;
int
height
;
}
;
// Derived class
class
Rectangle
:
public
Shape
{
public
:
int
getArea
(
)
{
return
(
width
height
)
;
}
}
;
class
Triangle
:
public
Shape
{
public
:
int
getArea
(
)
{
return
(
width
height
)
/
2
;
}
}
;
int
main
(
void
)
{
Rectangle
Rect
;
Triangle
Tri
;
Rect
.
setWidth
(
5
)
;
Rect
.
setHeight
(
7
)
;
// Output the area of the object
cout
<<
"
Total Rectangle area:
"
<<
Rect
.
getArea
(
)
<<
endl
;
Tri
.
setWidth
(
5
)
;
Tri
.
setHeight
(
7
)
;
// Output the area of the object
cout
<<
"
Total Triangle area:
"
<<
Tri
.
getArea
(
)
<<
endl
;
return
0
;
}
When the code above is compiled and executed, it produces the following result:
Total Rectangle area: 35
Total Triangle area: 17
From the example above, we can see how an abstract class defines an interface getArea(), and how the two derived classes implement this same function through different area-calculation algorithms.
Design Strategy
An object-oriented system may use an abstract base class to provide an appropriate, common, standardized interface for all external applications. Then, the derived classes inherit all similar operations by inheriting the abstract base class.
The functionality provided by external applications (i.e., public functions) exists in the abstract base class as pure virtual functions. These pure virtual functions are implemented in the corresponding derived classes.
This architecture also makes it easy to add new applications to the system, even after the system has been defined.