Class Program

I am new to the C++ so here i have one problem. I am not able to understand what i have to do in the below problem so please help me.


using class Employee, to create a database for the details of the
employee are entered at the time of joining like firstname, lastname, age, etc... For
names, use dynamic allocation of memory and also when they are leaving, this memory
should be recaptured. The program should return the total number of employees and their
details.
Last edited on
1) Start by making a class called Employee.

2) Then give that class members for firstname, lastname, age, etc.

3) For the names, use dynamically allocated memory

...etc

(if you didn't notice, I'm just reading the paragraph and interpretting each sentence as an individual step in the process. It really doesn't get much simpler than that... the assignment is telling you exactly what to do and how to do it.)

If a particular step is giving you trouble, post the step and why it's confusing you. Show us what you've done so far.
Below is the code but it is not working fine and also i want to know that HOW to implement "For
names, use dynamic allocation of memory and also when they are leaving, this memory
should be recaptured. The program should return the total number of employees and their
details." thing.? and HOW do i delete one employee and redisplay the record.
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
77
#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;
}

Dynamic memory allocation:


employee *pEmployee = new employee;


It creates a pointer to the new object using the new operator. I wouldn't bother using new for basic types. instead of char arrays, just use string - need to #include <string> and do one of these:

1. put std:: before each item that is in the std namespace - this is better than using namespace std;
2. put using std::cout; using std::cin; std::string; etc for each thing that is in std namespace, after the #includes then you can use cout, cin, string without qualification.


Each obj created with new should have a corresponding delete, otherwise a memory leak.

I always name my classes with a leading 'C' & capitalise leading letters of words as in CEmployee - just a reminder that it is a class.

There seems to be confusion about set & get functions - you a mixing the 2 together.


Edit:

This bit is bizarre:

1
2
3
4
5
6
7
8
9
10
11
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];
	}

Just use strings and assign them to the member variables. Why are you setting the first 2 chars only?



Last edited on
Can any one plz provide some example how to implement dynamic memory allocation for employee record above example and how do i delete record from multiple records and how do i use this deleted memory again to reallocate other value.
1
2
fname = new char[50];
lname = new char[50];


This code doesn't do what you think.

char[50] is an array of 50 chars - in other words a string that is 50 chars long. So just use string fname = ""; to declare instead. Then you would have an array of the strings (or use a <vector>)

The new operator returns a pointer, so fname & lname are pointers.

When the assignment asked for dynamic memory allocation, they meant for the employee objects, not for the member variables.

There you go, I have given some clues, change you code then post it again if you have any problems
Topic archived. No new replies allowed.