cow.cpp

来自「我学习C++ Primer Plus过程中写下的课后作业的编程代码」· C++ 代码 · 共 58 行

CPP
58
字号
// Cow.cpp
#include <iostream>
#include "Cow.h"
#include <string>
Cow::Cow()
{
	strcpy(name,"no name");
	hobby = new char[9];
	strcpy(hobby,"no bobby");
	weight = 0.0;
}

Cow::Cow( const char * nm, const char * ho, double wt )
{
	strncpy (name, nm, 19);
	name[19] = '\0';
	int Len = strlen(ho);
	hobby = new char[Len+1];
	strcpy(hobby, ho);
	weight = wt;
}

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()
{
	delete []hobby;
}

Cow & Cow::operator = ( const Cow & c )	// 重载赋值操作符
{
	if ( this == & c )
		return * this;
	delete []hobby;
	int Len = strlen(c.hobby);
	hobby = new char[Len+1];
    strcpy ( hobby, c.hobby );
	strcpy ( name, c.name );
	weight = c.weight;
	return *this;
}

void Cow::ShowCow()const
{
	using std::cout;
	using std::endl;
	cout<<"name:\t"<<name<<endl;
	cout<<"hobby:\t"<<hobby<<endl;
	cout<<"weight:\t"<<weight<<endl;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?