Thursday, April 25, 2013

Operator overloading – Complex Number C++



Addition, Subtraction, Multiplication and Division of Complex Number using Operator Overloading



Example :
 #include<iostream.h>

  class ComplexMath
      {
           private:
              double real;
              double im;
           public:
              void getData();
              ComplexMath operator +(ComplexMath);
              ComplexMath operator -(ComplexMath);
              ComplexMath operator /(ComplexMath);
              ComplexMath operator *(ComplexMath);
              void Disp();
              ComplexMath()
                   {
                        real=0;
                        im=0;
                   }
              ~ComplexMath(){}
  };
  void ComplexMath::getData()
      {
           ComplexMath z,y,x;
           cout<<"\n\tEnter Real : ";
           cin>>real;
           cout<<"\n\tEnter Imaginary : ";
           cin>>im;
      }
  ComplexMath ComplexMath::operator +(ComplexMath C2)
      {
           ComplexMath C3;
           C3.real=real+C2.real;
           C3.im=im+C2.im;
           return C3;
      }
  ComplexMath ComplexMath::operator -(ComplexMath C2)
      {
           ComplexMath C3;
           C3.real=real-C2.real;
           C3.im=im-C2.im;
           return C3;
      }
  ComplexMath ComplexMath::operator /(ComplexMath C2)
      {
           float num = (C2.real*C2.real)+(C2.im*C2.im);
           ComplexMath C3;
           C3.real=(real*C2.real)+(im*C2.im); //Real numerator
           C3.real=C3.real/num;
           C3.im=(im*C2.real)-(real*C2.im);
           C3.im=C3.im/num;
           return C3;
      }
  ComplexMath ComplexMath::operator *(ComplexMath C2)
      {
           ComplexMath C3;
           C3.real=real*C2.real+im*(-C2.im)*(-1); //Real number of the numerator
           C3.im=real*(-C2.im)+im*C2.real; //Imaginary number of the numerator
           return C3;
      }

  void ComplexMath::Disp()
      {
           cout<<real<<","<<im<<"i";
      }
  int main()
      {
           ComplexMath C1,C2,C3;
           int ch=0;
           cout<<"\n\tEnter First Complex No. ";
           C1.getData();
           cout<<"\n\tEnter Second Complex No. ";
           C2.getData();
           while(ch!=5)
              {
                   cout<<"\n\t1. Addition";
                   cout<<"\n\t2. Subtraction";
                   cout<<"\n\t3. Multiplication";
                   cout<<"\n\t4. Division";
                   cout<<"\n\t5. Exit";
                   cout<<"\n\n\t\tChoice? ";
                   cin>>ch;
                   switch (ch)
                        {
                            case 1:
                                C3=C1+C2;
                                C1.Disp();
                                cout<<" + ";
                                C2.Disp();
                                cout<<" = ";
                                C3.Disp();
                                break;
                            case 2:
                                C3=C1-C2;
                                C1.Disp();
                                cout<<" - ";
                                C2.Disp();
                                cout<<" = ";
                                C3.Disp();
                                break;
                            case 3:
                                C3=C1*C2;
                                C1.Disp();
                                cout<<" * ";
                                C2.Disp();
                                cout<<" = ";
                                C3.Disp();
                                break;
                            case 4:
                                C3=C1/C2;
                                C1.Disp();
                                cout<<" / ";
                                C2.Disp();
                                cout<<" = ";
                                C3.Disp();
                                break;
                            case 5:
                                break;
                            default:
                                cout<<"\n\n\t\tWrong Input\n";

                        }
              }
           return 0;
      }

Thursday, April 18, 2013

Operator overloading – Extraction & Insertion Operator C++


Stream extraction (>>) and insertion (<<) operators can be overloaded for read from keyboard and display the output. input and output to accept the user defined data.  These operator also used for writing and reading from data files.


Example :

#include <iostream.h>

//Example of Operator  <<  >>

  class Cal
      {
           private:
              int a,b,c;
           public:
              Cal()
                   {
                        a = b = 0;
                   }
              Cal(int x, int y)
                   {
                        a = x;
                        b = y;
                   }
              friend ostream &operator<<(ostream &Write, Cal &C )
                   {
                        C.c=C.a+C.b;
                        Write<<"a = "<<C.a<<" & b =" <<C.b<<"  Sum = "<<C.c;
                        return Write;
                   }
              friend istream &operator>>(istream  &Read, Cal &C )
                   {
                        Read>>C.a>>C.b;
                        return Read;
                   }
      };
  int main()
      {
           Cal C1(10,20), C2;
           cout <<"\n\tEnter Two Nos. : ";
           cin>>C2;
           cout<<"\n\tSum of : "<<C1 << endl;
           cout <<"\n\tSum of :" <<C2 << endl;
           return 0;
      }



#include <iostream.h>
#include <fstream.h>

//Example of File using Operator  <<  >>

  class FileOp
      {
           private:
              int roll,marks;
           public:
              FileOp()
                   {}
              friend ostream &operator<<(ostream &Write,  FileOp &FP )
                   {
                        Write<<" Roll : "<<FP.roll<<" Marks : "<<FP.marks;
                        return Write;
                   }

              friend istream &operator>>(istream  &Read, FileOp &FP )
                   {
                        Read>>FP.roll>>FP.marks;
                        return Read;
                   }
      };
  int main()
      {
           fstream f;
           f.open("aaa.txt",ios::out);
           FileOp FP1,FP2;
           cout<<"\n\tEnter Roll & Marks : " << endl;
           cin>>FP1;
           f.write((char *)&FP1,sizeof(FP1));
           f.close();
           f.open("aaa.txt",ios::in);
           f.read((char *)&FP2,sizeof(FP2));
           cout<<"Data :" << FP2 << endl;
           return 0;
      }

Thursday, April 11, 2013

Operator overloading – Bitwise Operator C++




The bitwise operators perform bit by bit -AND (&), exclusive-OR (^), OR (|), RIGHT-SHIFT (>>) and LEFT-SHIFT (<<)  operations.


Example :

#include <iostream.h>

//Example of Operator ^ & | <<  >>

  class Bit_Op
      {
           private:
              int n;
           public:
              Bit_Op()
                   {
                        n=0;
                   }
              void input();
              void disp();
              void Deci_Bin();
              Bit_Op operator &(Bit_Op);
              Bit_Op operator ^(Bit_Op);
              Bit_Op operator |(Bit_Op);
              Bit_Op operator <<(Bit_Op);
              Bit_Op operator >>(Bit_Op);
      };
  void Bit_Op::Deci_Bin()
      {
           int n1,i=0,j,a[15];
           n1=n;
           while(n1>0)
              {
                   a[i++]=n1%2;
                   n1=n1/2;
              }
           for(j=i-1;j>=0;j--)
              {
                   cout<<a[j];
              }
      }
  void Bit_Op::input()
      {
           cout<<"\n\tEnter  no ";
           cin>>n;
      }
  void Bit_Op::disp()
      {
           cout<<" "<<n;
      }
  Bit_Op Bit_Op::operator ^(Bit_Op O)
      {
           Bit_Op OP;
           OP.n=n^O.n;
           return OP;
      }
  Bit_Op Bit_Op::operator &(Bit_Op O)
      {
           Bit_Op OP;
           OP.n=n&O.n;
           return OP;
      }
  Bit_Op Bit_Op::operator |(Bit_Op O)
      {
           Bit_Op OP;
           OP.n=n|O.n;
           return OP;
      }
  Bit_Op Bit_Op::operator <<(Bit_Op O)
      {
           Bit_Op OP;
           OP.n=n<<O.n;
           return OP;
      }
  Bit_Op Bit_Op::operator >>(Bit_Op O)
      {
           Bit_Op OP;
           OP.n=n>>O.n;
           return OP;
      }

  int main()
      {
           Bit_Op O1,O2,O3,O4;
           O1.input();
           O2.input();
           cout<<"\n\tNos. ";
           O1.disp();
           cout<<" & ";
           O2.disp();
           cout<<"  Result ^ ";
           O3=O1^O2;
           O3.disp();
           cout<<"\n\tBinary  ";
           O1.Deci_Bin();
           cout<<" & ";
           O2.Deci_Bin();
           cout<<"  Result ^ ";
           O3.Deci_Bin();
           cout<<"\n\n\tNos. ";
           O1.disp();
           cout<<" & ";
           O2.disp();
           cout<<"  Result & ";
           O3=O1&O2;
           O3.disp();
           cout<<"\n\tBinary  ";
           O1.Deci_Bin();
           cout<<" & ";
           O2.Deci_Bin();
           cout<<"  Result & ";
           O3.Deci_Bin();
           cout<<"\n\n\tNos. ";
           O1.disp();
           cout<<" | ";
           O2.disp();
           cout<<"  Result | ";
           O3=O1|O2;
           O3.disp();
           cout<<"\n\tBinary  ";
           O1.Deci_Bin();
           cout<<" | ";
           O2.Deci_Bin();
           cout<<"  Result & ";
           O3.Deci_Bin();
           cout<<"\n\n\tNos. ";
           O1.disp();
           cout<<" << ";
           O2.disp();
           cout<<"  Result << ";
           O3=O1<<O2;
           O3.disp();
           cout<<"\n\tBinary  ";
           O1.Deci_Bin();
           cout<<" << ";
           O2.Deci_Bin();
           cout<<"  Result << ";
           O3.Deci_Bin();
           cout<<"\n\n\tNos. ";
           O1.disp();
           cout<<" >> ";
           O2.disp();
           cout<<"  Result >> ";
           O3=O1>>O2;
           O3.disp();
           cout<<"\n\tBinary  ";
           O1.Deci_Bin();
           cout<<" >> ";
           O2.Deci_Bin();
           cout<<"  Result >> ";
           O3.Deci_Bin();
           return 0;
      }