-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamic_memory_allocation.cpp
49 lines (39 loc) · 1.27 KB
/
dynamic_memory_allocation.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// #run-time or dynamic memory allocation
/*
-> In the Dynamic memory allocation, variables get allocated
only if your program unit gets active.
-> Dynamic Memory Allocation is done during program execution.
-> It uses heap for managing the dynamic allocation of memory.
-> variable memory size, memory reusability possible.
-> In this memory is allocated at run time and can be released at any time during the program.
-> Destructor is called manually.
-> arrow operator is used to reference pointers and memory address.
**/
#include<iostream>
using namespace std;
class Person{
private:
int age; // private variable
public:
string name; // public variable
int getAge() // getter function
{
return age;
}
void setAge(int age) // setter function
{
this->age = age;
}
~Person(){
cout<<"Destructor"; // called manually
}
};
int main(){
Person* ramesh = new Person(); // dynamic memory allocation
ramesh->name = "Ramesh singh";
ramesh->setAge(30);
cout<<"Name:"<<ramesh->name<<endl; // accessing name using arrow(->) operator
cout<<"Age:"<<ramesh->getAge(); //accessing getter for age using arrow(->) operator
delete ramesh;
return 0;
}