Question: This is a code written to represnet a complex number using a class. After executing the below code although it executes without errors I get

This is a code written to represnet a complex number using a class. After executing the below code although it executes without errors I get a return value as below. This is because of a problem in the destructor which I have already discovered. I need a destructor and I also need to delete the dynamicallly allocated memory as well. Without changing any other thing in the code please find a way to do this without this return value 32222.... . The program runs without errors but i need to fix the memory overflow at last

This is a code written to represnet a complex number using a

complexnum.h

#include using namespace std; class Complex{ private: int *real,*img; //two pointer to hold real part and imaginery part public: //parametrize constructor to create complex objects Complex(int _real,int _img){ real = new int(_real); img = new int(_img); } //default constructor Complex(){ real = new int(0); img = new int(0); } //destructor ~Complex(){ delete real; delete img; } //print function to print complex number void print(){ //print a+bi if(*img>=0){ cout 

complexnum.cpp

#include"complexnum.h" using namespace std; int main(){ //variables to enter data of two complex numbers int real1,img1,real2,img2; //input from user for complex 1 cout>real1; cout>img1; //input from user form complex 2 cout>real2; cout>img2; //creating three complex numbers two of them from user input //and thrid is for testing all the operations Complex c1(real1,img1),c2(real2,img2),c3; cout  Process exited after 5.679 seconds with return value 3221226356 Press any key to continue

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!