+ 1

Can anybody explain me why I need to delete the pointer of object in this?? this was shown in most of the website I was seeking

#include <iostream> #include <string> using namespace std; class Employee { public: Employee(string n, double s, long int i, string p, string c) :name(n), salary(s), id(i), post(p), company(c) {}; void getInfo() { cout << "Name: " << name << endl; cout << "Salary: " << salary << endl; cout << "ID: " << id << endl; cout << "Post: " << post << endl; cout << "Company: " << company << endl; } private: string name; double salary; long int id; string post; string company; }; int main() { Employee *e1 = new Employee("Rocky", 120000000000000000000000.00, 8055, "CEO", "INDIA"); e1->getInfo(); // here //can anybody explain me why i should delete this // it was recommended but why; delete e1; return 0; }

16th May 2022, 3:22 PM
Cyber Eagle
Cyber Eagle - avatar
2 Answers
0
To use better memory management and to avoid any memory leaks.. hope these helps.. https://www.programiz.com/cpp-programming/memory-management https://en.m.wikipedia.org/wiki/Memory_leak
16th May 2022, 4:14 PM
Jayakrishna 🇼🇳
0
To free memory... I think
16th May 2022, 4:09 PM
Mihir Lalwani
Mihir Lalwani - avatar