compiles but not working...help plz!!

this program compilation succeeded but it doesnt work.
i guess it has something to do with the assignment operator or the copy constructor but i cant figure out what...

Header:

1
2
3
4
5
6
7
8
9
10
11
12
13
class employee{
	char *name;
	unsigned int salary;
public:
	employee();
	employee(const char*);
	employee(const char*,unsigned int);
	employee (const employee&);
	employee operator = (employee);
	void init(const char*,unsigned int);
	void print();
	~employee();
};


Cpp:

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
#include <iostream>
#include <string>
#include "class.h"

using namespace std;

employee::employee() : salary(1000)
{
	name=new char[20];
}

employee::employee(const char* ename) : salary(1000)
{
	strcpy_s(name,20,ename);
}
employee::employee(const char* ename,unsigned int salary)
{
	name=new char[20];
	strcpy_s(name,20,ename);
	this->salary=salary;
}

employee::employee(const employee& emp)
{
	name=new char[20];
	int i=0;
	while (name[i]!='\0') 
	{
		name[i]=emp.name[i];
		i++;
	}
	salary=emp.salary;
}

void employee::init(const char* ename, unsigned int salary)
{
	name=new char[20];
	strcpy_s(name,20,ename);
	this->salary=salary;
}

void employee::print()
{
	cout<<"name: ";
	int i=0;
	while (name[i]!='\0')
	{
		cout<<name[i];
		i++;
	}
	cout<<"\n"<<"salary: "<<salary<<endl;
}

employee employee::operator = (employee emp)
{
	strcpy_s(name,20,const_cast <const char*>(emp.name));
	emp.salary=salary;
	return *this;
}

employee::~employee()
{
	delete [] name;
}


Main:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include "class.h"

using namespace std;


int main()
{
	employee emp1 ("Bill Jones",5000),emp5("Michael Adams");
	employee emp2;
	emp2=emp1;
	employee emp3;
	emp3=emp2;
	employee * workers= new employee [3];
	workers[0]=emp3;
	workers[1]= employee("katy Ashton");
    delete [] workers;
}



can someone plz help me with that??
Last edited on
The problem in your code is in the constructor that takes a const char*. It allocates no memory for name to point to, but treats name as if it doesn't contain some arbitrary memory address.

The way you're using name it would make more sense for the class definition to be:

1
2
3
4
 class employee{
    char name[20] ;
    // ...
};


Although, the following should probably be preferred:

1
2
3
4
 class employee{
    std::string name;
    // ...
};

Last edited on
first of all thanks for the help.
i added name=new name[20] to the constructor who gets the const char but now apparantly theres another problem with my assignment operator... can u tell what it is??
Last edited on
Other than the fact that you assign to the object that is supposed to be the source of the assignment, I don't see anything wrong with your assignment operator although the signature is unconventional.

A more conventional definition would look like the following:

1
2
3
4
5
6
employee& employee::operator=(const employee& emp)
{
    strcpy_s(name, 20, emp.name);
    salary = emp.salary;
    return *this;
}


Topic archived. No new replies allowed.