 Hello friends, I am Sanjay Gupta. In this video I am going to demonstrate you how you can implement dynamic 1D array using constructor and destructor in C++. Before starting, you can note my information. You can connect with me by following these details. You can download my app Techimus that is based on programming languages available on Google Play. You can search my channel on YouTube by typing my name. My channel will be available there. You can open my channel. You can subscribe my channel by clicking on this button. Here various programming related videos and playlists are available. Now coming on to the topic. In this video I am going to demonstrate you the implementation of dynamic 1D array using constructor and destructor. First of all, I am including a header file iostream. Then I am using a namespace std. Now I am implementing class vector. Dynamic array is known as vector. That's why I am using this name. Here I am declaring a pointer and another variable size. Then in public section, first I am going to implement constructor. Inside this constructor, I am asking size from the user using this cout statement. Then size will be received inside size variable using cn. Then through new, I am going to allocate memory and allocated memory blocks. Beginning address or base address will be assigned into p. So this is dynamic memory allocation and this is constructor. Here constructor is allocated dynamic memory. Now for the allocation, I am implementing destructor. Inside destructor, I am writing a statement delete p. This statement is responsible for dynamic memory deallocation and this is destructor. So this way I have implemented constructor and destructor for memory allocation and deallocation. Now I am implementing declarations of two functions that are read and print. Now I am terminating the class. After termination of class, I am going to define read and print functions. So void is written type. Then vector is name of class. Then first read function. Inside this function, I am declaring a variable i. Then I am implementing for loop starting from 0 to size. Inside this loop, I am receiving values inside p of i using cn. So this is read function definition. Now similar to the I am implementing print function definition. Again, I am implementing a loop starting from 0 to size. This time instead of cn, I am using cout. And it will change the line every time whenever an element will be displayed. So these two functions are defined. Now I am going to implement main function. Inside main function, I am creating object of vector class that is v. So friends, here constructor will be called automatically. So this is the benefit of constructor as you have created object of the class. Constructor will be called automatically. Now you can call read function. Before that you can show a message. Enter elements. Then I am showing a message you have entered. Then I am calling print function and then return 0. You can see I have not called destructor because it is called automatically whenever an object goes out of scope. Instead of implementing functions for dynamic memory allocation and deallocation, I have implemented constructor and destructor. So this is complete example which is implementing constructor for dynamic memory allocation, destructor for dynamic memory deallocation. Read function is for reading all the elements and print function is for printing all the elements available in an array. Then main function here object is created which will call constructor automatically. Then read function will be called. Then print function will be called. And after termination of main destructor will be called and all allocated memory will be destroyed automatically. Now I am going to execute this code. You can see it is asking for size. So I am entering 5. Then it is asking for elements. I am entering 1, 5 times. You can see it is showing you have entered 1, 1, 1, 1, 1. So it is displaying 1, 5 times. So friends, I hope you have understood how you can implement dynamic memory allocation and dynamic memory deallocation using constructor and destructor in C++. If you want to watch more programming related videos, you can search my channel on YouTube by my name Sanjay Gupta. Here various programming language related videos are available. Thank you for watching this video.