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 

#include<iostream>
using namespace std;

class add {
  private:
    int a, b;
  public:
    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;
  s.getData();
  sum=s.addTwo();
  cout << "sum of two number:" <<sum<<endl;
  return 0;
}
output
Enter Two Numbers: 10   20
sum of two number:30

Related programs