- C++ Data Structures
- C++ Basic Input/Output
- C++ Date & Time
- C++ References
- C++ Pointers
- C++ Strings
- C++ Arrays
- C++ Numbers
- C++ Functions
- C++ Decision Making
- C++ Loop Types
- C++ Operators
- C++ Storage Classes
- C++ Modifier Types
- C++ Constants/Literals
- C++ Variable Scope
- C++ Variable Types
- C++ Data Types
- C++ Comments
- C++ Basic Syntax
- C++ Environment Setup
- C++ Overview
- C++ Home
C++ Object Oriented
- C++ Interfaces
- C++ Encapsulation
- C++ Abstraction
- C++ Polymorphism
- C++ Overloading
- C++ Inheritance
- C++ Classes & Objects
C++ Advanced
- C++ Web Programming
- C++ Multithreading
- C++ Signal Handling
- C++ Preprocessor
- C++ Templates
- C++ Namespaces
- C++ Dynamic Memory
- C++ Exception Handling
- C++ Files and Streams
C++ Useful Resources
- C++ Discussion
- C++ Useful Resources
- C++ Standard Library
- C++ STL Tutorial
- C++ Object Oriented
- C++ Quick Guide
- C++ Questions and Answers
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Interfaces in C++ (Abstract Classes)
An interface describes the behavior or capabipties 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 as follows −
class Box { pubpc: // pure virtual function virtual double getVolume() = 0; private: double length; // Length of a box double breadth; // Breadth of a box double height; // Height of a box };
The purpose of an abstract class (often referred to as an ABC) 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 ABC needs to be instantiated, it has to implement each of the virtual functions, which means that it supports the interface declared by the ABC. 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.
Abstract Class Example
Consider the following example where parent class provides an interface to the base class to implement a function called getArea() −
#include <iostream> using namespace std; // Base class class Shape { pubpc: // pure virtual function providing interface framework. virtual int getArea() = 0; void setWidth(int w) { width = w; } void setHeight(int h) { height = h; } protected: int width; int height; }; // Derived classes class Rectangle: pubpc Shape { pubpc: int getArea() { return (width * height); } }; class Triangle: pubpc Shape { pubpc: int getArea() { return (width * height)/2; } }; int main(void) { Rectangle Rect; Triangle Tri; Rect.setWidth(5); Rect.setHeight(7); // Print the area of the object. cout << "Total Rectangle area: " << Rect.getArea() << endl; Tri.setWidth(5); Tri.setHeight(7); // Print the area of the object. cout << "Total Triangle area: " << Tri.getArea() << endl; return 0; }
When the above code is compiled and executed, it produces the following result −
Total Rectangle area: 35 Total Triangle area: 17
You can see how an abstract class defined an interface in terms of getArea() and two other classes implemented same function but with different algorithm to calculate the area specific to the shape.
Designing Strategy
An object-oriented system might use an abstract base class to provide a common and standardized interface appropriate for all the external apppcations. Then, through inheritance from that abstract base class, derived classes are formed that operate similarly.
The capabipties (i.e., the pubpc functions) offered by the external apppcations are provided as pure virtual functions in the abstract base class. The implementations of these pure virtual functions are provided in the derived classes that correspond to the specific types of the apppcation.
This architecture also allows new apppcations to be added to a system easily, even after the system has been defined.
Advertisements