Results 1 to 9 of 9
  1. #1
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    CS Code help, number program

    me again lol, got another assignment i'm stuck on, The main body of the program is done, i just need to fill in the functions and make the main.

    Create a class called Complex that represents a complex number. You should provide the following functionality:

    * a default constructor that makes it 0
    * a constructor that allows the setting of the real and imaginary parts
    * a method to produce the conjugate
    * operators for addition, subtraction, multiplication, and division
    * an operator for printing to a stream

    You should also create a main function that tests all of this functionality.




    What i have so far.

    Code:
    #include <iostream>
    using namespace std;
    
    class Complex {
        friend Complex operator+(Complex lhs, Complex rhs);
        friend Complex operator-(Complex lhs, Complex rhs);
        friend Complex operator*(Complex lhs, Complex rhs);
        friend Complex operator/(Complex lhs, Complex rhs);
        friend ostream& operator<<(ostream& lhs, Complex rhs);
        // 2+3i    5-6i   4   2i   0
    
        private:
            double real;
            double imag;
    
        public:
            Complex(); // Should become 0.
            Complex(double r, double i);
            Complex conjugate();
            double getReal() { return real; }
            double getImag() { return imag; }
    };
    
    Complex::Complex() {
        real = imag = 0;
    }
    
    Complex::Complex(double r, double i) {
        real = r;
        imag = i;
    }
    
    Complex Complex::conjugate() {
        Complex c(real, -imag);
        return c;
    }
    
    //(a+bi) + (c+di) = (a+c) + (b+d)i
    Complex operator+(Complex lhs, Complex rhs) {
    
    }
    
    //(a+bi) - (c+di) = (a-c) + (b-d)i
    Complex operator-(Complex lhs, Complex rhs) {
    
    }
    
    //(a+bi) * (c+di) = (ac-bd) + (bc+ad)i
    Complex operator*(Complex lhs, Complex rhs) {
    
    }
    
    // (a+bi) / (c+di) = 
    Complex operator/(Complex lhs, Complex rhs) {
    
    }
    
    ostream& operator<<(ostream& lhs, Complex rhs) {
    
    }

  2. #2
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    i found this online but it adds to much bs.

    Code:
    #include <cmath>
    #include <iostream>
    #include <iomanip.h>
    
    using namespace std;
    
    
    class complex
    {
         private:
                      float real;               // Real Part
                float imag;      //  Imaginary Part
    
    
       public:
              complex(float,float);
              complex(complex&);
              complex operator +(complex);
              complex operator -(complex);
              complex operator *(complex);
              complex operator /(complex);
              complex getconjugate();
              complex getreciprocal();
              float getmodulus();
              void setdata(float,float);
              void getdata();
              float getreal();
              float getimaginary();
              bool operator ==(complex);
              void operator =(complex);
              friend ostream& operator <<(ostream &s,complex &c);
    };
    //                                        CONSTRUCTOR
                      complex::complex(float r=0.0f,float im=0.0f)
                {
                     real=r;
                   imag=im;
                }
    
    //                                 COPY CONSTRUCTOR
                complex::complex(complex &c)
                {
                     this->real=c.real;
                   this->imag=c.imag;
                }
    
    
                void complex::operator =(complex c)
                {
                   real=c.real;
                   imag=c.imag;
                }
    
    
                complex complex::operator +(complex c)
                {
                     complex tmp;
                   tmp.real=this->real+c.real;
                   tmp.imag=this->imag+c.imag;
                   return tmp;
                }
    
                complex complex::operator -(complex c)
                {
                     complex tmp;
                   tmp.real=this->real - c.real;
                   tmp.imag=this->imag - c.imag;
                   return tmp;
                }
    
                        complex complex::operator *(complex c)
                {
                     complex tmp;
                   tmp.real=(real*c.real)-(imag*c.imag);
                   tmp.imag=(real*c.imag)+(imag*c.real);
                   return tmp;
                }
    
                complex complex::operator /(complex c)
                {
                     float div=(c.real*c.real) + (c.imag*c.imag);
                   complex tmp;
                   tmp.real=(real*c.real)+(imag*c.imag);
                   tmp.real/=div;
                   tmp.imag=(imag*c.real)-(real*c.imag);
                   tmp.imag/=div;
                   return tmp;
                }
    
                complex complex::getconjugate()
                {
                     complex tmp;
                   tmp.real=this->real;
                   tmp.imag=this->imag * -1;
                   return tmp;
                }
    
                complex complex::getreciprocal()
                {
                     complex t;
                   t.real=real;
                   t.imag=imag * -1;
                   float div;
                   div=(real*real)+(imag*imag);
                   t.real/=div;
                   t.imag/=div;
                   return t;
                }
    
                float complex::getmodulus()
                {
                     float z;
                   z=(real*real)+(imag*imag);
                   z=sqrt(z);
                   return z;
                }
    
                void complex::setdata(float r,float i)
                {
                     real=r;
                   imag=i;
                }
    
                void complex::getdata()
                {
                     cout<<"Enter Real:";
                   cin>>this->real;
                   cout<<"Enter Imaginary:";
                   cin>>this->imag;
    
                }
    
                float complex::getreal()
                {
                     return real;
                }
    
                float complex::getimaginary()
                {
                     return imag;
                }
    
                bool complex::operator ==(complex c)
                {
                 return (real==c.real)&&(imag==c.imag) ? 1 : 0;
                 }
    
                  ostream& operator <<(ostream &s,complex &c)
                {
                     s<<"Real Part = "<<c.real<<endl
                    <<"Imaginary Part = "<<c.imag<<endl;
                   s<<"z = "<<c.real<<setiosflags(ios::showpos)
                    <<c.imag<<"i"<<endl<<resetiosflags(ios::showpos);
                    return s;
                }
    
    
    
    int main()
    {
         complex a(10.0f,-2.f); // Calls Constructor
       cout<<a<<endl;               // Calls the overloaded << operator
       complex b(-2);         // Calls Constructor
       complex c=b;                    // Calls Copy Constructor
       c=a;                                   // calls overloaded = operator
       b.getdata();                    // Calls Getdata()
       c.getdata();
       if(b==c)            // calls overloaded == operator
          cout<<"b == c";
          else
          cout<<"b != c";
    
    
       cout<<endl<<c.getmodulus()<<endl; // calls getmodulus function()
       complex d;
       d=a+b;   // Calls overloaded +
       cout<<d<<endl;
       d=a-b;     // Calls overloaded -
       cout<<d<<endl;
       d=a*b;        // calls overloaded *
       cout<<d<<endl;
       d=a/b;        // calls overloaded /
       cout<<d<<endl;
    
       return 0;
    }

  3. #3
    E. Body
    Join Date
    Jan 2007
    Posts
    2,103
    BG Level
    7

    What exactly are you stuck on with main?

  4. #4
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    atm i have no idea how to implement it. and i need to actually define the functions. im using the other code as refrence. im thinking i need to do something like.

    Code:
    Complex operator-(Complex lhs, Complex rhs) {
    	tmp.real = real - imag;
    	tmp.imag = imag - real;
    	return tmp;
    is this right?

  5. #5
    E. Body
    Join Date
    Jan 2007
    Posts
    2,103
    BG Level
    7

    No, first you need your scope resolution operator in front of the function name. Second, you don't need to pass in two parameters, you only need one. You can use the dot operator on one to subtract/add/whatever it from the variable you pass in. Also, you'll have to instantiate tmp. Once you have the functions written, testing it in main will be the easy part.

  6. #6
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    Quote Originally Posted by Eliseos View Post
    No, first you need your scope resolution operator in front of the function name. Second, you don't need to pass in two parameters, you only need one. You can use the dot operator on one to subtract/add/whatever it from the variable you pass in. Also, you'll have to instantiate tmp. Once you have the functions written, testing it in main will be the easy part.
    can you give an example of this by chance please.

  7. #7
    E. Body
    Join Date
    Jan 2007
    Posts
    2,103
    BG Level
    7

    When you overload a binary operator, the object on the left side of the operator calls (passes) the object on the right side. I am not going in to much more detail than that since I'm assuming this is homework.

  8. #8
    Cerberus
    Join Date
    Oct 2008
    Posts
    436
    BG Level
    4

    Code:
    //(a+bi) + (c+di) = (a+c) + (b+d)i
    Complex operator+(Complex lhs, Complex rhs) {
    	complex add; 
    	add.real = lhs.real + rhs.real;
    	add.imag = lhs.imag + rhs.imag;
    	return add;
    }
    
    //(a+bi) - (c+di) = (a-c) + (b-d)i
    Complex operator-(Complex lhs, Complex rhs) {
    	complex minus;
    	minus.real = lhs.real - rhs.real;
    	minus.imag = lhs.imag - rhs.imag;
    	return minus;
    }
    
    //(a+bi) * (c+di) = (ac-bd) + (bc+ad)i
    Complex operator*(Complex lhs, Complex rhs) {
    	complex times;
    	times.real = lhs.real * rhs.real;
    	times.imag = lhs.imag * rhs.imag;
    }

    is this right?

  9. #9
    E. Body
    Join Date
    Jan 2007
    Posts
    2,103
    BG Level
    7

    No, like I said, you only should be passing in one parameter. Also, write your main function before trying to implement all of your functions. Write a simple test case of adding two complex numbers then output the result to see if it's working correctly. Once you get one function working, the rest will be much easier to implement. Do you understand the terms I've been using like parameter and object?

Similar Threads

  1. Java Code help
    By Skie in forum Tech
    Replies: 11
    Last Post: 2010-10-11, 14:37
  2. c++ code help
    By Skie in forum Tech
    Replies: 19
    Last Post: 2010-02-17, 21:27
  3. c++ code help version 3.
    By Skie in forum Tech
    Replies: 3
    Last Post: 2009-12-04, 17:56
  4. Windows XP code help
    By Etice in forum Tech
    Replies: 3
    Last Post: 2009-07-16, 14:25