C++ - SPLessons

C++ Interface

Chapter 25

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

C++ Interface

C++ Interface

shape Introduction

CPP Interface gives the overview of class before implementation of that class. Before going to interfaces, it is necessary to learn about pure virtual functions and abstract classes.

Pure virtual functions

shape Description

The virtual functions declared in the parent class without giving any meaningful definition to it are called as pure virtual functions.

Pure virtual functions are again defined in its own way in the child class. These functions do not contain any body and is equated to zero.

virtual void s()=0;

shape Example

class Shape 
{
public:
      Shape( int x=0, int y=0)
      {
         breadth = x;
         height = y;
      } 

protected:
      int breadth, height;

      virtual int area() = 0;  // pure virtual function
   
};

Abstract classes

shape Description

Abstract classes contains minimum of one pure virtual functions. Mainly, abstract parent class is taken into consideration while explaining interface.

Abstract parent class using pure virtual functions does not have any right to create any objects. Instead, it helps in defining the function used by the child class and act act as an interface or a mediator to the child class. Concrete classes have the capability of creating the objects for parent class using pure virtual functions.

Without defining a body to pure virtual functions in parent class by child class is useless. It again becomes a abstract class.

shape Example

#include<iostream>
using namespace std;
class parent_class         //Abstract parent class
{
	 public:
	 virtual void splessons() = 0;   //Pure Virtual Function
};

void parent_class :: splessons()         //Pure Virtual function definition
{
	cout << "Pure Virtual function definition\n";
}

class child_class:public parent_class
{
	 public:
	 void splessons()
	 { 
	  cout << "Implementation of Virtual Function in child class"; 
	 } 
}; 
int main() 
{ 
	  parent_class* p; 
	  child_class c; 
	  p = &c; 
	  p->splessons();
}

Output

Implementation of Virtual Function in child class

Interfaces

shape Description

CPP Interface is the class which gives an index about the operations to be performed by the child class on pure virtual functions, which are present in parent class.

  • Interfaces contains only pure virtual functions. No member variables are considered here.
  • CPP Interface allows multiple inheritance whereas abstract class does not allow multiple inheritance.But, interface should provide implementation for all the abstract methods inherited. Interface does not support updation of parent classes.
  • Interface is mostly used when there is similar functionality between independent classes.

shape Example

#include <iostream>
using namespace std;
 
// Parent class
class Shape 
{
public:
   // pure virtual function providing interface
   virtual int getArea() = 0;
   void setBase(int b)
   {
      base = b;
   }
   void setHeight(int h)
   {
      height = h;
   }
protected:
   int base;
   int height;
};
 
// Child classes
class Triangle : public Shape
{
public:
   int getArea()
   { 
      return (base * height)/2; 
   }
};
class Parallelogram : public Shape
{
public:
   int getArea()
   { 
      return (base * height); 
   }
};
 
int main(void)
{
   Triangle Tri;
   Parallelogram  Par;
 
   Tri.setBase(5);
   Tri.setHeight(8);
   cout << "Area of Triangle is : " << Tri.getArea() << endl;

   Par.setBase(10);
   Par.setHeight(20);
   cout << "Area of parallelogram is : " << Par.getArea() << endl; 

   return 0;
}

Output

Area of Triangle is : 20
Area of parallelogram is : 200

Summary

shape Key Points

  • Overview of a class is nothing but a interface.
  • Virtual functions without definition is called as pure virtual function.
  • A class with at-least one pure virtual function is called as abstract class.
  • CPP Interface is list of operations done by child class on pure virtual functions.

shape Programming
Tips

When a interface is changed, all the classes has to be changed associated with that interface.