Advertisement

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


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


virtual void input() = 0;

virtual void add() = 0;

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

#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 << "Sum is:" << c;
   }
};

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

   return 0;
}

Output:

Enter two numbers:10 20
Sum is:30