Thursday, 21 September 2017

New and delete operator in C++

/*
 New and delete operator in C++
*/

#include "iostream"
using namespace std;

int main(){

 int *p=new int;
 *p = 10;
 cout<<"\n*p: "<<*p;

 float *q = new float;
 *q = 20.05;
 cout<<"\n*q: "<<*q;
 int n=3;
 cout<<endl;
 int *r = new int[n] ;
 for(int i=0; i<n; i++){
  q[i] = i+1;
  cout<<q[i]<<endl;
 }
}
/*
 OUTPUT
[pavan@localhost Unit-2-Inheritance]$ g++ NewDelete.cpp
[pavan@localhost Unit-2-Inheritance]$ ./a.out 

*p: 10
*q: 20.05
1
2
3
[pavan@localhost Unit-2-Inheritance]$ 
*/

No comments: