C++ Program to add two numbers using function overriding


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


class First { 
   public void sum(int, int) { 
        //statement
   }
};

class Second : public First { 
     public void sum(int, int) { 
        //statement
     }
};

Example: How to add two numbers using function overriding in C++.

#include<iostream>
using namespace std;

class First {
public:

   void add(int x, int y) {
       cout << "Sum in first:" << x + y;
   }
};

class Second : public First {
public:

   void add(int x, int y) {
       cout << "Sum in second:" << x + y;
   }
};

int main() {

   Second obj;
   obj.add(10, 20);

   return 0;
}

Output:

Sum in second:30

Xiith is created for educational, experimental, and schooling purpose. Examples on Xiith are made easier to make a better or basic understanding. Tutorials, testimonials, and examples are continuously checked to avoid delusion, but we cannot take complete responsibility of all programs on Xiith. When you are operating this site, you have to agree to read and accept our terms of use, cookie and privacy policy.Copyright © 2020 by Xiith. All Rights Reserved. About us.