How to Sum Two Numbers in Object-Oriented Programming in C++

How to Sum Two Numbers in Object-Oriented Programming in C++

Object-oriented programming is a powerful programming paradigm that provides numerous benefits, including code reusability, encapsulation, and inheritance. One common task in object-oriented programming is the addition of two numbers. In this article, we will discuss how to sum two numbers in object-oriented programming in C++.

Creating a Class for Addition

In C++, classes are used to define objects and their behavior. To create a class for addition, we first need to define the properties and methods that the class will have. In this case, the class will have two properties that represent the two numbers that we want to add. We will also define a method called “add” that will perform the addition operation and return the result.

class Addition {
    private:
        int num1;
        int num2;
    public:
        void setNumbers(int n1, int n2) {
            num1 = n1;
            num2 = n2;
        }
        int add() {
            return num1 + num2;
        }
};

In this code, we define a class called “Addition” with two private integer properties “num1” and “num2”. We also define a public method called “setNumbers” that takes two integer arguments and sets the values of “num1” and “num2” to those arguments. Finally, we define a public method called “add” that returns the sum of “num1” and “num2”.

Creating an Object of the Addition Class

Once we have defined the Addition class, we can create objects of the class and use them to perform addition operations. To create an object of the Addition class, we use the following code:

Addition obj;
obj.setNumbers(3, 4);
int sum = obj.add();

In this code, we create an object of the Addition class called “obj”. We then set the values of “num1” and “num2” to 3 and 4, respectively, using the “setNumbers” method. Finally, we call the “add” method on the object to get the sum of the two numbers.

Using Constructors

Constructors are special methods in a class that are called when an object of the class is created. In the Addition class, we can use a constructor to set the initial values of “num1” and “num2” when an object is created. The following code shows how to define a constructor for the Addition class:

class Addition {
    private:
        int num1;
        int num2;
    public:
        Addition(int n1, int n2) {
            num1 = n1;
            num2 = n2;
        }
        int add() {
            return num1 + num2;
        }
};

In this code, we define a constructor for the Addition class that takes two integer arguments and sets the values of “num1” and “num2” to those arguments. We can then create an object of the Addition class with the following code:

Addition obj(3, 4);
int sum = obj.add();

In this code, we create an object of the Addition class called “obj” and pass the values 3 and 4 to the constructor. The values of “num1” and “num2” are set to 3 and 4, respectively, when the object is created. We can then call the “add” method on the object to get the sum of the two numbers.

Conclusion

In this article, we have discussed how to sum two numbers in object-oriented programming in C++. We created a class called “Addition” that takes two numbers as input, adds them together, and returns the result using a public member function called “sum”. By following these steps, you can easily sum two numbers in C++ using object-oriented programming principles. We hope this article has been helpful in guiding you towards creating quality content that outranks other websites on Google search results.

Example

#include<iostream>
using namespace std;
 class test
 {
     public:
    int name(int a,int b);
 };
int test::name(int a,int b)
{
int c=a+b;
 cout<<c;
}
int main()
{
    int c;
 test obj;
 obj.name(30,50);


}

Object -Oriented Programming ful course

1 thought on “How to Sum Two Numbers in Object-Oriented Programming in C++”

  1. Pingback: C++ class Declaration | Free Code Center

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
NEW TOXIC ROCKET CYCLE DECK in Clash Royale 2023 Best Deck in Clash Royale Best Deck for Arena 8 in Clash Royale Best Hog deck for Pro players Best Clash Royale Deck