Professional Documents
Culture Documents
delete operator :
To destroy the variable space which has been created by using the new dynamically
syntax :
delete pointer; - single variable ex : delete ptr; delete []pointer; - for an array delete []ptr;
Create a memory for 10 integers using NEW operator and destroy using the delete operator.
#include<iostream.h> void main() { int *ptr,i; ptr=new int[5]; cout<<endl<<"Enter 5 numbers"; for(i=0;i<5;i++) { cin>>*(ptr+i); } for(i=0;i<5;i++) { cout<<*(ptr+i)<<endl; } delete []ptr; }
cout<<endl<<"enter the data"; cin>>bno>>author>>bname; } void book::show() { cout<<endl; cout<<bno<<author<<bname; } void main() { book b1,*b2; b2=new book; b1.get(); b2->get(); b1.show(); b2->show(); delete b2;
-> Used to access the member of the class through pointer variable
~String() { delete str; } void display() { cout<<str; cout<<"\n Destructor called"; } }; void main() { String s1="Lafore"; s1.display(); cout<<length; }
OUTPUT
It is a special pointer that exists for a class while a non-static Member function is executing. It is used to access the address of the Class itself and may return data items to the caller.
#include<iostream.h> class rectangle void main() { { private : rectangle r; int length,breadth; r.set_data(10,20); public : r.show(); void set_data(int,int); } void show(); }; void rectangle::set_data(int l,int b) { 10 this->length=l; OUTPUT this->breadth=b; } void rectangle ::show() { cout<<this->length<<this->breadth ; }
20