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
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...
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...
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...
Data Structures using C++ Consider the following class #ifndef LINKEDQUEUETYPE_H #define LINKEDQUEUETYPE_H #include <iostream> #include <new>...
Data Structures using C++ Consider the following class #ifndef LINKEDQUEUETYPE_H #define LINKEDQUEUETYPE_H #include <iostream> #include <new>    #include <cstdlib> #include "QueueADT.h" using namespace std; // Definition of the node template <class ItemType> struct NodeType {        ItemType info;        NodeType<ItemType> *next; }; template <class ItemType> class LinkedQueueType: public QueueADT<ItemType> { public:        // Constructor        LinkedQueueType();           // Default constructor.           // Post: An empty queue has been created. queueFront = NULL;           //       queueBack = NULL;...
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...
Data Structures using C++ Consider the classes QueueADT and ArrayQueueType: QueueADT: #ifndef QUEUEADT_H #define QUEUEADT_H template...
Data Structures using C++ Consider the classes QueueADT and ArrayQueueType: QueueADT: #ifndef QUEUEADT_H #define QUEUEADT_H template <class ItemType> class QueueADT { public:        // Action responsibilities        virtual void resetQueue() = 0;           // Reset the queue to an empty queue.           // Post: Queue is empty.        virtual void add(const ItemType& newItem) = 0;           // Function to add newItem to the queue.           // Pre: The queue exists and is not full.          ...
Complete this in C++ and explain what is being done. 1      Introduction The functions in the...
Complete this in C++ and explain what is being done. 1      Introduction The functions in the following subsections can all go in one big file called pointerpractice.cpp. 1.1     Basics Write a function, int square 1(int∗ p), that takes a pointer to an int and returns the square of the int that it points to. Write a function, void square 2(int∗ p), that takes a pointer to an int and replaces that int (the one pointed to by p) with its...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT