Showing posts with label New. Show all posts
Showing posts with label New. Show all posts

New & Delete Example in C++ Program


This C++ Program shows you how to create new objects via the new keyword. In the program below, we create an integer pointer variable named *p. The value of the integer is initialized to 87. Then we have printed the value of p through the cout operator. This is the basic procedure for creating new objects from classes. 


The deletion of previously created objects can be done with the help of delete operator. Simply write delete followed by the variable name to delete the variable.This process of deleting previously created is a good practice as it frees up memory resources from the computer. Also, if a huge number of Objects are left untreated after their usage, their memory piles up to form wasted memory resources which can significantly slow down the speed of the computer. 

Use New & Delete Keywords in C++


#include <iostream.h>
#include <conio.h>

int main()
{
      clrscr();

      int *p;
     
      p = new int; // allocate space for an int
     
      *p = 100;
     
      cout << "At " << p << " ";
     
      cout <<     "is the value " << *p << "\n";
     
      delete p;
     
      getch();
     
      return 0;
}
Custom Search