What is the need of a destructor? Explain with the help of an example.



What is the need of a destructor? Explain with the help of an example...

Answer / Sangh Priya Rahul

A destructor is needed to clean up any resources (like memory, files, network connections etc.) that an object acquired during its lifetime. An example would be deleting dynamically allocated memory in C++ when an object is destroyed or goes out of scope. For instance, consider a class `MyClass` with a pointer member `data`. The destructor can be used to free this memory:
```cpp
MyClass::~MyClass() {
delete[] data;
}
```

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C++ General Interview Questions

What is abstraction in c++?

2 Answers  


What is the use of this pointer in c++?

1 Answers  


wrong statement about c++ a)code removably b)encapsulation of data and code c)program easy maintenance d)program runs faster

11 Answers  


What are the differences between new and malloc?

1 Answers  


Array base access faster or pointer base access is faster?

1 Answers  


When is a template better solution than a base class??

2 Answers   emc2,


What is the operator in c++?

1 Answers  


What are advantages of c++?

1 Answers  


Are there any new intrinsic (built-in) data types?

1 Answers  


There are 100 students in a class. The management keep information in two tables. Those two tables are given like Roll no Name Age 001 ABC 15 002 XYZ 14 and Roll No Subject Marks 001 Math 75 001 Physics 55 002 Math 68 001 Hindi 69 They want the information like this Roll No Name Hindi Physics Math Total 001 ABC 69 55 75 199 002 XYZ 68 74 84 226 And Roll No Suject Highest 001 Math 98 007 Physics 84 021 Hindi 74 All 275 All information is kept in structure in main memory. You have to find last two tables.

1 Answers  


What is stoi in c++?

1 Answers  


Differentiate between C and C++.

1 Answers   Wipro,


Categories