Program Involving Overloading

Thread Solved
Reply

Join Date: Sep 2006
Posts: 17
Reputation: mikeallen is an unknown quantity at this point 
Solved Threads: 0
mikeallen mikeallen is offline Offline
Newbie Poster

Program Involving Overloading

 
0
  #1
Oct 31st, 2006
I'm trying to write a program that uses overloading of operators to perform operations (add, multiply, etc.). I've got the program done, but I'm having trouble modifying the driver I have to fit the program. The original program used a print method, and the new program uses overloading input and output operators. I'm having trouble using the overloaded output and input operators. I know that I have to use them instead of using the print() function, but I'm not sure how to do that.

This is the complex.cpp file:

  1. #include "complex.h"
  2. #include <iostream>
  3. using namespace std;
  4. //using std::ostream;
  5. //using std::istream;
  6. // Constructor
  7. Complex::Complex( double realPart, double imaginaryPart )
  8. {
  9. real = realPart;
  10. imaginary = imaginaryPart;
  11. } // end class Complex constructor
  12. // addition operator
  13. Complex Complex::operator+( const Complex &operand2 ) const
  14. {
  15. return Complex( real + operand2.real,
  16. imaginary + operand2.imaginary );
  17. } // end function operator+
  18. // subtraction operator
  19. Complex Complex::operator-( const Complex &operand2 ) const
  20. {
  21. return Complex( real - operand2.real,
  22. imaginary - operand2.imaginary );
  23. } // end function operator-
  24. // multiplication operator
  25. Complex Complex::operator*( const Complex &operand2 ) const
  26. {
  27. return Complex( real * operand2.real,
  28. imaginary * operand2.imaginary );
  29. } // end function operator*
  30. bool Complex::operator==( const Complex &operand2 ) const
  31. {
  32. if((real == operand2.real) && (imaginary == operand2.imaginary))
  33. {
  34. return true;
  35. }
  36. else
  37. {
  38. return false;
  39. }
  40. } // end function operator==
  41. bool Complex::operator!=( const Complex &operand2 ) const
  42. {
  43. if((real != operand2.real) && (imaginary != operand2.imaginary))
  44. {
  45. return true;
  46. }
  47. else
  48. {
  49. return false;
  50. }
  51. } // end function operator!=
  52. // Overloaded << operator
  53. ostream& operator<<( ostream &output, const Complex &complex )
  54. {
  55. output << complex.real << " + " << complex.imaginary << 'i';
  56. return output;
  57. } // end function operator<<
  58. // Overloaded >> operator
  59. istream& operator>>( istream &input, Complex &complex )
  60. {
  61. input >> complex.real;
  62. input.ignore( 3 ); // skip spaces and +
  63. input >> complex.imaginary;
  64. input.ignore( 2 );
  65. return input;
  66. } // end function operator>>

This is the complex.h file:
  1.  
  2. #ifndef COMPLEX_H
  3. #define COMPLEX_H
  4. #include <iostream>
  5. using std::ostream;
  6. using std::istream;
  7. // class Complex definition
  8. class Complex {
  9. friend ostream &operator<<( ostream &, const Complex & );
  10. friend istream &operator>>( istream &, Complex & );
  11. public:
  12. Complex( double = 0.0, double = 0.0 ); // constructor
  13. Complex operator+( const Complex& ) const; // addition
  14. Complex operator-( const Complex& ) const; // subtraction
  15. Complex operator*( const Complex& ) const; // multiplication
  16. Complex& operator=( const Complex& ); // assignment
  17. bool operator==( const Complex& ) const;
  18. bool operator!=( const Complex& ) const;
  19. private:
  20. double real; // real part
  21. double imaginary; // imaginary part
  22. }; // end class Complex
  23. #endif // COMPLEX_H

This is the original driver:
  1. #include <iostream>
  2. using std::cout;
  3. using std::endl;
  4. #include "Complex.h"
  5. int main()
  6. {
  7. Complex x;
  8. Complex y( 4.3, 8.2 );
  9. Complex z( 3.3, 1.1 );
  10. cout << "x: ";
  11. x.print();
  12. cout << "\ny: ";
  13. y.print();
  14. cout << "\nz: ";
  15. z.print();
  16. x = y + z;
  17. cout << "\n\nx = y + z:" << endl;
  18. x.print();
  19. cout << " = ";
  20. y.print();
  21. cout << " + ";
  22. z.print();
  23. x = y - z;
  24. cout << "\n\nx = y - z:" << endl;
  25. x.print();
  26. cout << " = ";
  27. y.print();
  28. cout << " - ";
  29. z.print();
  30. cout << endl;
  31. return 0;
  32. } // end main

Thanks in advance for your help!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,160
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1437
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: Program Involving Overloading

 
1
  #2
Oct 31st, 2006
you have already coded all the required overload operators. In main() just use them like this
int main()
{

   Complex x;
   Complex y( 4.3, 8.2 );
   Complex z( 3.3, 1.1 );
   cout << "x: " << x;
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 17
Reputation: mikeallen is an unknown quantity at this point 
Solved Threads: 0
mikeallen mikeallen is offline Offline
Newbie Poster

Re: Program Involving Overloading

 
0
  #3
Nov 1st, 2006
Originally Posted by Ancient Dragon View Post
you have already coded all the required overload operators. In main() just use them like this
int main()
{
 
   Complex x;
   Complex y( 4.3, 8.2 );
   Complex z( 3.3, 1.1 );
   cout << "x: " << x;

I tried to do what you said, but I'm still having a problem. I keep getting an error message
  1. [Linker error] undefined reference to `Complex::Complex(double, double)'
  2.  

I think the problem is in this section:
[code]
Complex x;
Complex y( 4.3, 8.2 );
Complex z( 3.3, 1.1 );
[/cod]
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,050
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 331
Moderator
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Program Involving Overloading

 
1
  #4
Nov 1st, 2006
The problem is that you're calling
  1. Complex x;
When you haven't defined or implemented a default constructor for Complex. Add one, and the errors should be gone.
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 17
Reputation: mikeallen is an unknown quantity at this point 
Solved Threads: 0
mikeallen mikeallen is offline Offline
Newbie Poster

Re: Program Involving Overloading

 
0
  #5
Nov 1st, 2006
Originally Posted by joeprogrammer View Post
The problem is that you're calling
  1. Complex x;
When you haven't defined or implemented a default constructor for Complex. Add one, and the errors should be gone.
How exactly would I do that? I thought I implmented the Complex objet in the complex.h file with the line "Complex complex;" Thank you so much for your help!
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 17
Reputation: mikeallen is an unknown quantity at this point 
Solved Threads: 0
mikeallen mikeallen is offline Offline
Newbie Poster

Re: Program Involving Overloading

 
0
  #6
Nov 1st, 2006
How exactly would I do that? I thought that i was implementing the default constructor in the complex.h file with this line of code:

  1. Complex( double = 0.0, double = 0.0 ); // constructor

I created a complex object in the complexdriver.cpp file,
  1. Complex complex; // create a complex object

but I'm still getting the same error.
Last edited by mikeallen; Nov 1st, 2006 at 10:25 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,581
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 461
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Program Involving Overloading

 
1
  #7
Nov 1st, 2006
Naa you aint defining the default constructor, you are just declaring it in Complex.h but its defination doesnt occur in Complex.cpp, thats the whole root of the problem.

Implement one in complex.cpp and all should work out fine.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,671
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 261
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Program Involving Overloading

 
0
  #8
Nov 1st, 2006
Complex( double = 0.0, double = 0.0 ); // declaration of constructor taking two doubles as arguments, each argument defaulting to 0.0 if no parameter passed in.

Complex() : real(0.0), imaginary(0.0) {} //declaration and definition of default constructor setting real and imaginary data members to 0.0. No arguments can be passed to constructor.

If you don't declare any constructors the compiler will develop a default constructor and a copy constructor for you. If you explicitly declare any constructor, then you should declare at least your own default constructor explicitly, and I would recommend you declare the copy constructor explicitly, as well.
Last edited by Lerner; Nov 1st, 2006 at 1:19 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 17
Reputation: mikeallen is an unknown quantity at this point 
Solved Threads: 0
mikeallen mikeallen is offline Offline
Newbie Poster

Re: Program Involving Overloading

 
0
  #9
Nov 1st, 2006
Thanks everybody! I got my program working.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC