dynamic object

Here my goal is to enter employee record and then display it.Delete specific record then display again all the records using dynamic memory management.

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
78
79
80
81
82
83
#include <iostream>
using namespace std;

class Employee{
	private:
		string name;
		int age;
		double salary;
		string dept;
	public:
		Employee();
		//~Employee();
		void create_emp();
		void show_emp();
		void delete_emp(Employee *e);
};

Employee :: Employee()
{
	name = "";
	age = 0;
	salary = 0.0;
	dept = "";
}

/*Employee :: ~Employee()
{
	delete emp;
}
*/
void Employee :: create_emp()
{
	cout<<"Employee record"<<endl;
	cout<<"Employee Name:";
	cin>>name;
	cout<<"\nEmployee Age:";
	cin>>age;
	cout<<"\nEmployee Salary:";
	cin>>salary;
	cout<<"\nEmployee Department:";
	cin>>dept;
}
void Employee :: delete_emp(Employee *e)
{
	delete[] e;
}
void Employee :: show_emp()
{
	cout<<"Display Employee Record";
	cout<<"Employee Name:"<<name;
	cout<<"\nEmployee Age:"<<age;
	cout<<"\nEmployee Salary:"<<salary;
	cout<<"\nEmployee Department:"<<dept;
}

int main(int argc, char **argv)
{
	int size=0;
	Employee *emp = new Employee[size+1];
	int ch;
	cout<<"\nEnter Choice\n";
	do{
		cout<<"1 Create Employee\n";
		cout<<"2 Display Record\n";
		cout<<"3 Delete Record\n";
		cout<<"4 Press 0 to exit\n\n\n\n";
		cin>>ch;
		switch(ch){
			case 1:cout<<"Record"<<++size<<":";
					emp[size-1].create_emp();
					emp = new Employee[size+1];
					break;
			case 2:for(int i=0;i<size;i++){
					emp[i].show_emp();}
					break;
			case 3:for(int i=0;i<size;i++){
					emp[i].delete_emp(emp);}
					break;
			//case default:exit();
		}
	}while(ch);
	return 0;
}


problem with above code is that when we have entered value and then displayed on screen at that time only last entered value would be display rather than displaying all the values.
So how do we have to display all the entered record and then delete any specific record by deleting object holding that record???
void create_emp();

This should be a constructor.


And this:
void delete_emp(Employee *e);

should be the destructor.

Always have the default case for a switch.

1
2
case default:
    exit();


This bit doesn't make sense:

cout<<"4 Press 0 to exit\n\n\n\n";

Why not make it option 0 and put it in the switch?

I personally avoid using do loops - they can always be written as a while or for loop with a decent end condition.

Now, what I would do for this program:

Have the class Employee which just stores the info & has suffient functions to access the private members. Next have another class which has a <vector> or <list > of the employee objects. This class provides the functions to carry out operations on individual employee objects or all of them.

HTH Good Luck!!!!
Yeah but how do i have to define destructure for employee class and also i don't want to use stl library for employee operations like store info. delete info. display info. all things i wanna do with only dynamic memory but how?? Can anyone plz share code so that i can implement it with better understanding.
To use dynamic memory you need to use dynamic arrays.

Employee * var = new Employee [10];
or to use STL vector, not array. It would be easier.
define destructure for employee class


By this do you mean a destructor? If so you can provide a destructor that uses delete on anything that was created with new inside the class. Use delete in main or elsewhere objects are created with new

and also i don't want to use stl library for employee operations like store info. delete info. display info.


These can be ordinary functions - although what is your objection to using things like push_back to put objects into vectors?

all things i wanna do with only dynamic memory but how??


That is what I was talking about with this remark.

Have the class Employee which just stores the info & has suffient functions to access the private members. Next have another class which has a <vector> or <list > of the employee objects. This class provides the functions to carry out operations on individual employee objects or all of them.


You know how to create a class, so create a CEmployeeVtr or CEmployeeList, which would use a <vector> or <list> respectively. I prepend my class name with "C" - it makes it easier to name objects. It is also a good idea to prepend member variables with "m_".

You will need to create a function that retrieves a particular Employee record. You can then write all your other functions that work with Employee objects.

Can anyone plz share code so that i can implement it with better understanding.


I think it is your job to write the code - we can give hints & ideas.

Finally, it is normal to put the class declaration in a header file, while the implementation goes in a .cpp file.
Last edited on
Topic archived. No new replies allowed.