Showing posts with label Operator Overloading. Show all posts
Showing posts with label Operator Overloading. Show all posts

Fibonacci Series Using Op. Overloading


//A program to generatea fibonacci series by overloading a postfix operator//
//generation of the series of fibonacci number
//using operator overloading of postfix incrementer

#include  <iostream.h>
#include<conio.h>
class fibonnaci
{
   private :
      unsigned long int f0,f1,fib;
   public :
      fibonnaci();
//    fibonnaci operator(); //constructor
      fibonnaci operator++(int );
      void display();
};
fibonnaci :: fibonnaci ()
{
   f0=0;
   f1=1;
   fib=f0+f1;
}
void fibonnaci:: display()
{
   cout << fib << '\t';
}
fibonnaci fibonnaci :: operator++ (int x)
{
   f0=f1;
   f1=fib;
   fib=f0+f1;
   return *this;
}

void main()
{
   clrscr();
   fibonnaci obj;
   int n;
   cout << " how many fibonnaci numbers are to be";
   cout << "displayed? \n";
   cin >> n;
   for (int i = 0; i <= n-1; ++i)
{
      obj.display();
      obj++;
}
getch();
}

Comma Operator Overload in C++


#include <iostream.h>
#include <conio.h>
class loc
{
      int longitude, latitude;
      public:
      loc() {}
      loc(int lg, int lt)
      {
            longitude = lg;
            latitude = lt;
      }
      void show()
      {
            cout << longitude << " ";
            cout << latitude << "\n";
      }
      loc operator+(loc op2);
      loc operator,(loc op2);
};
// overload comma for loc
loc loc::operator,(loc op2)
{
      loc temp;
      temp.longitude = op2.longitude;
      temp.latitude = op2.latitude;
      cout << op2.longitude << " " << op2.latitude << "\n";
      return temp;
}
// Overload + for loc
loc loc::operator+(loc op2)
{
      loc temp;
      temp.longitude = op2.longitude + longitude;
      temp.latitude = op2.latitude + latitude;
      return temp;
}
int main()
{
      clrscr();
      loc ob1(10, 20), ob2( 5, 30), ob3(1, 1);
      ob1.show();
      ob2.show();
      ob3.show();
      cout << "\n";
      ob1=(ob1,ob2+ob2);
      ob1.show();
      ob1 = (ob1, ob2+ob2, ob3);
      ob1.show(); // displays 1 1, the value of ob3
      getch();
      return 0;
}

Operator Overload (Arrow)


#include <iostream.h>
#include <conio.h>

class myclass
{
      public:
      int i;
      myclass *operator->()
      {
            return this;
      }
};

int main()
{
      clrscr();
      myclass ob;
      ob->i = 10; // same as ob.i
      cout << ob.i << " " << ob->i;
      getch();
      return 0;
}

Operator Overloading Tutorial for Longitude and Latitude in C++


#include <iostream.h>
#include <conio.h>
class loc
{
      int longitude, latitude;
      public:
      loc() {}
      loc(int lg, int lt) {
      longitude = lg;
      latitude = lt;
}
void show()
{
      cout << longitude << " ";
      cout << latitude << "\n";
}
loc operator+(loc op2);
loc operator()(int i, int j);
};
// Overload ( ) for loc.
loc loc::operator()(int i, int j)
{
      longitude = i;
      latitude = j;
      return *this;
}
// Overload + for loc.
loc loc::operator+(loc op2)
{
      loc temp;
      temp.longitude = op2.longitude + longitude;
      temp.latitude = op2.latitude + latitude;
      return temp;
}
int main()
{
      clrscr();
      loc ob1(10, 20), ob2(1, 1);
      ob1.show();
      ob1(7, 8); // can be executed by itself
      ob1.show();
      ob1 = ob2 + ob1(10, 10); // can be used in expressions
      ob1.show();
      getch();
      return 0;
}
Custom Search