Sample Class to Avoid Memory Leaks

Learn about the destructors in the C++ language.

We'll cover the following

Problem

Write a program that implements a Sample class that has an int pointer and a float pointer as its private data members. While creating objects of this class, in the constructor, allocate space for the integer and the float and set them up with some values. Ensure that the objects are destroyed systematically.

Sample run

Here’s what you should see when you run the program.

10      5.5
20      2.4
Memory pointed to by p and q is deleted
Memory pointed to by p and q is deleted

Coding solution

Here is a solution to the problem above.

Get hands-on with 1200+ tech skills courses.