Hi,
I need to see the output of the program when no copy
constructor is used. Since by default, there is an implicit one, how
should i perform the override, so as to totally remove the copy
constructor functionality.
Consider the following code.
class Employee
{
private:
char* designation;
int experience;
public:
Employee(char* dsgn, int exp)
{
experience=exp;
designation = new char[strlen(dsgn)];
strcpy(designation, dsgn);
}
char* getDesignation() { return designation; }
void setDesignation(char* dsgn) { strcpy(designation,
dsgn); }
int getExperience() { return experience; }
void setExperience(int exp) { experience=exp; }
// My override. Doesnt work fine.
Employee(Employee&e) { }
};
int main()
{
Employee trainee("Trainee",0);
Employee athi = trainee;
Employee vk = trainee;
cout << athi.getDesignation() << " with " <<
athi.getExperience() << " year experience" << endl;
cout << vk.getDesignation() << " with " << vk.getExperience()
<< " year experience" << endl;
return 0;
}
Output
----------
with 1108544020 year experience
Segmentation fault (core dumped)
As you can see, if no copy constructor is called, by default the
functionality should be a bitwise copy. i.e both should print same
results. But here it results in unexpected output with a seg fault.
Where am i going wrong?
Regards,
Sarathy