944,116 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 4447
  • C++ RSS
Oct 31st, 2006
0

Program Involving Overloading

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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!
Similar Threads
Reputation Points: 17
Solved Threads: 0
Newbie Poster
mikeallen is offline Offline
17 posts
since Sep 2006
Oct 31st, 2006
0

Re: Program Involving Overloading

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;
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Nov 1st, 2006
0

Re: Program Involving Overloading

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
C++ Syntax (Toggle Plain Text)
  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]
Reputation Points: 17
Solved Threads: 0
Newbie Poster
mikeallen is offline Offline
17 posts
since Sep 2006
Nov 1st, 2006
1

Re: Program Involving Overloading

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.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Nov 1st, 2006
0

Re: Program Involving Overloading

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!
Reputation Points: 17
Solved Threads: 0
Newbie Poster
mikeallen is offline Offline
17 posts
since Sep 2006
Nov 1st, 2006
0

Re: Program Involving Overloading

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:

C++ Syntax (Toggle Plain Text)
  1. Complex( double = 0.0, double = 0.0 ); // constructor

I created a complex object in the complexdriver.cpp file,
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 17
Solved Threads: 0
Newbie Poster
mikeallen is offline Offline
17 posts
since Sep 2006
Nov 1st, 2006
1

Re: Program Involving Overloading

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.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Nov 1st, 2006
0

Re: Program Involving Overloading

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Nov 1st, 2006
0

Re: Program Involving Overloading

Thanks everybody! I got my program working.
Reputation Points: 17
Solved Threads: 0
Newbie Poster
mikeallen is offline Offline
17 posts
since Sep 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Counter changing to a negative number
Next Thread in C++ Forum Timeline: ahhhh program wont do my else statement





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC