1 Replies - 316 Views - Last Post: 06 October 2012 - 04:35 AM Rate Topic: -----

#1 jayparikh  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 24-January 09

Dynamic Object

Posted 06 October 2012 - 01:16 AM

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.


#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;
}



Output is not upto the mark.

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???

Is This A Good Question/Topic? 1
  • +

Replies To: Dynamic Object

#2 Coding Mash  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 26
  • Joined: 29-September 12

Re: Dynamic Object

Posted 06 October 2012 - 04:35 AM

There are many problems with your code.
The first and foremost is that you are not allocating memory to your Object Array dynamically.

int size = 0 ;
Employee *emp ;



You are creating this array of size 1. So you can store the information of only 1 object in it.
If you want to create and store multiple objects in it, there are two solutions. Either you declare a static array of some constant size, or grow the size of the array dynamically.

For dynamic growth, the basic pattern would look like this.

int size = 0 ;[size="5"][/size]
Employee *emp = new Employee[size];   //This is the main array
Employee *temp = nullptr ;           //This is the temporary array

//for creating a new record, first shift all record of emp into temp

temp = new Employee [size] ;
for (int i = 0 ; i < size ; ++i)
{
  temp[i] = emp[i] ;
}

++size ;    //then increment size of emp

delete [] emp ;    //delete emp

temp[size - 1].create_emp() ;   //Create new record object in temp

//shift back the data into emp
emp = temp ;
temp = nullptr ;   



Doing this procedure every time, you can create and store your data to any number.[/size][/size]
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1