Search
 
SCRIPT & CODE EXAMPLE
 

CPP

how to add 2 objects using operator overloading in c++

/*C++ program to add two objects using binary plus (+) operator overloading.*/
 
#include<iostream>
using namespace std;
 
class NUM
{
    private:
        int n;
         
    public:
        //function to get number
        void getNum(int x)
        {
            n=x;
        }
        //function to display number
        void dispNum(void)
        {
            cout << "Number is: " << n;
        }
        //add two objects - Binary Plus(+) Operator Overloading
        NUM operator +(NUM &obj)
        {
            NUM x;  //create another object
            x.n=this->n + obj.n;
            return (x); //return object
        }
};
int main()
{
    NUM num1,num2,sum;
    num1.getNum(10);
    num2.getNum(20);
     
    //add two objects
    sum=num1+num2;
     
    sum.dispNum();
    cout << endl;
    return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: algorithm map values 
Cpp :: rand() and srand() in C/C++ 
Cpp :: fabs in c++ example 
Cpp :: can you use rand to read in from an external file inc++ 
Cpp :: ue_log example 
Cpp :: C++ Display Numbers from 1 to 5 
Cpp :: ue4 array copy c++ 
Cpp :: composition namespaces c++ 
Cpp :: convert char to C 
Cpp :: c++ void to avoid functions 
Cpp :: distructor of the node of the link list 
Cpp :: initialize multiple variables to 0 c++ 
Cpp :: C++ Vector Initialization method 02 
Cpp :: floating point exception 
Cpp :: c++ calorie calculator using a for loop 
Cpp :: unity decompile il2cpp 
Cpp :: Codeforces Round #376 (Div. 2), problem: (A) Night at the Museum 
Cpp :: is there interfaces in c++ like 
Cpp :: _ZNSolsEi 
Cpp :: c++ arreglo/array 
Cpp :: In every C++ program: 
Cpp :: scope resolution operator in c++ 
Cpp :: cin in c++ 
Cpp :: how to include a library in arduino 
Cpp :: Arduino Counting without Millis 
Cpp :: C++ mutex header 
C :: stop redis server 
C :: print an array in c 
C :: convert string to float c 
C :: prime check in c 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =