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 :: operand-- c++ 
Cpp :: sort c++ 
Cpp :: c++ replace 
Cpp :: sum of a matrix c++ 
Cpp :: c++ compile to exe 
Cpp :: input n space separated integers in c++ 
Cpp :: quicksort geeksforgeeks 
Cpp :: show stack c++ 
Cpp :: how to say hello world in c++ 
Cpp :: c ++ splitlines 
Cpp :: SUMOFPROD2 solution 
Cpp :: best time to buy and sell stock leetcode solution 
Cpp :: how to use command line arguments with integers in c++ 
Cpp :: structure of a function in C++ 
Cpp :: c++ changing string to double 
Cpp :: search by value in map in stl/cpp 
Cpp :: prime number c++ 
Cpp :: comparator priority queue c++ 
Cpp :: c++ Attribute Parser 
Cpp :: c++ namespace example 
Cpp :: Lambda capture as const cpp 
Cpp :: power in c++ 
Cpp :: creating node in c++ 
Cpp :: c++ cin 
Cpp :: flag of georgia 
Cpp :: greatest and smallest in 3 numbers cpp 
Cpp :: remove element from c++ 
Cpp :: vector iterator in c++ 
Cpp :: valid parentheses in cpp 
Cpp :: find second largest number in array c++ 
ADD CONTENT
Topic
Content
Source link
Name
6+3 =