| | |
Program Involving Overloading
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Sep 2006
Posts: 17
Reputation:
Solved Threads: 0
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:
This is the complex.h file:
This is the original driver:
Thanks in advance for your help!
This is the complex.cpp file:
C++ Syntax (Toggle Plain Text)
#include "complex.h" #include <iostream> using namespace std; //using std::ostream; //using std::istream; // Constructor Complex::Complex( double realPart, double imaginaryPart ) { real = realPart; imaginary = imaginaryPart; } // end class Complex constructor // addition operator Complex Complex::operator+( const Complex &operand2 ) const { return Complex( real + operand2.real, imaginary + operand2.imaginary ); } // end function operator+ // subtraction operator Complex Complex::operator-( const Complex &operand2 ) const { return Complex( real - operand2.real, imaginary - operand2.imaginary ); } // end function operator- // multiplication operator Complex Complex::operator*( const Complex &operand2 ) const { return Complex( real * operand2.real, imaginary * operand2.imaginary ); } // end function operator* bool Complex::operator==( const Complex &operand2 ) const { if((real == operand2.real) && (imaginary == operand2.imaginary)) { return true; } else { return false; } } // end function operator== bool Complex::operator!=( const Complex &operand2 ) const { if((real != operand2.real) && (imaginary != operand2.imaginary)) { return true; } else { return false; } } // end function operator!= // Overloaded << operator ostream& operator<<( ostream &output, const Complex &complex ) { output << complex.real << " + " << complex.imaginary << 'i'; return output; } // end function operator<< // Overloaded >> operator istream& operator>>( istream &input, Complex &complex ) { input >> complex.real; input.ignore( 3 ); // skip spaces and + input >> complex.imaginary; input.ignore( 2 ); return input; } // end function operator>>
This is the complex.h file:
C++ Syntax (Toggle Plain Text)
#ifndef COMPLEX_H #define COMPLEX_H #include <iostream> using std::ostream; using std::istream; // class Complex definition class Complex { friend ostream &operator<<( ostream &, const Complex & ); friend istream &operator>>( istream &, Complex & ); public: Complex( double = 0.0, double = 0.0 ); // constructor Complex operator+( const Complex& ) const; // addition Complex operator-( const Complex& ) const; // subtraction Complex operator*( const Complex& ) const; // multiplication Complex& operator=( const Complex& ); // assignment bool operator==( const Complex& ) const; bool operator!=( const Complex& ) const; private: double real; // real part double imaginary; // imaginary part }; // end class Complex #endif // COMPLEX_H
This is the original driver:
C++ Syntax (Toggle Plain Text)
#include <iostream> using std::cout; using std::endl; #include "Complex.h" int main() { Complex x; Complex y( 4.3, 8.2 ); Complex z( 3.3, 1.1 ); cout << "x: "; x.print(); cout << "\ny: "; y.print(); cout << "\nz: "; z.print(); x = y + z; cout << "\n\nx = y + z:" << endl; x.print(); cout << " = "; y.print(); cout << " + "; z.print(); x = y - z; cout << "\n\nx = y - z:" << endl; x.print(); cout << " = "; y.print(); cout << " - "; z.print(); cout << endl; return 0; } // end main
Thanks in advance for your help!
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;•
•
Join Date: Sep 2006
Posts: 17
Reputation:
Solved Threads: 0
•
•
•
•
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)
[Linker error] undefined reference to `Complex::Complex(double, double)'
I think the problem is in this section:
[code]
Complex x;
Complex y( 4.3, 8.2 );
Complex z( 3.3, 1.1 );
[/cod]
The problem is that you're calling
When you haven't defined or implemented a default constructor for
c Syntax (Toggle Plain Text)
Complex x;
Complex. Add one, and the errors should be gone. "Technological progress is like an axe in the hands of a pathological criminal."
•
•
Join Date: Sep 2006
Posts: 17
Reputation:
Solved Threads: 0
•
•
•
•
The problem is that you're calling
When you haven't defined or implemented a default constructor forc Syntax (Toggle Plain Text)
Complex x;Complex. Add one, and the errors should be gone.
•
•
Join Date: Sep 2006
Posts: 17
Reputation:
Solved Threads: 0
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:
I created a complex object in the complexdriver.cpp file,
but I'm still getting the same error.
C++ Syntax (Toggle Plain Text)
Complex( double = 0.0, double = 0.0 ); // constructor
I created a complex object in the complexdriver.cpp file,
C++ Syntax (Toggle Plain Text)
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.
•
•
Join Date: Jul 2005
Posts: 1,676
Reputation:
Solved Threads: 262
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.
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.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Counter changing to a negative number
- Next Thread: ahhhh program wont do my else statement
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






