C++ Program to add two numbers using parameterized constructor

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


class Add {
   int c;
public:

   Add(int a, int b) {
       //statement
   }
};

C++ Program to add two numbers using parameterized constructor

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

#include<iostream>
using namespace std;

class Add {
   int c;
public:

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

   void display() {
       cout << "Sum is:" << c;
   }
};

int main() {

   int a, b;

   cout << "Enter two numbers:";
   cin >> a >> b;

   Add obj(a, b);
   obj.display();

   return 0;
}

Output:

Enter two numbers:10 20
Sum is:30