Code Snippets

  

C++ Source Code


Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 99,783 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,537 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!




Complex Numbers Class

This class Implements Complex Numbers and it's Functions.

Submitted By: born2c0de
Actions:
Rating:
Views: 21,146

Language: C++

Last Modified: April 13, 2008

Snippet


  1. #include <cmath>
  2. #include <iostream>
  3. #include <iomanip.h>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. class complex
  9. {
  10.      private:
  11.                   float real;               // Real Part
  12.             float imag;      //  Imaginary Part
  13.  
  14.  
  15.    public:
  16.           complex(float,float);
  17.           complex(complex&);
  18.           complex operator +(complex);
  19.           complex operator -(complex);
  20.           complex operator *(complex);
  21.           complex operator /(complex);
  22.           complex getconjugate();
  23.           complex getreciprocal();
  24.           float getmodulus();
  25.           void setdata(float,float);
  26.           void getdata();
  27.           float getreal();
  28.           float getimaginary();
  29.           bool operator ==(complex);
  30.           void operator =(complex);
  31.           friend ostream& operator <<(ostream &s,complex &c);
  32. };
  33. //                                        CONSTRUCTOR
  34.                   complex::complex(float r=0.0f,float im=0.0f)
  35.             {
  36.                  real=r;
  37.                imag=im;
  38.             }
  39.  
  40. //                                 COPY CONSTRUCTOR
  41.             complex::complex(complex &c)
  42.             {
  43.                  this->real=c.real;
  44.                this->imag=c.imag;
  45.             }
  46.  
  47.  
  48.             void complex::operator =(complex c)
  49.             {
  50.                real=c.real;
  51.                imag=c.imag;
  52.             }
  53.  
  54.  
  55.             complex complex::operator +(complex c)
  56.             {
  57.                  complex tmp;
  58.                tmp.real=this->real+c.real;
  59.                tmp.imag=this->imag+c.imag;
  60.                return tmp;
  61.             }
  62.  
  63.             complex complex::operator -(complex c)
  64.             {
  65.                  complex tmp;
  66.                tmp.real=this->real - c.real;
  67.                tmp.imag=this->imag - c.imag;
  68.                return tmp;
  69.             }
  70.  
  71.                     complex complex::operator *(complex c)
  72.             {
  73.                  complex tmp;
  74.                tmp.real=(real*c.real)-(imag*c.imag);
  75.                tmp.imag=(real*c.imag)+(imag*c.real);
  76.                return tmp;
  77.             }
  78.  
  79.             complex complex::operator /(complex c)
  80.             {
  81.                  float div=(c.real*c.real) + (c.imag*c.imag);
  82.                complex tmp;
  83.                tmp.real=(real*c.real)+(imag*c.imag);
  84.                tmp.real/=div;
  85.                tmp.imag=(imag*c.real)-(real*c.imag);
  86.                tmp.imag/=div;
  87.                return tmp;
  88.             }
  89.  
  90.             complex complex::getconjugate()
  91.             {
  92.                  complex tmp;
  93.                tmp.real=this->real;
  94.                tmp.imag=this->imag * -1;
  95.                return tmp;
  96.             }
  97.  
  98.             complex complex::getreciprocal()
  99.             {
  100.                  complex t;
  101.                t.real=real;
  102.                t.imag=imag * -1;
  103.                float div;
  104.                div=(real*real)+(imag*imag);
  105.                t.real/=div;
  106.                t.imag/=div;
  107.                return t;
  108.             }
  109.  
  110.             float complex::getmodulus()
  111.             {
  112.                  float z;
  113.                z=(real*real)+(imag*imag);
  114.                z=sqrt(z);
  115.                return z;
  116.             }
  117.  
  118.             void complex::setdata(float r,float i)
  119.             {
  120.                  real=r;
  121.                imag=i;
  122.             }
  123.  
  124.             void complex::getdata()
  125.             {
  126.                  cout<<"Enter Real:";
  127.                cin>>this->real;
  128.                cout<<"Enter Imaginary:";
  129.                cin>>this->imag;
  130.  
  131.             }
  132.  
  133.             float complex::getreal()
  134.             {
  135.                  return real;
  136.             }
  137.  
  138.             float complex::getimaginary()
  139.             {
  140.                  return imag;
  141.             }
  142.  
  143.             bool complex::operator ==(complex c)
  144.             {
  145.              return (real==c.real)&&(imag==c.imag) ? 1 : 0;
  146.              }
  147.  
  148.               ostream& operator <<(ostream &s,complex &c)
  149.             {
  150.                  s<<"Real Part = "<<c.real<<endl
  151.                 <<"Imaginary Part = "<<c.imag<<endl;
  152.                s<<"z = "<<c.real<<setiosflags(ios::showpos)
  153.                 <<c.imag<<"i"<<endl<<resetiosflags(ios::showpos);
  154.                 return s;
  155.             }
  156.  
  157.  
  158.  
  159. int main()
  160. {
  161.      complex a(10.0f,-2.f); // Calls Constructor
  162.    cout<<a<<endl;               // Calls the overloaded << operator
  163.    complex b(-2);         // Calls Constructor
  164.    complex c=b;                    // Calls Copy Constructor
  165.    c=a;                                   // calls overloaded = operator
  166.    b.getdata();                    // Calls Getdata()
  167.    c.getdata();
  168.    if(b==c)            // calls overloaded == operator
  169.       cout<<"b == c";
  170.       else
  171.       cout<<"b != c";
  172.  
  173.  
  174.    cout<<endl<<c.getmodulus()<<endl; // calls getmodulus function()
  175.    complex d;
  176.    d=a+b;   // Calls overloaded +
  177.    cout<<d<<endl;
  178.    d=a-b;     // Calls overloaded -
  179.    cout<<d<<endl;
  180.    d=a*b;        // calls overloaded *
  181.    cout<<d<<endl;
  182.    d=a/b;        // calls overloaded /
  183.    cout<<d<<endl;
  184.  
  185.    return 0;
  186. }

Copy & Paste


Comments


TheMagnitude 2008-02-25 14:20:05

Nice job

Sleepyman 2008-04-06 06:15:46

There is an error in the multiplication function. The second subtraction sign should be addition instead.

born2c0de 2008-04-13 01:03:31

Thanks for pointing that out. I have corrected the code.


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month
-->