Interfaces in C++

Interfaces and Abstract Classes

An interface describes the behavior or capabilities of a C++ class without committing to a particular implementation of that class.

The C++ interfaces are implemented using abstract classes and these abstract classes should not be confused with data abstraction which is a concept of keeping implementation details separate from associated data.

A class is made abstract by declaring at least one of its functions as pure virtual function. A pure virtual function is specified by placing “= 0” in its declaration.

class Base {
	virtual int print() {
		cout << "Print from Base Class." << endl;

The purpose of an abstract class is to provide an appropriate base class from which other classes can inherit. Abstract classes cannot be used to instantiate objects and serves only as an interface. Attempting to instantiate an object of an abstract class causes a compilation error.

Thus, if a subclass of an abstract class needs to be instantiated, it has to implement each of the virtual functions. Failure to override a pure virtual function in a derived class, then attempting to instantiate objects of that class, is a compilation error.

Classes that can be used to instantiate objects are called concrete classes.

Related Posts

Template Specializations in C++

A template specialization is a separate definition in which the...

Copy Constructors in C++

The constructor that takes a single parameter that is a...

Friend Class and Function in C++

A friend class can access private and protected members of...

Templates in C++

A template is a blueprint or formula for creating a...

Polymorphism in C++

Polymorphism means having more than one function with the same...

Interfaces in C++

An interface describes the behavior or capabilities of a C++...