C++ Program to add two numbers using pure virtual function


In this program, You will learn how to add two numbers using pure virtual function in C++.


virtual void input() = 0;

virtual void add() = 0;


Example: How to add two numbers using pure virtual function in C++

Run it Now
#include<iostream>
using namespace std;

class First {
public:
   virtual void input() = 0;
   virtual void add() = 0;
};

class Second : public First {
public:
   int a, b, c;

   void input() {
       cout << "Enter Two Numbers :";
       cin >> a>>b;
   }

   void add() {
       c = a + b;
       cout << "\nSum is :" << c;
   }
};

int main() {
   First *f;
   Second s;
   f = &s;
   f->input();
   f->add();

   return 0;
}

Output:

Enter Two Numbers :12 13
Sum is :25