Add Two Numbers Using Class In C++ - C++ Programming Tutorial
C++ Course / Object-Oriented Programming / Add Two Numbers Using Class In C++

Add Two Numbers Using Class In C++

BLUF: Mastering Add Two Numbers Using Class In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Add Two Numbers Using Class In C++

C++ is renowned for its efficiency. Learn how Add Two Numbers Using Class In C++ enables low-level control and high-performance computing in the tutorial below.

The following code demonstrates the addition of two numbers in C++ through the utilization of a class. A class serves as a template or a framework that outlines the attributes and behaviors of an instance. Within this code snippet, the class Addition is employed for the purpose of summing two numbers.

The class Addition contains three private attributes: num1, num2, and sum, which store the two numbers to be added and the result of the addition operation. Access to these private members is restricted to within the class, ensuring they cannot be altered by external code.

C++ Code

Example

#include <iostream>
class Addition {
    private:
        int num1, num2, sum;
    public:
        void input() {
            std::cout << "Enter two numbers: ";
            std::cin >> num1 >> num2;
        }
        void calculate() {
            sum = num1 + num2;
        }
        void output() {
            std::cout << "The sum of " << num1 << " and " << num2 << " is " << sum << std::endl;
        }
};

int main() {
    Addition add;
    add.input();
    add.calculate();
    add.output();
    return 0;
}

Output:

Output

Enter two numbers: 5 8
The sum of 5 and 8 is 13

Explanation:

The class contains three public member methods: input, calculate, and output. The input method is employed to receive user input for the two variables num1 and num2. The calculate method computes the total of num1 and num2, storing it in the sum member variable. Lastly, the output method showcases the sum of num1 and num2 on the screen. Within the main function, a new instance of the Addition class is instantiated using the syntax Addition add.

An instance is a specific occurrence of a class that embodies the attributes and methods specified within the class definition. The sequence of operations on the add object involves invoking the input, calculate, and output methods consecutively to receive user input, compute the total, and present the outcome. Within the input function, the cin object sourced from the iostream library is employed to acquire user input for the variables num1 and num2. Utilizing the cin object alongside the >> operator facilitates the extraction of values from the standard input stream. To prompt the user for two numerical inputs, the cout object from the iostream library and the << operator are utilized.

The calculate method combines num1 and num2 by employing the + operator and saves the outcome in the sum attribute. This method doesn't require any parameters or provide a return value. The display method utilizes the cout object from the iostream library to exhibit the total of num1 and num2 on the screen. Alongside the << operator, the cout object is utilized to input values into the standard output stream. The values of num1, num2, and sum are showcased utilizing the << operator.

Finally, the main function concludes by returning 0 as an indication of the successful completion of the program. To sum up, the aforementioned code presents a basic illustration of adding two numbers using a class in C++. The implementation involves defining a class named Addition with private attributes num1, num2, and sum, along with public methods input, calculate, and output for receiving input, computing the sum, and displaying the outcome. Within the main function, an instance of the class is instantiated, and its methods are invoked to carry out the necessary computations. This program is uncomplicated and serves as an excellent introductory example for individuals looking to grasp the fundamentals of classes in C++.

C++ Code-2

Example

#include <iostream>
using namespace std;
class Addition {
private:
    int num1, num2, sum;
public:
    void input() {
        cout << "Enter two numbers: ";
        cin >> num1 >> num2;
    }

    void calculate() {
        sum = num1 + num2;
    }

    void output() {
        cout << "Sum = " << sum << endl;
    }
};

int main() {
    Addition add;
    add.input();
    add.calculate();
    add.output();
    return 0;
}

Output:

Output

Enter two numbers: 6 7
Sum = 13

Explanation:

In summary, the code provided above serves as a basic illustration of adding two numbers through a class in C++. It presents a class named Addition that encompasses private attributes num1, num2, and sum, along with public methods input, calculate, and output for receiving input, computing the sum, and presenting the outcome. Within the main function, an instance of this class is instantiated, and its methods are invoked to carry out the necessary computations.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience