Javatpoint Logo
Javatpoint Logo

Add two Numbers using Class in C++

The below code is an implementation of adding two numbers in C++ using a class. A class is a blueprint or a structure that defines the data members and member functions of an object. In this code, the class Addition is used to add two numbers.

The class Addition has three private members num1, num2, and sum that represent the two numbers to be added and the sum of the two numbers respectively. These private members can be accessed only within the class and are protected from being modified by code outside the class.

C++ Code

Output:

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

Explanation:

The class also has three public member functions: input(), calculate(), and output(). The input() function is used to take input from the user for the two numbers num1 and num2. The calculate() function is used to calculate the sum of num1 and num2 and store it in the sum member. The output() function is used to display the sum of num1 and num2 on the screen. In the main() function, an object of the class Addition is created using the syntax Addition add.

An object is an instance of a class and represents the data members and member functions defined in the class. The member functions of the object add are called in the order input(), calculate(), and output() to take input, calculate the sum, and display the result. The input() function uses the cin object from the iostream library to take input from the user for the two numbers num1 and num2. The cin object is used in conjunction with the >> operator to extract values from the standard input stream. The user is prompted to enter two numbers using the cout object from the iostream library and the << operator.

The calculate() function adds num1 and num2 using the + operator and stores the result in the sum member. This function does not take any arguments or return any value. The output() function uses the cout object from the iostream library to display the sum of num1 and num2 on the screen. The cout object is used in conjunction with the << operator to insert values into the standard output stream. The values of num1, num2, and sum are displayed using the << operator.

Finally, the main() function returns 0 to indicate successful execution of the program. In conclusion, the above code is a simple implementation of adding two numbers using a class in C++. The code defines a class Addition with private members num1, num2, and sum and public member functions input(), calculate(), and output() to take input, calculate the sum, and display the result. An object of the class is created in the main() function and its member functions are called to perform the required operations. The program is straightforward and easy to understand, making it a good starting point for learning about classes in C++.

C++ Code-2

Output:

Enter two numbers: 6 7
Sum = 13

Explanation:

In conclusion, the above code is a simple example of adding two numbers using a class in C++. The code defines a class Addition with private members num1, num2, and sum and public member functions input(), calculate(), and output() to take input, calculate the sum, and display the result. An object of the class is created in the main() function and its member functions are called to perform the required operations.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA