Overloading
C++ Operators Available for Overloading
+ | - | * | / | % | ^ | & | | | ~ | = |
< | > | += | -= | *= | /= | %= | ^= | &= | |= |
<< | >> | >>= | <<= | == | != | <= | >= | && | || |
++ | -- | -<* | , | -> | [] | () | new | delete | ! |
Operator +
- #ifndef V_H
- #define V_H
- #include <iostream>
- #include <string>
-
- class V
- {
- private:
- int size;
- int *array;
- public:
- //constructor
- V(int s):size(s)
- {
- std::cout<<"Constructor ..."<<std::endl;
- array = new int[size];
- for(int i = 0; i < size; i++)
- array[i] = 10*i;
- std::cout<<"----End Constructor ..."<<std::endl;
- }
-
- //copy constructor
- V(const V &right):size(right.size), array(size?new int [size]:nullptr)
- {
- std::cout<<"Copy Constructor ..."<<std::endl;
- std::copy(right.array, right.array+size, array);
- std::cout<<"----End Copy Constructor ..."<<std::endl;
- }
-
- //move constructor
- V(V && right):size(0), array(nullptr)
- {
- std::cout<<"Move Constructor ..."<<std::endl;
- size = right.size;
- array = right.array;
- right.size = 0;
- right.array = nullptr;
- std::cout<<"----End Move Constructor ..."<<std::endl;
- }
-
- int *getAddress() const {return array;}
- int getSize() const {return size;}
-
- void display() const
- {
- for(int i = 0; i < size; i++)
- std::cout<<array[i]<<" ";
- std::cout<<std::endl;
- }
-
- std::string toString() const
- {
- std::string str = "V: ";
- for(int i = 0; i < size; i++)
- str += std::to_string(array[i])+" ";
- return str;
- }
-
- V & time(int n)
- {
- for(int i = 0; i < size; i++)
- array[i] *= n;
- return *this;
- }
-
- //copy assignment
- const V& operator=(const V &right)
- {
- std::cout<<"Copy Assignment ..."<<size<<std::endl;
- if(this != &right)
- {
- V temp(right);
- //std::swap(size, temp.size);
- //std::swap(array, temp.array);
- std::swap(*this, temp);
- }
- std::cout<<"----End Assignment ..."<<std::endl;
-
- return *this;
- }
-
- //move assignment
- V & operator=(V&& right)
- {
- std::cout<<"Move Assignment ..."<<std::endl;
- if(this != &right)
- {
- delete [] array;
- array = 0;
- size = 0;
-
- size = right.size;
- array = right.array;
-
- right.size = 0;
- right.array = nullptr;
- }
- std::cout<<"----End Move Assignment ..."<<std::endl;
-
- return *this;
- }
-
- //Overloading
- V operator+(const V &right)
- {
- std::cout<<"Operator + ..."<<std::endl;
- V temp(size+right.size);
- std::copy(array, array+size, temp.array);
- std::copy(right.array, right.array+right.size, temp.array+size);
- std::cout<<"End Operator + ..."<<std::endl;
-
- return temp;
- }
-
- ~V()
- {
- std::cout<<"Destructor ..."<<size<<std::endl;
- delete array;
- array = 0;
- size = 0;
- }
- };
- #endif
-
- #include <iostream>
- #include "V.h"
-
- int main(int argc, char *argv[])
- {
- V v(3);
- V v2(4);
-
- V v3 = v + v2;
-
- v.display();
- v2.display();
- v3.display();
-
- return 0;
- }
-
- v3 = v + v2;
- v3 = v.operator+(v2)
Prefix ++
- //Prefix ++ Operator
- V operator++()
- {
- std::cout<<"Prefix ++ ..."<<std::endl;
- for(int i = 0; i < size; i++)
- array[i] += 1;
- std::cout<<"End Prefix ++ ..."<<std::endl;
- return *this;
- }
-
Postfix ++
- //Postfix ++ Operator
- V operator++(int)
- {
- std::cout<<"Postfix ++ ..."<<std::endl;
- V temp(*this);
- for(int i = 0; i < size; i++)
- array[i] += 1;
- std::cout<<"End Postfix ++ ..."<<std::endl;
- return temp;
- }
-
- #include <iostream>
- #include "V.h"
-
- int main(int argc, char *argv[])
- {
- V v(10);
-
- V v2 = ++v;
-
- std::cout<<v.toString()<<std::endl;
- std::cout<<v2.toString()<<std::endl;
-
- V v3 = v++;
- std::cout<<v.toString()<<std::endl;
- std::cout<<v3.toString()<<std::endl;
-
- return 0;
- }
-
- int, dummy parameter, is designed to be used in postfix mode
- temp, a copy of the object before making change
Operator <
- bool operator<(const V &right)
- {
- if(size < right.size)
- return true;
- else
- return false;
- }
-
- #include <iostream>
- #include <algorithm>
- #include <vector>
- #include "V.h"
-
- int main(int argc, char *argv[])
- {
- std::vector<V> container;
- V v(10), v2(2), v3(4), v4(20);
- container.push_back(v);
- container.push_back(v2);
- container.push_back(v3);
- container.push_back(v4);
-
- std::sort(container.begin(), container.begin()+4);
-
- for(int i = 0; i < container.size(); i++)
- std::cout<<container[i].toString()<<std::endl;
-
- return 0;
- }
-
operator < is used in std::sort in algorithm library
Operator <<
- #ifndef UTIL_H
- #define UTIL_H
- #include <iostream>
-
- class V;
-
- std::ostream &operator<<(std::ostream &strm, const V &right)
- {
- strm<<"V: ";
- for(int i = 0; i < right.size; i++)
- strm<<right.array[i]<<" ";
- return strm;
- }
-
- std::istream &operator>>(std::istream &strm, V &right)
- {
- delete [] right.array;
- right.array = nullptr;
- right.size = 0;
-
- std::cout<<"Enter the size: "<<std::endl;
- strm>>right.size;
- right.array = new int[right.size];
- for(int i = 0; i < right.size; i++)
- right.array[i] = i*10;
-
- return strm;
- }
- #endif
-
- #include <iostream>
- #include "V.h"
- #include "util.h"
-
- int main(int argc, char *argv[])
- {
- V v(10);
-
- std::cout<<v<<std::endl;
-
- V v2(0);
- std::cin>>v2;
- std::cout<<v2<<std::endl;
-
-
- return 0;
- }
-
<< and >> are part of ostream and istream classes in the C++ runtime library, overloading overload the ostream version of << and the istream version of >>
Operator []
- int &operator[](const int &index)
- {
- if(index < 0 || index >= size)
- {
- std::cerr<<"Out of range ..."<<std::endl;
- std::exit(1);
- }
- return array[index];
- }
-
- #include <iostream>
- #include "V.h"
- #include "util.h"
-
- int main(int argc, char *argv[])
- {
- V v(10);
-
- int a = v[2];
- std::cout<<a<<std::endl;
-
- v[2] = 100;
- std::cout<<v<<std::endl;
-
-
- return 0;
- }
-
Object Conversion
- //Convert object to integer
- operator int()
- {
- return size;
- }
-
- #include <iostream>
- #include "V.h"
-
- int main(int argc, char *argv[])
- {
- V v(10);
-
- int size = v;
-
- std::cout<<"Size: "<<size<<std::endl;
-
- return 0;
- }
-