Tuesday, 24 February 2015

New and Delete Operator in C++

/*

Dynamic Memory Allocation
===========================

new - New Memory Allocation
delete - Memory Deallocation

*/

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

class Date
{
private:
int dd,mm,yy;
public:
void setDate(int a,int b,int c)
{
dd=a;
mm=b;
yy=c;
}
void Display()
{
cout<<"\n ToDay Date is\n";
cout<<dd<<"\\"<<mm<<"\\"<<yy;
}
};
void main()
{
clrscr();

Date *d=new Date;
d->setDate(21,2,2005);  // ->  - Member Access Operator
d->Display();
delete d;

Date *d1=new Date;
d1->setDate(22,2,2005);
d1->Display();
delete d1;

getch();
}

No comments:

Post a Comment