Abstract Class in C++ With Simple Example

A class with one or more pure virtual functions is an Abstract Class in C++, Objects of abstract class can’t be created

  • Abstract Class is a class which contains atl east one Pure Virtual function in it.
  • Abstract classes are used to provide an Interface for its sub classes.
  • Classes inheriting an Abstract Class must provide definition to the pure virtual function, otherwise they will also become abstract class.
  • But pointer of abstract class can hold address of derived class

Simple Example Math Symbol Problem

Create an abstract class MathSymbol may provide a pure virtual function doOperation(), and create two more classes Plus and Minus implement doOperation() to provide concrete implementations of addition in Plus class and subtraction in Minus class.

Algorithm for Math Symbol Problem

  • Create Mathsymbol abstract class in c++ with doOperation() pure virtual function.
  • Create plus class derived from Mathsymbol.
  • Create minus class dervied from Mathsymbol.
  • Perform addition in plus class using doOperation() pure virtual function.
  • Perform subtraction in minus class using doOperation() pure virtual function.
  • Print each values in respective classes.

Implementation Abstract Class in C++

  • Already learnt creating classes
  • Learnt about Inheritance
  • Define Pure virtual function in Abstract(base) class.
  • Inherit virtual function for distinct operations.
  • Already learnt about how to display the output.

Class Diagram using Dia

Class Diagram using Dia

Simple Example

  • Some classes exist logically but not physically.
  • Example : Mathsymbol

–Mathsymbol M;
–Mathssymbol makes sense only as a base of some classes derived from it. Serves as a “category”
–Hence instantiation of such a class must be prevented

class Mathsymbol //Abstract
{
public :
//Pure virtual Function
virtual void doOperation() = 0;
}

Mathsymbol M; // error : variable of an abstract class

  • A pure virtual function not defined in the derived class remains a pure virtual function.
  • Hence derived class also becomes abstract

class Plus : public Mathsymbol{       //No doOperation() – Abstract
public :
void print(){
cout << “Doing Addition” << endl;
}

class Minus : public Mathsymbol {
public :
void doOperation(){    // Override Mathsymbol:: doOperation()
int a=10,b=5;

cout << “Subtration is = ” <<a-b<< endl;
}

Minus m;   // Valid
Plus p;   // error : variable of an abstract class

  • It is still possible to provide definition of a pure virtual function in the base class
  • The class still remains abstract and functions must be redefined in the derived classes, but a common piece of code can be kept there to facilitate reuse
  • In this case, they can not be declared inline
class Mathsymbol { //Abstract
public :
virtual void doOperation() = 0;};// OK, not defined inlinevoid Mathsymbol::doOperation(){
cout << “Arithmetic” << endl;
}
class Minus : public Mathsymbol

{
public :
void doOperation(){
Mathsymbol::doOperation(); //Reuse
cout <<“Subtraction”<< endl;
}

Example: Pure Virtual Functions

class A {

public:

  virtual void x() = 0;

  virtual void y() = 0;

};

  • A is an abstract (base) class
  • Similar to an interface in Java
  • Declares pure virtual functions (=0)
  • May also have non-virtual methods, as well as virtual methods that are not pure virtual
class B : public A {

public:

  virtual void x();

};

  • Derived classes override pure virtual methods
  • B overrides x(), C overrides y()
class C : public B {

public:

  virtual void y();

};

  • Can’t instantiate an abstract class
  • class that declares pure virtual functions
  • or inherits ones that are not overridden
  • A and B are abstract, can create a C
int main () {

  A * ap = new C;

  ap->x ();

  ap->y ();

  delete ap;

  return 0;

};

  • Can still have a pointer or reference to an abstract class type
  • Useful for polymorphism

Pure Virtual Functions: Summary

  • Pure virtual functions are useful because they make explicit the abstractness of a class
  • Tell both the user and the compiler how it was intended to be used
  • Note : It is a good idea to keep the common code as close as possible to the root of you hierarchy

Difference b/w Virtual and Pure Virtual Function

A pure virtual function makes the class it is defined for abstract. Abstract classes cannot be instantiated. Derived classes need to override/implement all inherited pure virtual functions. If they do not, they too will become abstract. In C++, a class can define a pure virtual function that has an implementation.

Pure Virtual Function

  • A pure virtual function is a function that has the notation “= 0” in the declaration of that function. Why we would want a pure virtual function and what a pure virtual function looks like is explored in more detail below.
  • Simple Example of a pure virtual function in C++
  • class SomeClass

{ public: virtual void pure_virtual() = 0; // a pure   virtual function // note that there is no function body

};

Runtime Polymorphism

  • Definition
    • Ability to take more than one form
  • An essential feature of an OO Language
  • It builds upon Inheritance
  • Allows run-time interpretation of object type for a given class hierarchy
    • Also Known as “Late Binding”
  • Implemented in C++ using virtual functions

Dynamic Binding

  • Is the run time determination of which function to call for a particular object of a derived class based on the type of the argument
  • Declaring a member function to be virtual instructs the compiler to generate code that guarantees dynamic binding
  • Dynamic binding requires pass-by-reference
Read More Topics
Image Processing
C++ Inheritance
Dynamic Memory Allocation
Program Design in C Language

About the author

Santhakumar Raja

Hi, This blog is dedicated to students to stay update in the education industry. Motivates students to become better readers and writers.

View all posts

Leave a Reply