Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 c++ please help
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
tinhnho




PostPosted: Sat Mar 13, 2004 5:47 pm   Post subject: c++ please help

Hi everyone

i have a proplem with C++.Here is the question:
Use the class complex .Write two(2) versions ,a nonmember function friend version and a member function version.Bóth them have the binary operator functions add,multiply, and substract.Each should return complex.
Declare three complex objects ,c1, c2 ,c3.Initialize the objects c1,c2 to values of your choice and then print thẹm Invoke each of the overloaded operators(eg., c3= c1+c2 ;)and print the resulting objects c3

Here is the complex function and what i thinking:
code:

#include <iostream.h>
#include <cstdlib>


class complex {
public:
   complex(double r) {real = r ; imag =0;}
   void assign(double r, double i){ real = r; imag = i;}
   void print() { cout << real << " + " << imag << "i";}
   operator double() {return (sqrt(real * real + imag * imag));}
private:
   double real,imag;
};

int main()
{
      complex c1(2), c2(3),c3;


     c3 = c1+c2;
     c3.print();

     c3=c1 -c2;
     c3.print();
   
     c3= c1 * c2;
     c3.print();

.....
      system("PAUSE");
      return 0;
}

note: more information
Example:
Multiple
c1 = 3+4i
c2 = -2 -5i
c3 = c1 * c2
==>c3 = 14 -23i

thanks for reading
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Sat Mar 13, 2004 7:58 pm   Post subject: (No subject)

You're headed in the right direction, but I don't think you've quite got your operator overloading syntax down.

For instance, to overload the + operator for the friend version...

This is shown to work

code:
#include <iostream>
#include <stdlib.h>

class complex {
   private:
      // private should usually always
      // be at the top of the class.
      double real, imaginary;
   public:
      // You're also missing the convenient
      // constructor syntax.
      complex(double r = 0.0, double i = 0.0)
      : real(r), imaginary(i)
      { }

      friend complex operator+(const complex& c1, const complex& c2);

      // And for output, the best C++ method is:
      friend std::ostream& operator<<(std::ostream& out, const complex& c);
};

complex operator+(const complex& c1, const complex& c2) {
   return complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
}

std::ostream& operator<<(std::ostream& out, const complex& c) {
   return out << c.real << " " << c.imaginary << "i";
}

int main() {
   complex c1(4, 2), c2(5, 7.1), c3;

   c3 = c1 + c2;

   std::cout << c3 << std::endl;
}
tinhnho




PostPosted: Sun Mar 14, 2004 12:22 am   Post subject: (No subject)

thanks for reply. i got it figure out. and it works fine.But do you have any idea for member function version?thanks
wtd




PostPosted: Sun Mar 14, 2004 12:37 am   Post subject: (No subject)

Well, just keep in mind that any function that's a member of a class has an implicit first argument called this.

Thus we can take one of the arguments away from our operator and it'll still funcion quite nicely.

code:
class complex {
   complex operator+(const complex& c) const;
};

complex complex::operator+(const complex& c) const {
   return complex(real + c.real, imaginary + c.imaginary);
}

/* Or the longer version:
complex complex::operator+(const complex& c) const {
   return complex(this->real + c.real, this->imaginary + c.imaginary);
} */
tinhnho




PostPosted: Sun Mar 14, 2004 2:59 am   Post subject: (No subject)

Here is for member function version:
code:

#include <iostream.h>
#include <cstdlib>

using std::cout;

class complex {
public:
   complex(double r) {real = r ; imag =0;}
   void assign(double r, double i){ real = r; imag = i;}
   void print() { cout << real << " + " << imag << "i\n";}

   operator double(){return (sqrt(real * real + imag * imag));}

   complex(double r = 0.0, double i = 0.0): real(r), imag(i){ }

   complex (const complex& a) { *this = a; } //copy contructor

   complex operator+(const complex& a)const;

   complex operator-(const complex& a)const;

   complex operator*(const complex& a) const;

private:
   double real,imag;
};

complex complex::operator+(const complex& a)const
{
   return complex(real + a.real, imag + a.imag);
}

complex complex::operator*(const complex& a)const
{
   return complex(real * a.real, imag + a.imag);
}

complex complex::operator-(const complex& a)const
{
   return complex(real - a.real, imag - a.imag);
}

int main()
{
   complex c1(4, 2), c2(5, 7), c3;

   cout << "c1= ";
   c1.print();
   cout << "c2= ";
   c2.print();

   // function add
   c3 = c1 + c2;
   cout << "c3= c1 + c2 = ";
   c3.print();

   //function multiply
   c3 = c1 * c2;
   cout << "c3= c1 * c2 =";
   c3.print();

   //function substract
   c3 = c1 - c2;
   cout << "c3= c1 - c2 =";
   c3.print();

   system(" pause");
   return 0;
}



And here is the nonmember function friend version:
code:

#include <iostream>
#include <stdlib.h>

using std::cout;

class complex {
public:
   complex(double r) {real = r ; imag =0;}
   void assign(double r, double i){ real = r; imag = i;}
   void print() { cout << real << " + " << imag << "i\n";}
   operator double() {return (sqrt(real * real + imag * imag));}
   complex(double r = 0.0, double i = 0.0): real(r), imag(i){ }

   friend ostream& operator<<(ostream& out, const complex& c);

   friend complex operator+(const complex& c1, const complex& c2);
   friend complex operator*(const complex& c1, const complex& c2);
   friend complex operator-(const complex& c1, const complex& c2);
private:
   double real, imag;

};

complex operator+(const complex& c1, const complex& c2)
{
   return complex(c1.real + c2.real, c1.imag + c2.imag);
}

complex operator*(const complex& c1, const complex& c2)
{
   return complex(c1.real * c2.real, c1.imag * c2.imag);
}

complex operator-(const complex& c1, const complex& c2)
{
   return complex(c1.real - c2.real, c1.imag - c2.imag);
}

ostream& operator<<(ostream& out, const complex& c)
{
   return out << c.real << " " << c.imag << "i";
}

int main() {
   complex c1(4, 2), c2(5, 7), c3;

   cout << "c1= ";
   c1.print();
   cout << "c2= ";
   c2.print();

   // function add
   c3 = c1 + c2;
   cout << "c3= c1 + c2 = ";
   c3.print();

   //function multiply
   c3 = c1 * c2;
   cout << "c3= c1 * c2 =";
   c3.print();

   //function substract
   c3 = c1 - c2;
   cout << "c3= c1 - c2 =";
   c3.print();




   system(" pause");
   return 0;
}


Here is the output for 2 program(they have out same each other):
c1= 4 + 2i
c2= 5 + 7i
c3= c1 + c2 = 9 + 9i
c3= c1 * c2 =20 + 9i
c3= c1 - c2 =-1 + -5i
Press any key to continue . . .

Please check and let me know if i got any error,thanks alot
wtd




PostPosted: Sun Mar 14, 2004 8:42 am   Post subject: (No subject)

Just a few notes... for your copy constructor, you'll want to copy the data inside the object. As it is, I think you're just pointing something else to the same bit of memory. Something like:

code:
complex(const complex& c) : real(r.real), imaginary(c.imaginary) { }


Also, with the overloaded << operator, you no longer need the print function. You can simply write:

code:
std::cout << "c3 = c1 + c2 = " << c3 << std::endl;
tinhnho




PostPosted: Sun Mar 14, 2004 12:43 pm   Post subject: (No subject)

thank for reply.In this program, do i need to have copy constructor?which way is the best ?thanks
tinhnho




PostPosted: Sun Mar 14, 2004 1:31 pm   Post subject: (No subject)

I used other compiler which is MS visual C++ 6 to compiled it.There are 16 errors in friend version.I used to use Dev C++ 4.Anyone have same proplem?
wtd




PostPosted: Sun Mar 14, 2004 5:24 pm   Post subject: (No subject)

tinhnho wrote:
I used other compiler which is MS visual C++ 6 to compiled it.There are 16 errors in friend version.I used to use Dev C++ 4.Anyone have same proplem?


If it works with Dev-C++ (which uses GCC as the compiler) then I wouldn't worry about it too much. MS Visual C++ 6 is notorious for not being compliant with standards. It's handling of input/output and templates are especially atrocious. I think Visual C++.NET improved this, but GCC's still a better bet.
tinhnho




PostPosted: Wed Mar 17, 2004 4:01 pm   Post subject: (No subject)

hi wtd

i have a proplem with this part.It doesn't work:
code:

complex operator*(const complex& c1, const complex& c2)
{
   return complex(c1.real* c2.real,c1.real * c2.imag,c1.imag* c2.real,c1.imag * c2.imag );
}

Example:
c1= 4 + 2i
c2= 5 + 7i
c3=c1 * c2=(4+2i)*(5+7i)= 20 +28i +10i +14i^2=14i^2 + 38i +20
i^2=-1
==>c3= 38i + 6
tinhnho




PostPosted: Wed Mar 17, 2004 5:38 pm   Post subject: (No subject)

nvm,i got it figure out,thanks alot
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: