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 :: options select from array 
Cpp :: c++ function pointer variable 
Cpp :: remove linked list elements leetcode 
Cpp :: how to sort string array in c++ 
Cpp :: How to generate all the possible subsets of a set ? 
Cpp :: how to take input in 2d vector in c++ 
Cpp :: c++ check first character of string 
Cpp :: C++ Pointers to Structure 
Cpp :: binary to decimal 
Cpp :: Implicit conversion casting 
Cpp :: convert uppercase to lowercase 
Cpp :: bitmap 
Cpp :: double plus overload 
Cpp :: Programming Languages codechef solution in c++ 
Cpp :: c++ include difference between quotes and brackets 
Cpp :: building native binary with il2cpp unity 
Cpp :: how to do if command in c++ 
Cpp :: simple program for sign in and sign up in c++ 
Cpp :: person parametr cpp 
Cpp :: c++ copy vector 
Cpp :: setFontSize QT 
Cpp :: pallindrome string 
Cpp :: argument to number C++ 
Cpp :: ordine crescente di numeri indefiniti in c++ 
Cpp :: cout two dimension array c++ 
Cpp :: && in cpp 
Cpp :: input many numbers to int c++ 
Cpp :: cocos2d c++ linux 
Cpp :: fsafdsfdsaf 
Cpp :: add two constant char pointers c++ 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =