#include %26lt;iostream%26gt;
using namespace std;
template %26lt;class T%26gt;
class Complex
{
friend ostream %26amp;operator%26lt;%26lt;( ostream %26amp;, const Complex %26amp; );
friend istream %26amp;operator%26gt;%26gt;( istream %26amp;, Complex %26amp; );
public:
Complex(const T = 0.0, const T = 0.0 ); // constructor
T%26amp; operator+( const Complex%26amp; ) const; // addition
T%26amp; operator-( const Complex%26amp; ) const; // subtraction
T%26amp; operator*( const Complex%26amp; ) const; // multiplication
T%26amp; operator=( const Complex%26amp; ); // assignment
T%26amp; operator==( const Complex%26amp; ) const;
T%26amp; operator!=( const Complex%26amp; ) const;
// T%26amp; operator[](double);
private:
double real; // real part
double imaginary; // imaginary part
};
// Constructor
template %26lt;class T%26gt;
Complex%26lt;T%26gt;::Complex(T r, T i)
{
T real = r;
T imaginary = i;
}
// Overloaded addition operator
template %26lt;class T%26gt;
T %26amp;Complex%26lt;T%26gt;::operator+(const Complex %26amp;operand2) const
{
Complex sum;
sum.real = real + operand2.real;
sum.imaginary = imaginary + operand2.imaginary;
return sum;
}
// Overloaded subtraction operator
template %26lt;class T%26gt;
T %26amp;Complex%26lt;T%26gt;::operator-(const Complex %26amp;operand2) const
{
Complex diff;
diff.real = real - operand2.real;
diff.imaginary = imaginary - operand2.imaginary;
return diff;
}
// Overloaded multiplication operator
template %26lt;class T%26gt;
T %26amp;Complex %26lt;T%26gt;::operator*(const Complex %26amp;operand2 ) const
{
Complex times;
times.real = real * operand2.real + imaginary * operand2.imaginary;
times.imaginary = real * operand2.imaginary + imaginary * operand2.real;
return times;
}
// Overloaded = operator
template %26lt;class T%26gt;
T %26amp;Complex%26lt;T%26gt;::operator=(const Complex %26amp;right )
{
real = right.real;
imaginary = right.imaginary;
return *this; // enables concatenation
}
template %26lt;class T%26gt;
T %26amp;Complex%26lt;T%26gt;::operator==( const Complex %26amp;right ) const
{ return right.real == real %26amp;%26amp; right.imaginary == imaginary ? true : false; }
template %26lt;class T%26gt;
T %26amp;Complex%26lt;T%26gt;::operator!=( const Complex %26amp;right ) const
{ return !( *this == right ); }
template %26lt;class T%26gt;
ostream%26amp; operator%26lt;%26lt;( ostream %26amp;output, const Complex%26lt;T%26gt; %26amp;complex )
{
output %26lt;%26lt; complex.real %26lt;%26lt; " + " %26lt;%26lt; complex.imaginary %26lt;%26lt; 'i';
return output;
}
template %26lt;class T%26gt;
istream%26amp; operator%26gt;%26gt;( istream %26amp;input, Complex%26lt;T%26gt; %26amp;complex )
{
input %26gt;%26gt; complex.real;
input.ignore( 3 ); // skip spaces and +
input %26gt;%26gt; complex.imaginary;
input.ignore( 2 );
return input;
}
//Use the following main function to test your program
int main()
{
double a=4.3, b = 8.2, c = 3.3, d = 1.1;
Complex%26lt;double%26gt; x, y(a,b), z(c,d), k;
cout %26lt;%26lt; "Enter a complex number in the form: a + bi\n? ";
cin %26gt;%26gt; k;
cout %26lt;%26lt; "x: " %26lt;%26lt; x %26lt;%26lt; "\ny: " %26lt;%26lt; y %26lt;%26lt; "\nz: " %26lt;%26lt; z %26lt;%26lt; "\nk: "
%26lt;%26lt; k %26lt;%26lt; '\n';
(x = y + z);
cout %26lt;%26lt; "\nx = y + z:\n" %26lt;%26lt; x %26lt;%26lt; " = " %26lt;%26lt; y %26lt;%26lt; " + " %26lt;%26lt; z %26lt;%26lt; '\n';
(x = y - z);
cout %26lt;%26lt; "\nx = y - z:\n" %26lt;%26lt; x %26lt;%26lt; " = " %26lt;%26lt; y %26lt;%26lt; " - " %26lt;%26lt; z %26lt;%26lt; '\n';
(x = y * z);
cout %26lt;%26lt; "\nx = y * z:\n" %26lt;%26lt; x %26lt;%26lt; " = " %26lt;%26lt; y %26lt;%26lt; " * " %26lt;%26lt; z %26lt;%26lt; "\n\n";
if ( x != k )
cout %26lt;%26lt; x %26lt;%26lt; " != " %26lt;%26lt; k %26lt;%26lt; '\n';
cout %26lt;%26lt; '\n';
x = k;
if ( x == k )
cout %26lt;%26lt; x %26lt;%26lt; " == " %26lt;%26lt; k %26lt;%26lt; '\n';
int e=4, f=8, g=3, h=2;
Complex%26lt;int%26gt; l, m( e,f), n(g,h ), p;
cout %26lt;%26lt; "Enter a complex number in the form: a + bi\n? ";
cin %26gt;%26gt; p;
cout %26lt;%26lt; "x: " %26lt;%26lt; l %26lt;%26lt; "\ny: " %26lt;%26lt; m %26lt;%26lt; "\nz: " %26lt;%26lt; n %26lt;%26lt; "\nk: "
%26lt;%26lt; p %26lt;%26lt; '\n';
(l = m + n);
cout %26lt;%26lt; "\nx = y + z:\n" %26lt;%26lt; l %26lt;%26lt; " = " %26lt;%26lt; m %26lt;%26lt; " + " %26lt;%26lt; n %26lt;%26lt; '\n';
(l = m - n);
cout %26lt;%26lt; "\nx = y - z:\n" %26lt;%26lt; l %26lt;%26lt; " = " %26lt;%26lt; m %26lt;%26lt; " - " %26lt;%26lt; n %26lt;%26lt; '\n';
(l = m * n);
cout %26lt;%26lt; "\nx = y * z:\n" %26lt;%26lt; l %26lt;%26lt; " = " %26lt;%26lt; m %26lt;%26lt; " * " %26lt;%26lt; n %26lt;%26lt; "\n\n";
if ( l != p )
cout %26lt;%26lt; l %26lt;%26lt; " != " %26lt;%26lt; p %26lt;%26lt; '\n';
cout %26lt;%26lt; '\n';
l = p;
if ( l == p )
cout %26lt;%26lt; l %26lt;%26lt; " == " %26lt;%26lt; p %26lt;%26lt; '\n';
return 0;
}
Need help fixing the errors in this Template C++Program. I'm lost.Seems to be the same error repeatedly.Thanks
Haven't compiled your code, but looking at the definitions:
-Your +,-,* operators should return Complex%26lt;T%26gt;, not T - when you add two Complex%26lt;T%26gt;, you want to get a Complex%26lt;T%26gt; object and not a T object back.
-Your ==,!= operators should return bool, as these are logical operators.
-Your private members should be of type T - otherwise, when you assign them, you are implicitly saying that T is a specific type.
-In your constructor, you are redeclaring your members; use something like
Complex( T r, T i ) : real( r ), imaginary( i ) { }
to initialize them.
-In your operator implementations, you need to set a type for your return object. You'll need to declare them as Complex%26lt;T%26gt; objects, not just Complex.
Reply:It might help to know what the error message is./
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment