Question

In: Computer Science

What problems could a programmer encounter if they defined a destructor for a class but no...

What problems could a programmer encounter if they defined a destructor for a class but no copy constructor? Illustrate your description with an example class.

Programming language: C++

Requirement: provide answer along with one code example with illustration.

Solutions

Expert Solution

If we do not have  copy constructor in the program, we don’t get the expected output.

In the Code below Without Copy constructor .The changes made to str2 reflect in str1 as well which is never expected.

This is because if we don’t define our own copy constructor, the C++ compiler creates a default copy constructor for each class which does a member-wise copy between objects. The compiler created copy constructor works fine in general But Default constructor does only shallow copy.

Here is code without Copy Constructor.

#include<iostream>
#include<cstring>
using namespace std;

class MyClass
{
private:
   char *s;
   int size;
public:
   MyClass(const char *str = NULL) // constructor
       {
           size = strlen(str);
           s = new char[size+1];
           strcpy(s, str);
       }

   ~MyClass() // destructor
{
delete [] s;
}
   void print()
{ cout << s << endl;
}
   void change(const char * str) // Function to change
{
       delete [] s;
       size = strlen(str);
       s = new char[size+1];
       strcpy(s, str);
}
};

int main()
{
   MyClass str1("String 1");
   MyClass str2 = str1;

   str1.print(); // what is printed ?
   str2.print();

   str2.change("String 2 Change");

   str1.print(); // what is printed now ?
   str2.print();
   return 0;
}

Now the code with Copy Constructor:

#include<iostream>
#include<cstring>
using namespace std;

class MyClass
{
private:
   char *s;
   int size;
public:
   MyClass(const char *str = NULL) // constructor
       {
           size = strlen(str);
           s = new char[size+1];
           strcpy(s, str);
       }
MyClass(const MyClass& old_str) //Copy constructor
       {
       size = old_str.size;
       s = new char[size+1];
       strcpy(s, old_str.s);
       }

   ~MyClass() // destructor
{
delete [] s;
}
   void print()
{ cout << s << endl;
}
   void change(const char * str) // Function to change
{
       delete [] s;
       size = strlen(str);
       s = new char[size+1];
       strcpy(s, str);
}
};

int main()
{
   MyClass str1("String 1");
   MyClass str2 = str1;

   str1.print(); // what is printed ?
   str2.print();

   str2.change("String 2 Change");

   str1.print(); // what is printed now ?
   str2.print();
   return 0;
}

See the difference here changes only made for str2 Object not with str1 this is because Our Copy Constructor has created a deep copy of str1 String in str2.SO If there is some changes with any object It will Reflect the changes only for that object not for all others.


Related Solutions

Description The primary purpose is to gain experience building a simple programmer-defined class. Requirements • Class...
Description The primary purpose is to gain experience building a simple programmer-defined class. Requirements • Class Rectangle: Create a new class named Rectangle that o is in its own Rectangle.cs file o has properly formatted class header docs with your name as the author and a simple class description such as “Class Rectangle represents a rectangle with a length and width.” o has private attributes (fields) named length and width that will hold integer values. o has public properties named...
Could we ever achieve an unemployment rate below full employment? What problems might we encounter if...
Could we ever achieve an unemployment rate below full employment? What problems might we encounter if it did.
Discuss the primary causes and problems that expatriate employees could encounter that may limit both their...
Discuss the primary causes and problems that expatriate employees could encounter that may limit both their effectiveness in foreign assignments and their contributions to the firm once they return home.
What situations could an older adult encounter during an acute hospitalization that could increase the risk...
What situations could an older adult encounter during an acute hospitalization that could increase the risk of sustaining a fracture?
What is a constructor ? What is a destructor? What is a default constructor? Is it...
What is a constructor ? What is a destructor? What is a default constructor? Is it possible to have more than one default constructor?
In C++ a class can have multiple constructors, but only one destructor. Explain why this is...
In C++ a class can have multiple constructors, but only one destructor. Explain why this is the case using code to highlight why.
What problems might Robert encounter in comparing these companies to one another on the basis of their​ ratios?
  FIN101 Robert Arias recently inherited a stock portfolio from his uncle. Wishing to learn more about the companies in which he is now​ invested, Robert performs a ratio analysis on each one and decides to compare them to each other. Some of his ratios are listed here:   Island Burger Fink Roland   Ratio Electric Utility Heaven Software Motors Current ratio 1.06 1.35 6.79 4.55 Quick ratio 0.92 0.87 5.23 3.73 Debt ratio 0.69 0.45 0.04 0.34 Net profit...
CRITICAL THINKING: 1) What is venipuncture? 2) What are some problems that medical assistants may encounter...
CRITICAL THINKING: 1) What is venipuncture? 2) What are some problems that medical assistants may encounter when attempting to draw blood from patients – and particularly from elderly patients? 3) Susan, a medical assistant, is having trouble finding a venipuncture site on the left arm of her patient, Gabel, although Susan has thoroughly assessed and palpated the antecubital veins, including the bigger median cubital vein. Gabel doesn’t want Susan to draw blood from her right arm. What techniques are available...
1. What are some of the common problems users encounter when trying to compare financial statements...
1. What are some of the common problems users encounter when trying to compare financial statements of companies (even when they are in the same industry)? Describe how using XBRL could help alleviate these problems. 2. What are the current XBRL filing requirements for publicly-listed firms in the US? 3. Why should a business create XBRL-enabled financial reports? 4. Does the FDIC require banks to file any report(s) using XBRL? If so, which reports? 5. Do you have experience filing...
1.What are some of the common problems users encounter when trying to compare financial statements of...
1.What are some of the common problems users encounter when trying to compare financial statements of companies (even when they are in the same industry)? Describe how using XBRL could help alleviate these problems. 2. What are the current XBRL filing requirements for publicly-listed firms in the US? 3. Why should a business create XBRL-enabled financial reports? 4. Does the FDIC require banks to file any report(s) using XBRL? If so, which reports?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT