Dynamic Memory Allocation: In it,memory of variable is allocated in heap and used for run time.
The required memory are located in randomly position.new operator is used to allocate memory dynamically. And delete operator is used to deallocate the memory occupied by new operator..The size of memory is allocated from minimum to maximum.
syntax: datatype * pointer variable= new datatype;
Example:
1. Program to add two number using dynamic memory allocation.
#include <iostream.h>
#include <conio.h>
void main()
{
int *ptr1=new int;
int *ptr2=new int;
int sum;
cout<<"Enter value of first number:";
cin>>*ptr1;
cout<<"Enter value of second number:";
cin>>*ptr2;
sum=*ptr1+*ptr2;
cout<<"Sum is:"<<sum;
delete ptr1;
delete ptr2;
getch();
}
Output:
2. Program to calculate volume of rectangular box.
#include <iostream.h>
#include <conio.h>
void main()
{
int *ptrl=new int;
int *ptrb=new int;
int *ptrh=new int;
int volume;
cout<<"Enter value of length:";
cin>>*ptrl;
cout<<"Enter value of breadth:";
cin>>*ptrb;
cout<<"Enter value of height:";
cin>>*ptrh;
volume=*ptrl**ptrb**ptrh;
cout<<"Volume is:"<<volume;
delete ptrl;
delete ptrb;
delete ptrh;
getch();
}
3. Program to calculate area of circle dynamically.
#include <iostream.h>
#include <conio.h>
#include <math.h>
void main()
{
float *ptr=new float;
float Area;
cout<<"Enter radius of circle:";
cin>>*ptr;
Area=3.14*pow(*ptr,2);
cout<<"Area is:"<<Area;
delete ptr;
getch();
}
Output:
Dynamic memory allocation for array.
syntax: datatype *pointer variable=new datatype[size];
eg int *ptr = new int[20];
4. Program to calculate area of 3 circle dynamically.
#include <iostream.h>
#include <conio.h>
#include <math.h>
void main()
{
int i;
float Area;
float *ptr=new float [3];
for(i=0;i<3;i++)
{
cout<<"Enter the radius:";
cin>>*(ptr+i);
Area=3.14*pow(*(ptr+i),2);
cout<<"Area of circle:"<<Area<<endl;
}
delete[]ptr;
getch();
}
Output:
Dynamic Memory allocation for object
syntax: class Name *pointername = new className ;
5. Program to input name ,age and display by creating class called Student dynamically.
#include <iostream.h>
#include <conio.h>
class Student
{
private:char name[50];
int age;
public: void input();
void display();
};
void Student::input()
{
cout<<"Enter name:";
cin>>name;
cout<<"Enter age:";
cin>>age;
}
void Student::display()
{
cout<<endl;
cout<<"Name:"<<name<<endl<<"Age:"<<age;
}
void main()
{
Student *ptr=new Student;
cout<<endl;
ptr->input();
ptr->display();
delete ptr;
getch();
}
Output:
^
Dynamic Memory Allocation for Array Object
syntax: class name *pointername = new class name[size]
eg. class Student *ptr = new Student[10];
6. Program to input and display the class Student that have name and age for 2 students dynamically.
#include <iostream.h>
#include <conio.h>
class Student
{
char name[50];
int roll;
public:void input();
void display();
};
void Student::input()
{
cout<<"Enter name:";
cin>>name;
cout<<"Enter roll:";
cin>>roll;
}
void Student::display()
{
cout<<"Name:"<<name<<endl<<"Roll:"<<roll;
}
void main()
{
int i;
cout<<endl;
Student *s=new Student[2];
cout<<"Enter Details"<<endl;
for(i=0;i<2;i++)
{
cout<<endl;
(s+i)->input();
}
cout<<endl;
cout<<"Details are"<<endl;;
for(i=0;i<2;i++)
{
cout<<endl;
(s+i)->display();
}
delete []s;
getch();
}
7. Program to input and display the class Student that have name,age.and provide the subject marks and check whether the student is passed or failed dynamically.
#include <iostream.h>
#include <conio.h>
class Student
{
char name[50];
int roll;
int phy;
int math;
public:void input();
void display();
void result();
};
void Student::input()
{
cout<<"Enter name:";
cin>>name;
cout<<"Enter roll:";
cin>>roll;
cout<<"Enter marks of physics:";
cin>>phy;
cout<<"Enter mark of maths:";
cin>>math;
}
void Student::display()
{
cout<<"Name:"<<name<<endl;
cout<<"Roll:"<<roll<<endl;
cout<<"Physics:"<<phy<<endl;
cout<<"Math:"<<math<<endl;
}
void Student::result()
{
int tot;
tot=(phy+math)/2;
if(tot<=45)
{
cout<<"*********************";
cout<<"You are failed";
}
else
{
cout<<"*******************";
cout<<"You are passed";
}
}
void main()
{
int i;
cout<<endl;
Student *s=new Student[2];
cout<<"Enter Details"<<endl;
for(i=0;i<2;i++)
{
cout<<endl;
(s+i)->input();
}
cout<<"*******************************"<<endl;
cout<<"Details are"<<endl;;
for(i=0;i<2;i++)
{
cout<<endl;
(s+i)->display();
cout<<"**********************************"<<endl;
(s+i)->result();
}
delete []s;
getch();
}
Output:
Dynamic Memory Allocation using Constructor
DMA Constructor: It is used to a llocate the memory to objects dynamically using new or delte operator with help of constructuor
8 Program to initialize the value of x and y.
#include <iostream.h>
#include <conio.h>
class Test
{
int x,y;
public :
Test()
{
x=0;
y=0;
}
Test(int a,int b)
{
x=a;
y=b;
}
void display()
{
cout<<"x:"<<x<<endl<<"Y:"<<y<<endl;
}
~Test()
{
cout<<endl;
cout<<"Destructor is called";
}
};
void main()
{
Test *ptr1=new Test;
Test *ptr2=new Test(10,20);
ptr1->display();
ptr2->display();
delete ptr1;
delete ptr2;
getch();
}
Output:
9. Program to initialize the value of x and y passing reference variable.
#include <iostream.h>
#include <conio.h>
class Test
{
int *x,*y;
public :
Test()
{
x=new int;
y=new int;
*x=0;
*y=0;
}
Test(int a,int b)
{
x=new int;
y=new int;
*x=a;
*y=b;
}
void display()
{
cout<<"x:"<<*x<<endl<<"Y:"<<*y<<endl;
}
~Test()
{
cout<<"Destructor is called";
delete x;
delete y;
}
};
void main()
{
Test *ptr1=new Test;
Test *ptr2=new Test(10,20);
ptr1->display();
ptr2->display();
delete ptr1;
delete ptr2;
getch();
}
10. DMA program with constructor
#include <iostream.h>
#include <conio.h>
class Test
{
int *x,*y;
public :
Test()
{
x=new int;
y=new int;
*x=0;
*y=0;
}
Test(int a,int b)
{
x=new int;
y=new int;
*x=a;
*y=b;
}
void display()
{
cout<<"x:"<<*x<<endl<<"Y:"<<*y<<endl;
}
~Test()
{
cout<<"destrutor is called";
delete x;
delete y;
}
};
void main()
{
Test *ptr1=new Test;
Test *ptr2=new Test(10,20);
ptr1->display();
ptr2->display();
getch(); }
Comments
Post a Comment