Question

c++ 1. If a class having dynamically allocated members didn't define its own destructor, what possible...

c++

1. If a class having dynamically allocated members didn't define its own destructor, what possible problem(s) could arise?

2. If a class having dynamically allocated members didn't define its own copy constructor, what possible problem(s) could arise?

3. If a class having dynamically allocated members didn't define its own assignment operator, what possible problem(s) could arise?

Homework Answers

Answer #1

Constructor:

In object-oriented programming(C++), a constructor is a special method of the class that has the same name as the class name and it initializes the data member at object creation time.

Destructor:

When an object goes out of scope then the destructor method is called automatically and this method deletes the object by releasing the memory.

1.

If a class having dynamically allocated members didn't define its own destructor then the compiler will define it implicitly.  

For example:

#include <iostream>

using namespace std;

class ABC
{
int a;
public:
  
//default constructor
ABC()
{
a = 0;
}
  
//parameterize constructor
ABC(int x)
{
a = x;
}
};

int main()
{
ABC();

ABC a;
  
//call destructor
a.~ABC();
return 0;
}

In the above program, the constructor is defined but destructor is not defined but this program will execute successfully.

2.

If a class having dynamically allocated members didn't define its own copy constructor, then the compiler will define it implicitly.

A copy constructor is a special type of constructor that creates an object and initializes it with another object of the same class.

But, we must define a copy constructor if the class is having the pointer variable and has dynamic memory allocation.

The default copy constructor does only the shallow copy but the defined copy constructor can make a deep copy.

3.

If a class having dynamically allocated members didn't define its own assignment operator then the compiler will define it implicitly.

For example:

#include <iostream>

using namespace std;

class ABC
{
int a;
public:
  
//default constructor
ABC()
{
a = 0;
}
  
//parameterize constructor
ABC(int x)
{
a = x;
}
};

int main()
{
//create object
ABC a(10);
ABC b;
  
b = a;
  
return 0;
}

In the above program, the assignment operator is not defined but it will work correctly.

Both of the objects will point to different memory locations because only values are copied.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
You are asked to implement a C++ class to model a sorted array of unsigned integers....
You are asked to implement a C++ class to model a sorted array of unsigned integers. The class is to be used in an embedded application that cannot assume the presence of the STL. The array has to be dynamically allocated in such a way that allows programmers using it to specify the required size. Your class should should: (1) provide the appropriate constructors and destructor; (2) provide methods for updating, and showing numbers in/to the array (e.g., to be...
c++ C++ CLASSES and objects DO ADD COMMENTS DISPLAY OUTPUT First make three files: episode.cpp, episode.h...
c++ C++ CLASSES and objects DO ADD COMMENTS DISPLAY OUTPUT First make three files: episode.cpp, episode.h andRun.cpp to separate class header and implementation. In this program, we are going to create a small scale Telivision Management System. A) Create a class Episode with following variables: char* episode_name, char* episode_venue, char episode_date[22] and char episode_time[18]. Input format for episode_date: dd-mm-yyyy Input format for episode_time: hh:mm am/pm B) Implement default constructor and overloaded constructor. Print “Default Constructor Called” and “Overloaded Constructor Called”...
1.Select all C++ functions that must be defined for implementing deep copies in your class which...
1.Select all C++ functions that must be defined for implementing deep copies in your class which has instance variables pointing to dynamic memories. Group of answer choices a.Default Constructor b.Copy Constructor c.Overloading assignment operator d.Writing own friend functions to directly access dynamically allocated memories e.Writing own destructor to free the allocated memory f.Overloading new operator 2. Match the memory source (right side) of the variables (left side) of the following code snippet which is a part of 'myProg' program. Note...
IntNode class I am providing the IntNode class you are required to use. Place this class...
IntNode class I am providing the IntNode class you are required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined inline (within the class declaration). Do not write any other functions for the IntNode class. Use as is. struct IntNode { int data; IntNode *next;...
1. Two groups are having competition for chase. If one plays it in its own campus...
1. Two groups are having competition for chase. If one plays it in its own campus it will win with probability P> ½. They will play three times, two in group 1’s campus and one in group 2’s campus. If one wins game 1 and 2, then game 3 is not going to be played. a) What is P[T], the probability that the group 1 wins the series? b) If they play only once will group 1 have higher chance...
1. Two groups are having competition for chase. If one plays it in its own campus...
1. Two groups are having competition for chase. If one plays it in its own campus it will win with probability P> ½. They will play three times, two in group 1’s campus and one in group 2’s campus. If one wins game 1 and 2, then game 3 is not going to be played. a) What is P[T], the probability that the group 1 wins the series? b) If they play only once will group 1 have higher chance...
1.Two groups are having competition for chase. If one plays it in its own campus it...
1.Two groups are having competition for chase. If one plays it in its own campus it will win with probability P> ½. They will play three times, two in group 1’s campus and one in group 2’s campus. If one wins game 1 and 2, then game 3 is not going to be played. a) What is P[T], the probability that the group 1 wins the series? b) If they play only once will group 1 have higher chance of...
How to use C++ figure this question? Collection and Sorted Collection An array is great for...
How to use C++ figure this question? Collection and Sorted Collection An array is great for storing a list of values. However, one challenge when working with arrays is they have a fixed size. If you declare an array of size 10 and later need to actually store 11 elements you have to create a new array and copy everything over. C++ (and other langauges) create classes that handle this and other operations behind the scenes. In this assignment, we...
Two groups are having competition for chase. If one plays it in its own campus it...
Two groups are having competition for chase. If one plays it in its own campus it will win with probability P> ½. They will play three times, two in group 1’s campus and one in group 2’s campus. If one wins game 1 and 2, then game 3 is not going to be played. a) What is P[T], the probability that the group 1 wins the series? b) If they play only once will group 1 have higher chance of...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private data member for the triplet is a generic array with three elements. The triplet ADT has the following functions:  default constructor  explicit constructor: initialize the data member using parameters  three accessors (three get functions) which will return the value of each individual element of the array data member  one mutator (set function) which will assign values to the data member...