Skip to main content

C++ Interfaces

Interface are providing the behavior or designing strategy of base class. In c++ abstract class are used to pure virtual function.

Pure virtual function are start with keyword of virtual. And there are specialty of this function there is no body portion (definition) of this function. see the declaration of pure virtual function

 virtual return_type function_name(parameter_list)=0;
/*--------
  //Explanation
  virtual : keyword
  return_type: return data type
  function_name: name of function
  parameter_list: arguments
  =0  : no body
----------*/

for example

virtual int info()=0;
virtual void salary()=0;

Important point, any class are contain at least one pure virtual function. that is called abstract class. And abstract class is provide guaranty those function are overridden by inherited derived class. it means derived class are provide real definition of pure virtual function. see the example and understand this point.

/*
  Abstract Class Example In C++ 
*/
#include<iostream>
using namespace std;

class Programming{
  private:
    //data member
    int data,keyword;
  public:
    //pure virtual function
    virtual void code()=0;
    void setValue(int,int);
    void display();
};
class Cpp: public Programming{
  public:
    //public member function
    void code();  
};
class Java:public Programming{
  public:
    //public member function
    void code();
};

void Programming::setValue(int data,int keyword){
  this->data=data;
  this->keyword=keyword;
}
void Programming::display(){
  //display private data
  cout<<" data    :"<<data<<endl;
  cout<<" keyword :"<<keyword<<endl;
}
void Cpp :: code(){
  cout<<"C Programming Code"<<endl;
}

void Java:: code(){
  cout<<"Java Programming Code"<<endl;
}
int main(){
  //Create Object of Derived class
  Cpp cpp;
  Java java;
  //set data value
  cpp.setValue(10,95);
  java.setValue(70,100);

  //execute public member function
  cpp.code();
  cpp.display();
  java.code();
  java.display();
}
Output
C Programming Code
 data    :10
 keyword :95
Java Programming Code
 data    :70
 keyword :100

In this program note that class Programming are have one pure virtual function (code()). This function are no body in Programming class. And derived class are overloaded this function. If derived class are not overloading virtual function then this will produce an error. see the example.

/*
  Abstract Class Example In C++ 
*/
#include<iostream>
using namespace std;

class Programming{
  private:
    //data member
    int data,keyword;
  public:
    //pure virtual function
    virtual void code()=0;
};
class Cpp: public Programming{
//logic
};
class Java:public Programming{
  //logic
};
int main(){
  Cpp cpp;
//logic here
}
Error:
 error: cannot declare variable 'cpp' to be of abstract type 'Cpp'
  Cpp cpp;
      ^~~
 because the following virtual functions are pure within 'Cpp':
 class Cpp: public Programming{
       ^~~
'virtual void Programming::code()'
 virtual void code()=0;




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment