This course will introduce you to C++ 11 and teach you about syntax fundamentals.

I am working on my assignment for a class, everything works, but when reading about copy constructors they often mention needing a destructor. I wasn't sure if I needed one. It seems that most examples are using arrays or dynamic content, which I don't use. Can you look at my code and say if I need one? Thanks!

// Fig. 8.16: complex1.cpp

// Complex class member function definitions.

//Used the multiplication algorhythm posted in week 11 conference.

//Had some help from various sources tweaking my overloading.

#include <iostream>

using std::cout;

#include"complex1.h"// Complex class definition

// constructor

Complex::Complex( double realPart, double imaginaryPart )

: real( realPart ),

imaginary( imaginaryPart )

{

// empty body

} // end Complex constructor

//Copy Constructor

Complex::Complex(const Complex& operand2)

{

real = operand2.real;

imaginary = operand2.imaginary;

}

// 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-

// I used the algorhthym posted in our conference. I had something similar but my parens were off I think :)

Complex Complex::operator*( const Complex &operand2 ) const

{

return Complex( ((real * operand2.real)-(imaginary * operand2.imaginary)), ((real * operand2.imaginary)+(operand2.real * imaginary)) );

} // end function operator*

Complex Complex::operator=(const Complex& operand2 )

{

if (this != &operand2)

{

real = operand2.real;

imaginary = operand2.imaginary;

}

return *this;

}

std::ostream& operator<<(std::ostream& stream,const Complex& complexNumber)

{

stream << "(" << complexNumber.getReal() << " , " << complexNumber.getImaginary()<<")" ;

return stream;

}

std::istream& operator>>(std::istream& stream,Complex& value)

{

double realPart, imaginaryPart;

stream >> realPart >> imaginaryPart;

value.getReal(realPart);

value.getImaginary(imaginaryPart);

return stream;

}

bool Complex::operator==( const Complex &operand2 ) const

{

if (real == operand2.real && imaginary == operand2.imaginary)

return true;

else

return false;

}

bool Complex::operator!=( const Complex &operand2 ) const

{

if (real != operand2.real || imaginary != operand2.imaginary)

return true;

else

return false;

}

---------------

#ifndef COMPLEX1_H

#define COMPLEX1_H

class Complex {

public:

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

Complex(const Complex& operand2); //Copy Constructor

Complex operator+( const Complex & ) const; // addition

Complex operator-( const Complex & ) const; // subtraction

Complex operator*( const Complex &operand2 ) const; // Multiplication

Complex operator=( const Complex& operand2 ); //overloaded assignment operator

bool operator==( const Complex &operand2 ) const;

bool operator!=( const Complex &operand2 ) const;

//Friend overloaded Stream operators

friend std::ostream &operator<<(std::ostream &stream, const Complex &value);

friend std::istream &operator>>(std::istream &stream, Complex &value);

//accessor functions

double getReal() const { return real; }

double getImaginary() const { return imaginary; }

void getReal(double r) { real = r; }

void getImaginary(double i) { imaginary = i; }

private:

double real; // real part

double imaginary; // imaginary part

}; // end class Complex

#endif

// Fig. 8.16: complex1.cpp

// Complex class member function definitions.

//Used the multiplication algorhythm posted in week 11 conference.

//Had some help from various sources tweaking my overloading.

#include <iostream>

using std::cout;

#include"complex1.h"// Complex class definition

// constructor

Complex::Complex( double realPart, double imaginaryPart )

: real( realPart ),

imaginary( imaginaryPart )

{

// empty body

} // end Complex constructor

//Copy Constructor

Complex::Complex(const Complex& operand2)

{

real = operand2.real;

imaginary = operand2.imaginary;

}

// 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-

// I used the algorhthym posted in our conference. I had something similar but my parens were off I think :)

Complex Complex::operator*( const Complex &operand2 ) const

{

return Complex( ((real * operand2.real)-(imaginary * operand2.imaginary)), ((real * operand2.imaginary)+(opera

} // end function operator*

Complex Complex::operator=(const Complex& operand2 )

{

if (this != &operand2)

{

real = operand2.real;

imaginary = operand2.imaginary;

}

return *this;

}

std::ostream& operator<<(std::ostream& stream,const Complex& complexNumber)

{

stream << "(" << complexNumber.getReal() << " , " << complexNumber.getImaginary

return stream;

}

std::istream& operator>>(std::istream& stream,Complex& value)

{

double realPart, imaginaryPart;

stream >> realPart >> imaginaryPart;

value.getReal(realPart);

value.getImaginary(imagina

return stream;

}

bool Complex::operator==( const Complex &operand2 ) const

{

if (real == operand2.real && imaginary == operand2.imaginary)

return true;

else

return false;

}

bool Complex::operator!=( const Complex &operand2 ) const

{

if (real != operand2.real || imaginary != operand2.imaginary)

return true;

else

return false;

}

---------------

#ifndef COMPLEX1_H

#define COMPLEX1_H

class Complex {

public:

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

Complex(const Complex& operand2); //Copy Constructor

Complex operator+( const Complex & ) const; // addition

Complex operator-( const Complex & ) const; // subtraction

Complex operator*( const Complex &operand2 ) const; // Multiplication

Complex operator=( const Complex& operand2 ); //overloaded assignment operator

bool operator==( const Complex &operand2 ) const;

bool operator!=( const Complex &operand2 ) const;

//Friend overloaded Stream operators

friend std::ostream &operator<<(std::ostream &stream, const Complex &value);

friend std::istream &operator>>(std::istream &stream, Complex &value);

//accessor functions

double getReal() const { return real; }

double getImaginary() const { return imaginary; }

void getReal(double r) { real = r; }

void getImaginary(double i) { imaginary = i; }

private:

double real; // real part

double imaginary; // imaginary part

}; // end class Complex

#endif

However having a destructor would be (in my opinion) better coding - even if the destructor doesn't do anything

Still there is a use for your destructor (and your constructor too). You can use it to detect memory leaks, when you forget to free dynamically allocated objects:

public class Complex

{

public:

static int objectCount = 0;

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

Complex(const Complex& operand2); //Copy Constructor

~Complex(); // destructor

}

Complex::Complex( double realPart, double imaginaryPart ) : real( realPart ), imaginary( imaginaryPart )

{

objectCount++;

}

Complex::Complex(const Complex& operand2)

{

real = operand2.real;

imaginary = operand2.imaginary;

objectCount++;

}

Complex::~Complex()

{

objectCount--;

}

So, before ending your application, objectCount should be 0, if not, there is a memory leak.

Also you can put messages in both contructors and destructor, for testing purposes.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.