In this blog, I’m explaining about Pure virtual function in c++

 

A pure virtual function is a function that has the notation “=0” in the declaration  of that function.

Example
#include "stdafx.h"
#include <iostream>
#include <string>
 
using namespace System;
using namespace std;
class VirtualClass
{
public:
            virtual void display(int )=0;// a pure virtual function
//There is not function body
};
class  overload
{
public:
             overload();
             int add(int a,int b)
             {
                         return a+b;
             }
             float add(float a,int b)
             {
                         return a+b;
             }
             void display(float res)
             {
                         cout<<res;
             }
 
~ overload();
 
private:
 
};
class DriveClass:overload
{
public:
void add(char str[30],char str1[30])
{
                        char result[100];
                        int j=0;
                for(int i=0;str[i]!='\0';i++)
                        {
                                    result[j]=str[i];
                                    j++;
                        }
                        for(int i=0;str1[i]!='\0';i++)
                        {
                                    result[j]=str1[i];
                                    j++;
                        }
                        for(int i=0;result[i]!='\0';i++)
                                    cout<<result[i];
                                   
            }
void  display(int res)
             {
                         cout<<res;
             }
            DriveClass();
            ~DriveClass();
private:
};
 
DriveClass::DriveClass()
{
}
 
DriveClass::~DriveClass()
{
}
 
 overload:: overload
()
{
}
overload::~ overload()
{
}
 
int main(array<System::String ^> ^args)
{
            overload obj;
            DriveClass Driobj;
            while (true)
            {
                        int exec;
                        cout<<"\n1)For Integer Addition\n";
                        cout<<"2)For Float Addition\n";
                        cout<<"3)for String Additon";
            cout<<"4)exit\n";
                        cout<<"Enter any option";
           cin>>exec;
                         switch (exec)
                         {
                         case 1:
                                     float a,b,res;
                                     cout<<"Enter two nos.";
                                     cin>>a>>b;
                                     res=obj.add(a,b);
                                     obj.display(res);
                                    break;
                         case 2:
                                     int x,y,res1;
                                      cout<<"Enter two nos.";
                                      cin>>x;cin>>y;
                                      res1=obj.add(x,y);
                                      Driobj.display(res1);
                                     break;
                         case 3:
                                     
                                     char str[30],str1[30],str2[100];
                                     cout<<"Enter a string";
                                     cin>>str;
                                     cout<<"Enter another string";
                                     cin>>str1;
                                      Driobj.add(str,str1);
                                     break;
                         case 4:
                                     exit(0);
                                     break;
                         default:
                                     break;
                         }
     }
           
            Console::ReadKey(0);
    return 0;
}

 

Leave Comment