Advance Interview Ask In C++ Interview #3
How to delete array of objects in C++? Proof by C++ code for proper deletion Answer includes how to delete array of objects in C++ created dynamically with C++ code example with proof. In other words, delete array of pointers to objects in c++. Interview Question: Write C++ code to create an array of objects using new keyword and delete these objects. Also, proof that all the objects are deleted properly. Answer: We know that when we create an object of a class dynamically from heap memory using new keyword, then we must delete it explicitly to avoid memory leaks in C++ programs after we are done with its operations. Let’s take below class Pen as an example. class Pen { public: Pen() { cout << "Constructor..." <<endl; } ~Pen() { cout << "Destructor...!" <<endl; } void write(){ cout << "Writing...!" <<endl; } }; Below is an example, how we create an object ...