程序运行出错了
这是本人的一个编程作业题,是一个很简单的程序,编译可以通过,结果也能出现,但是有个严重的错误,因为出现结果的时候,总提示程序运行出错,我就是找不出来,希望哪位达人能指点我一下,感激不尽//cow.h
#ifndef COW_H_
#define COW_H_
class Cow
{
private:
char name[20];
char *hobby;
double weight;
public:
Cow();
Cow(char *nm, char *ho, double wt);
Cow(const Cow &c);
~Cow();
Cow & operator=(const Cow &c);
void ShowCow() const;
};
#endif
//////////////////////////////////////////////////
//cow.cpp
#include <iostream>
#include "cow.h"
using namespace std;
//member methods
Cow::Cow()
{
strcpy(name, "little white");
hobby = new char[20];
strcpy(hobby, "drink");
weight=100;
}
Cow::Cow(const Cow &c)
{
strcpy(name, c.name);
int len=strlen(c.hobby);
hobby=new char[len+1];
strcpy(hobby, c.hobby);
weight=c.weight;
}
Cow::Cow(char *nm, char *ho, double wt)
{
strcpy(name, nm);
int len=strlen(ho);
hobby=new char[len+1];
strcpy(hobby, ho);
wt=weight;
}
Cow::~Cow()
{
delete [] name;
delete [] hobby;
}
Cow & Cow::operator =(const Cow &c)
{
strcpy(name, c.name);
int len=strlen(c.hobby);
hobby=new char[len+1];
std::strcpy(hobby, c.hobby);
weight=c.weight;
return *this;
}
void Cow::ShowCow() const
{
cout << "name: " << name << endl;
cout << "hobby: " << hobby << endl;
cout << "Weight: " << weight << endl;
}
////////////////////////////////////////////
//usecow.cpp
#include <iostream>
#include "cow.h"
using namespace std;
int main()
{
Cow cow1;
Cow cow2("ketty", "eat", 100);
//Cow cow3(cow2);
cow1.ShowCow();
cow2.ShowCow();
//cow3.ShowCow();
//Cow cow4=cow3;
//cow4.ShowCow();
return 0;
}