Showing posts with label Pointer. Show all posts
Showing posts with label Pointer. 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;
}

Program to Demonstrate Pointers in C++


#include <iostream.h>
#include <conio.h>
class cl
{
      public:
      cl(int i)
      {
            val=i;
      }
     
      int val;
     
      int double_val()
      {
            return val+val;
      }
};

int main()
{
      clrscr();
      int cl::*data; // data member pointer
      int (cl::*func)(); // function member pointer
      cl ob1(1), ob2(2); // create objects
      cout<<sizeof(ob1)<<endl;
      cl *p1, *p2;
      p1 = &ob1; // access objects through a pointer
      p2 = &ob2;
      data = &cl::val; // get offset of val
      func = &cl::double_val; // get offset of double_val()
      cout << "Here are values: ";
      cout << p1->*data << " " << p2->*data << "\n";
      cout << "Here they are doubled: ";
      cout << (p1->*func)() << " ";
      cout << (p2->*func)() << "\n";
      getch();
      return 0;
}

Pointer # 2


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

class cl
{
      public:
      cl(int i)
      {
            val=i;
      }
      int val;
      int double_val()
      {
            return val+val;
      }
};

int main()
{
      int cl::*data; // data member pointer
      int (cl::*func)(); // function member pointer
      cl ob1(1), ob2(2); // create objects
      data = &cl::val; // get offset of val
      func = &cl::double_val; // get offset of double_val()
      cout << "Here are values: ";
      cout << ob1.*data << " " << ob2.*data << "\n";
      cout << "Here they are doubled: ";
      cout << (ob1.*func)() << " ";
      cout << (ob2.*func)() << "\n";
      getch();
      return 0;
}

Array Basic Program in C++


An Array refers to a collection of elements of similar data-types. This is particularly useful when the number of variables needed is in a big number. If we were to create variable names for every different variable then very soon we would run out of logical names to represent them. But Arrays make our work easier in this case. 

Arrays have the property that they carry the same name for a group of different distinct variables but all the variables have a different index to them. This index distinguishes each variable from any other variable. Also, the variables make the referencing of these elements very easy. 

When used in loops, these Arrays can prove to be a huge time saver. The loop variable can be used to index the Array inside the loop to give powerful control over the operations that have to be performed on the whole array.

Pointer Basic Program in C++

Pointer are used to reference/point to other variables. Their value can be set and read and they basically refer to a variable's memory location. If we are referencing to an Integer, we call it a Pointer to an Integer. Pointers are very useful as they contain critical information about a variable i.e. its address in the memory pool. The value of the Pointer itself is the address of the variable. Sometimes, Pointers are also called as Address Variables. 













Declaring Pointers

Pointers can be declared by using the * operator. We can declare a Pointer to point to different data types by specifying it in the declaration. See below how to create a Pointer to an Integer:

int *Z ;
Custom Search