Solved

Destructor, do I need one?  C++

Posted on 2007-11-25
11
293 Views
Last Modified: 2010-04-24
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
0
Comment
Question by:urobins
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
  • 2
  • +1
11 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 250 total points
ID: 20345697
You don't really need one since the only data contained in the class are two doubles (which will be removed automatically), but it doesn't hurt to put in an empty destructor.
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 250 total points
ID: 20345703
You do not explicitly require one (you aren't assigning memory to be cleaned up).

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

Author Comment

by:urobins
ID: 20345712
so to put one in that didn't do anything I would just do

Complex::~Complex
{
}

Is that correct?
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

Author Comment

by:urobins
ID: 20345713
You guys are quick by the way :)
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20345714
Almost :

Complex::~Complex()
{
}
0
 

Author Comment

by:urobins
ID: 20345725
oh doh!  Thanks guys.  I'm gonna split the points between you guys since I saw both answers at the same time, as always you guys are great!  
0
 

Author Closing Comment

by:urobins
ID: 31410848
As always Ininity08 and AndyAinscow were quick and provided the correct answer.  Stellar support and friendly too.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20345738
Previous experts have clearly stated that constructor is unnecessay but desirable.
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.
0
 

Author Comment

by:urobins
ID: 20345759
Thanks jaime_olivares, I appreciate the addition!  I looked at the link you provided about overloading my assignment operator and that is where I saw the destructor referenced.  I proceeded to look into that and it always showed deleteing or setting some value to 0 but it didn't appear I really had anything like that, so I was a bit confused.  I have everything working now, so thanks again to everyone who helped getting me going the right direction!
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20345774
sorry!!! big typo!!! Instead of:
>>Previous experts have clearly stated that constructor is unnecessay but desirable.
should be:
Previous experts have clearly stated that DESTRUCTOR is unnecessay but desirable.
0
 

Author Comment

by:urobins
ID: 20345786
HAHA I didn't even notice it said Destructor over constructor :) I read what I wanted to see I guess :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
Suggested Courses

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question