Click here to Skip to main content
12,292,006 members (67,425 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

21.1K views
1 bookmarked
Posted

Operator overloading in C++

, 27 Oct 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Operator overloading is one of the most fascinating features of c++

Introduction

Operator overloading is one of the most fascinating features of c++. By overloading operator we can give additional meaning to operators like +,-,*,<=,>= etc. which by default are supposed to work only on standard data types like int, float etc.

The operators that cannot be overloaded are ., ::, ? and :.

Here we are creating simple class example for operator overloading.

#include<iostream.h>
#include<conio.h>
class xy
{
 private:
  int x,y;
 public:
  // ======constructors=========
  xy()
  { x=y=0; }
  xy(int i,int j)
  {x=i;y=j;}
  xy(xy &z)
  {x=z.x;y=z.y;}
  // ====== unary operators ========
  xy operator-();
  xy operator++();
  xy operator--();
  xy operator++(int);
  xy operator--(int);
  //======= binary operators ========
  xy operator+(xy &);
  xy operator-(xy &);
  xy operator*(xy &);
  xy operator/(xy &);
  xy operator%(xy &);
  xy operator=(xy &);
  xy operator+=(xy &);
  xy operator-=(xy &);
  xy operator*=(xy &);
  xy operator/=(xy &);
  xy operator%=(xy &);
  // =======comparison or logical operators============
  int operator<(xy &);
  int operator>(xy &);
  int operator==(xy &);
  int operator!=(xy &);
  int operator<=(xy &);
  int operator>=(xy &);
  //========= inseration and extraction operators ==========
  friend ostream&operator<<(ostream&, xy&);
  friend istream&operator>>(istream&, xy&);
};
</conio.h></iostream.h>

Function operator-() for return absolute value.

xy xy::operator-()
{
 if(x<0){x*=-1;}
 if(y<0){y*=-1;}
 return *this;
}

Function of unary operators.

xy xy::operator++()
{
 ++x;++y;
 return *this;
}
xy xy::operator--()
{
 --x;--y;
 return *this;
}
xy xy::operator++(int)
{
 x++;y++;
 return *this;
}
xy xy::operator--(int)
{
 x--;y--;
 return *this;
}

Function of binary operators.

xy xy::operator+(xy &z)
{
  xy t;
  t.x=x+z.x;
  t.y=y+z.y;
  return t;
}
xy xy::operator-(xy &z)
{
  xy t;
  t.x=x-z.x;
  t.y=y-z.y;
  return t;
}
xy xy::operator*(xy &z)
{
  xy t;
  t.x=x*z.x;
  t.y=y*z.y;
  return t;
}
xy xy::operator=(xy &z)
{
  x=z.x;y=z.y;
  return *this;
}
xy xy::operator/(xy &z)
{
  xy t;
  t.x=x/z.x;
  t.y=y/z.y;
  return t;
}
xy xy::operator%(xy &z)
{
  xy t;
  t.x=x%z.x;
  t.y=y%z.y;
  return t;
}
xy xy::operator+=(xy &z)
{
  xy t;
  t.x=x+z.x;
  t.y=y+z.y;
  return t;
}
xy xy::operator-=(xy &z)
{
  xy t;
  t.x=x-z.x;
  t.y=y-z.y;
  return t;
}
xy xy::operator*=(xy &z)
{
  xy t;
  t.x=x*z.x;
  t.y=y*z.y;
  return t;
}
xy xy::operator/=(xy &z)
{
  xy t;
  t.x=x/z.x;
  t.y=y/z.y;
  return t;
}
xy xy::operator%=(xy &z)
{
  xy t;
  t.x=x%z.x;
  t.y=y%z.y;
  return t;
}

Function of insertion(>>) & extraction(<<) operators.

ostream&operator<<(ostream &o, xy &z)
{
 o<<endl<<"(x: "<<z.x<<",y: "<<z.y<<")";
 return o;
}
istream&operator>>(istream &i, xy &z)
{
 cout<<endl<<"Enter X & Y: ";
 i>>z.x>>z.y;
 return i;
}

main program to call some overloaded operators.

void main()
{
  clrscr();
  xy a,b(10,20),c;
  cin>>a;
  cout<<a<<b;
  cout<<endl<<"a+b:";
  c=a+b;
  cout<<c;
  cout<<endl<<"a-b:";
  c=a-b;
  cout<<c;
  cout<<endl<<"a*b:";
  c=a*b;
  cout<<c;
  cout<<endl<<"a/b:";
  c=a/b;
  cout<<c;
  cout<<endl<<"a%b:";
  c=a%b;
  cout<<c;
  cout<<endl<<"c+=b:";
  c=c+=b;
  cout<<c;
  getch();
}

The calling function internal layout is c=a.operator+(b); but C++ provided user friendly features operator overloading, so our calling layout is c=a+b like normal default data types operations.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author


You may also be interested in...

Comments and Discussions

 
GeneralP.S. - to the author: It's good to see you've looked into th... Pin
Stefan_Lang6-Nov-11 23:34
memberStefan_Lang6-Nov-11 23:34 
GeneralReason for my vote of 1 - no const correctness - return type... Pin
Stefan_Lang6-Nov-11 23:20
memberStefan_Lang6-Nov-11 23:20 
GeneralReason for my vote of 1 Nonsense! e.g. and = Pin
basilio_ek1-Nov-11 5:58
memberbasilio_ek1-Nov-11 5:58 
GeneralReason for my vote of 1 Very bad article Pin
Leonid Shikhmatov31-Oct-11 11:38
memberLeonid Shikhmatov31-Oct-11 11:38 
GeneralThe is missing the logical bool conversion operator (to use ... Pin
Adorjáni Alpár31-Oct-11 10:47
memberAdorjáni Alpár31-Oct-11 10:47 
GeneralRe: You should never overload && and ||. Why? Because both param... Pin
Quirkafleeg31-Oct-11 22:52
memberQuirkafleeg31-Oct-11 22:52 
GeneralReason for my vote of 1 You don't even follows usually accep... Pin
Philippe Mori28-Oct-11 12:58
memberPhilippe Mori28-Oct-11 12:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160518.1 | Last Updated 28 Oct 2011
Article Copyright 2011 by R S Dodiya
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid