C++ Program to add two numbers using class and object


In this program, You will learn how to add two numbers using class and object in C++.


20 = 10 + 10

40 = 20 + 20


Example: How to add two numbers using class and object in C++

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

class Add {

public:
    int addition(int x, int y) {
        return x + y;
    }
};

int main() {

    int x, y, s;

    cout << "Enter two numbers :";
    cin >> x >> y;

    Add obj;
    s = obj.addition(x, y);

    cout << "\nThe sum of two numbers is :" << s;

    return 0;
}

Output:

Enter two numbers :10 20
The sum of two numbers is :30