C++ Program to add two numbers using constructor overloading


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


20 = 10 + 10

30 = 20 + 10


Example: How to add two numbers using constructor overloading in C++

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

class Test {
   int c;
public:

   Test() {
       cout << "\nThe Sum is :";
   }

   Test(int a, int b) {
       c = a + b;
   }

   void display() {
       cout << c;
   }
};

int main() {

   int a, b;

   cout << "Enter Two Numbers :";
   cin >> a>>b;

   Test t;
   Test tt(a, b);
   tt.display();

   return 0;
}

Output:

Enter Two Numbers :12 13
The Sum is :25