Search
 
SCRIPT & CODE EXAMPLE
 

CPP

c++ operator overloading

#include <iostream>

class foo{
public: 
  foo(){} //Empty constructor
  
  foo(int a, int b){ //Constructor takes 2 args to set the value of a and b.
    this->a = a;
    this->b = b;
  }
  
  int a;
  int b;
  
  /*
  A function that returns a foo class when a "+" operator is applied to
  2 objects of type foo.
  */
  foo operator+(foo secondValue){
    foo returnValue; //Temporary class to return.
    returnValue.a = this->a + secondValue.a;
    returnValue.b = this->b + secondValue.b;
    return returnValue;
};
  
int main(){
  foo firstValue(1,3);
  foo secondValue(1,5);
  foo sum;
  
  sum = firstValue + secondValue;
  std::cout << sum.a << " " << sum.b << "
";
  
  return 0;
}
Comment

class operator overloading c++

#include<iostream>
using namespace std;
 
class Complex {
private:
    int real, imag;
public:
    Complex(int r = 0, int i = 0) {real = r;   imag = i;}
     
    // This is automatically called when '+' is used with
    // between two Complex objects
    Complex operator + (Complex const &obj) {
         Complex res;
         res.real = real + obj.real;
         res.imag = imag + obj.imag;
         return res;
    }
    void print() { cout << real << " + i" << imag << '
'; }
};
 
int main()
{
    Complex c1(10, 5), c2(2, 4);
    Complex c3 = c1 + c2;
    c3.print();
}
Comment

Syntax for C++ Operator Overloading

class className {
    ... .. ...
    public
       returnType operator symbol (arguments) {
           ... .. ...
       } 
    ... .. ...
};
Comment

operator overloading in c++

// operator overloading in c++ **sudipnext**
#include <iostream>
using namespace std;
class complex
{
    int real, img;

public:
    complex()
    {
        real = 0;
        img = 0;
    }
    void getData()
    {
        cout << "Enter the real part :- " << endl;
        cin >> real;
        cout << "Enter the img part:- " << endl;
        cin >> img;
    }
    complex operator+(complex &x)
    {
        complex temp;
        temp.real = x.real + real;
        temp.img = x.img + img;
        return temp;
    }
    void display()
    {
        cout << "The ans are :- " << real << "   " << img << endl;
    }
};
int main()
{
    complex obj1, obj2, obj3;
    obj1.getData();
    obj2.getData();
    obj3 = obj1 + obj2;
    obj3.display();
    return 0;
}
Comment

C++ Calling overloaded operators

Example 5-17. Calling overloaded operators
class demo
{
public:
 demo(int v) : value_(v) {}
 demo add(const demo& d) const;
 demo sub(const demo& d) const;
 demo mul(const demo& d) const;
 demo operator+(const demo& d) const;
 demo operator-(const demo& d) const;
 demo operator*(const demo& d) const;
 operator int( ) const { return value_; }
private:
 int value_;
};

// Silly examples, but illustrative
demo add(const demo& a) { return a; }
demo mul(const demo& a) { return a; }
demo div(const demo& a) { return a; }

demo operator+(const demo& a, const demo& b)
{
return a.operator+(b); // Force use of member function.
}

demo demo::add(const demo& d)
const
{
return *this + d; // Error: calls ::operator+( ) or demo::operator+( )?
}

demo demo::sub(const demo& d) const
{
return this->operator-(d); // Member operator
}

demo demo::mul(const demo& d) const
{
return ::operator*(*this, d); // Global operator
}

demo demo::operator+(const demo& d) const
{
 return demo(int(*this) + int(d));
}

demo demo::operator-(const demo& d) const
{
 return sub(d); // Calls demo::sub (recurses infinitely)
}

demo demo::operator*(const demo& d) const
{
 return ::mul(d); // Scopes operator to call global mul( )
}
Comment

overload the >> operator in c++

istream &operator>>( istream  &input, Class_Name &c )
Comment

operator overloading c++

// Use operator overloading to change the behaviour of division to multiplication for user defined data types

#include <bits/stdc++.h>
using namespace std;

class parent
{
    int x;

public:
    parent(int y)
    {
        this->x = y;
    }

    void operator/(parent p)
    {
        int temp = this->x * p.x;
        cout << temp;
    }
};

int main()
{
    parent obj1(10), obj2(20);
    obj2 / obj1;

    return 0;
}
Comment

operator overloading in C++

//adding two fraction classes together
//Fraction class has a numerator, denominator field with accessors
//A new fraction class is returned with the new numerator and denominator
Fraction operator+(const Fraction& left, const Fraction& right){
    int newDenom = left.denominator() * right.denominator();
    int newNumerator = (left.denominator() * right.numerator()) + (right.denominator() * left.numerator());
    Fraction newFraction(newNumerator, newDenom);
    return newFraction;
};
Comment

operator = overloading c++

inline bool operator==(const X& lhs, const X& rhs){ /* do actual comparison */ }
inline bool operator!=(const X& lhs, const X& rhs){ return !(lhs == rhs); }
Comment

PREVIOUS NEXT
Code Example
Cpp :: onoverlapbegin ue4 c++ 
Cpp :: loop execution decending order in c 
Cpp :: Lambda capture as const cpp 
Cpp :: c++ class 
Cpp :: c++ get active thread count 
Cpp :: cmake g++ address sanitizer 
Cpp :: if argv == string 
Cpp :: Initialize Vector Iterator Through Vector Using Iterators 
Cpp :: what was the piep piper app 
Cpp :: string concatenation operator overloading c++ 
Cpp :: Euler constant 
Cpp :: recursive factorial of a number 
Cpp :: how to get last element of set 
Cpp :: iomanip header file in c++ 
Cpp :: c++ include < vs "" 
Cpp :: how to run cpp using gcc vscode 
Cpp :: or in c++ 
Cpp :: c++ program to find gcd of 3 numbers 
Cpp :: cin c++ 
Cpp :: phi function 
Cpp :: cpp language explained 
Cpp :: c++ char 
Cpp :: sstream c++ 
Cpp :: c++ method name 
Cpp :: building native binary with il2cpp unity 
Cpp :: CPP print executable name 
Cpp :: ejemplo 
Cpp :: The Rating Dilemma codechef solution in c++ 
Cpp :: selection sort algorithm in cpp 
Cpp :: vowel and consonant program in c++ using if else 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =