dynamic memory allocation and reuse

Can any one plz provide some example how to implement dynamic memory allocation for employee record in the below example and how do i delete record from multiple records and how do i use this deleted memory again to reallocate other value.

the definition is to create employee class and provide member of class like eid,fname,lname age,etc and for name use dynamic memory allocation and when any employee left then memory would be recaptured and display total no. employee record with detail.

program is not working correctly!

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <cstring>
using namespace std;

class employee
{
	private:
		int *eid;
		char *fname;
		char *lname;
		int *age;
		int count;
	public:
		employee()
		{
			get_count(count);
		}
		void get_count(int count);
		int get_count();
		void get_employee();
		void display();
};

void employee :: get_count(int count)
{
	count++;
}

int employee :: get_count()
{
	cout<<"Total Number of employees:"<<count<<endl;
	return 0;
}

void employee :: get_employee()
{
	eid = new int[20];
	fname = new char[50];
	lname = new char[50];
	age = new int[20];
	cout<<"Enter Employee details"<<endl;
	for(int i=0;i<2;i++)
	{
		cout<<"Empid:";
		cin>>eid[i];
		cout<<"Fname:";
		cin>>fname[i];
		cout<<"Lname";
		cin>>lname[i];
		cout<<"Age:";
		cin>>age[i];
	}
}

void employee :: display()
{
	cout<<"Total Employee Record:"<<endl;
	for(int i=0;i<2;i++)
	{
		cout<<"Empid"<<eid[i]<<endl;
		cout<<"Fname"<<fname[i]<<endl;
		cout<<"Lname"<<lname[i]<<endl;
		cout<<"Age"<<age[i]<<endl;
	}
}

int main(int argc, char **argv)
{
	employee e1,e2;
	e1.get_employee();
	
	e1.display();
	
	return 0;
}
i don't read the whole code, but, from my opinion, maybe you should allocate new object of the class:
 
employee * e_p = new employee;


CMIIW
Well, first off you don't declare a destructor for your class. While normally you don't need to do this, if you are creating memory on the fly its a good idea to delete it when you are done, other wise you'll get whats called a "memory leak". so within the public section of your class, declare a destructor which deletes any memory allocated by new:
1
2
3
4
5
6
7
8
9
10
11
~employee()
{
if(eid)
   delete eic;
if(fname)
   delete fname;
if(lname)
   delete lname;
if(age)
   delete age;
};


Secondly i'm not sure about your design. You are trying to mix two design patterns into one:

Container or manager,
object

The Manager class holds all employees, is responsible for adding new employees and removing them from the employee database. It also keeps a record of the number of employees that it currently holds.

the Employee class holds all information that is relevant for each employee.

All interaction is done through the Manager class and not through the employee class.
Topic archived. No new replies allowed.