Monday, 1 June 2015

Using New And Delete Operator In C++

/* New and delete */
#include <iostream.h>
#include<conio.h>
class item
{
int ic,qty;
float up, amt;
char nm [10];
public:
void getdata ()
{
cout<<"Enter item code";
cin>>ic;
cout<<"Enter item name";
cin>>nm;
cout<<"Enter unit price and quantity";
cin>>up>>qty;
}
    void compute ()
    {
amt=up*qty;
    }
void putdata()
{
cout<<"\nItem data";
cout<<"\nItem name="<<nm;
cout<<"\nItem code="<<ic;
cout<<"\nItem amount="<<amt;
}
};
void main()
{
clrscr();
item * optr;
char ch;
while (1)
{
optr=new item;
optr->getdata();
optr->compute();
optr->putdata();
cout<<"\nWant to enter model detail press y";
cin>>ch;
if(ch=='y'||ch=='Y')
continue;
else
break;
}
delete optr;
getch();
}

Output:

Enter item code=101
Enter item name=Cheetos
Enter unit price and quantity=30 5

Item data
Item name=Cheetos
Item code=101
Item amount=150
Want to enter model detail press y

0 comments:

Post a Comment