W
wyndz0108
Hi,
I have this working program (see first program below) that works
without an explicit copy constructor:
//--------------------------- start of first program
-----------------------------//
#include <iostream>
using namespace std;
class Complex
{
public:
Complex() { real = 0 ; imag = 0 ; } // default constructor
Complex(int r, int i); // convert constructor
// overload the following operators
Complex operator - () ; // unary negation
int real;
int imag;
};
int main () {
Complex x(1,8); // input is 1+8i
Complex z ; // output should be -1 + -8i
z=-x;
cout << z.real << " + " << z.imag <<"i";
}
Complex::Complex(int r, int i): real(r), imag(i) {}
Complex Complex:perator- () {
return Complex(-real, -imag);
}
// --------------------- end of first program
------------------------- //
The error occurs when I added a copy constructor (see second program
below).
//------------------- start of second program ----------------------//
#include <iostream>
using namespace std;
class Complex
{
public:
Complex() { real = 0 ; imag = 0 ; } // default constructor
Complex (Complex &) ; // added copy constructor
Complex(int r, int i); // convert constructor
// overload the following operators
Complex operator - () ; // unary negation
int real;
int imag;
};
int main () {
Complex x(1,8); // input is 1+8i
Complex y(x);
Complex z ; // output should be -1 + -8i
z=-x;
cout << z.real << " + " << z.imag <<"i";
}
Complex::Complex(int r, int i): real(r), imag(i) {}
Complex::Complex(Complex & copy): real(copy.real), imag(copy.imag) {}
Complex Complex:perator- () {
return Complex(-real, -imag);
}
//------------------------ end of second program
------------------------------//
Please help.
Thank you.
I have this working program (see first program below) that works
without an explicit copy constructor:
//--------------------------- start of first program
-----------------------------//
#include <iostream>
using namespace std;
class Complex
{
public:
Complex() { real = 0 ; imag = 0 ; } // default constructor
Complex(int r, int i); // convert constructor
// overload the following operators
Complex operator - () ; // unary negation
int real;
int imag;
};
int main () {
Complex x(1,8); // input is 1+8i
Complex z ; // output should be -1 + -8i
z=-x;
cout << z.real << " + " << z.imag <<"i";
}
Complex::Complex(int r, int i): real(r), imag(i) {}
Complex Complex:perator- () {
return Complex(-real, -imag);
}
// --------------------- end of first program
------------------------- //
The error occurs when I added a copy constructor (see second program
below).
//------------------- start of second program ----------------------//
#include <iostream>
using namespace std;
class Complex
{
public:
Complex() { real = 0 ; imag = 0 ; } // default constructor
Complex (Complex &) ; // added copy constructor
Complex(int r, int i); // convert constructor
// overload the following operators
Complex operator - () ; // unary negation
int real;
int imag;
};
int main () {
Complex x(1,8); // input is 1+8i
Complex y(x);
Complex z ; // output should be -1 + -8i
z=-x;
cout << z.real << " + " << z.imag <<"i";
}
Complex::Complex(int r, int i): real(r), imag(i) {}
Complex::Complex(Complex & copy): real(copy.real), imag(copy.imag) {}
Complex Complex:perator- () {
return Complex(-real, -imag);
}
//------------------------ end of second program
------------------------------//
Please help.
Thank you.