|
-
December 11th, 2011, 11:32 AM
#1
[RESOLVED] Array of objects - member variable in free store error
#include<iostream.h>
#include<string.h>
class Employee{
protected:
char* name;
public:
Employee(){ // Default constructor used to declare arrays
name="empty";
cout << "Default constructor called for " << name << endl;
}
Employee(char* name_1){
name=new char[strlen(name_1)+1];
name=name_1;
cout << "Explicit constructor called for " << name << endl;
}
~Employee(){
delete [] name;
cout << "Destructor called for " << name << endl;
}
char* show(){return name;}
};
int main(){
cout << "----------------------" << endl;
Employee Executive[2];
Executive[0]=Employee("Alex");
Executive[1]=Employee("Mery");
cout << "your user is " << Executive[0].show() << endl;
cout << "your user is " << Executive[1].show() << endl;
return 0;
}
------------------------------------------------------------
array is created and destroyed as soon as it created. Why ?
There are other problems also what's wrong with this code ?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|