#include <iostream>
#include <cstring>
using namespace std;
class Cow
{
char name[20];
char * hobby;
double weight;
public:
Cow()
{
strcpy(name,"peter");
strcpy(hobby,"nothing");
weight = 1.0;
}
Cow(const char * nm, const char * ho, double wt)
{
int len = strlen(ho);
strncpy(name,nm,19);
name[19] = '\0';
hobby = new char[len + 1];
strcpy(hobby,ho);
weight = wt;
}
Cow(const Cow & c)
{
int len = strlen(c.hobby);
strcpy(name,c.name);
hobby = new char[len + 1];
strcpy(hobby,c.hobby);
weight = c.weight;
}
~Cow()
{
delete [] hobby;
}
Cow & operator=(const Cow & c)
{
strcpy(name,c.name);
delete [] hobby;
hobby = new char[strlen(c.hobby) + 1];
strcpy(hobby,c.hobby);
weight = c.weight;
}
void ShowCow() const
{
cout << "Name: " << name << endl
<< "Hobby: " << hobby << endl
<< "Weight: " << weight << endl;
}
};
int main()
{
Cow cow1;
cow1.ShowCow();
return 0;
}
Program just crashes, spent time on it, but can't seem to find the problem. Does anyone see the problem?