C++ program to add two numbers using class

Last updated:4th Sep 2022

In this program, you will write a c++ program to add two numbers entered by the user by using class. for example, if the user entered a=10 and b=20 then the sum of the two numbers becomes 30.

enter two number : 20   30
sum of two number: 50 

using namespace std;

class add {
    int a, b;
    void getData();
    int addTwo();

// get the two number from user
void add::getData() {
  cout << "Enter Two Numbers: ";
  cin >> a >> b;

// add two number
int add::addTwo() {
  int sum;
  sum = a + b;
  return sum;

int main() {
  int sum;
  // creating object of class
  add s;
  cout << "sum of two number:" <<sum<<endl;
  return 0;
Enter Two Numbers: 10   20
sum of two number:30

Related programs