⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cow.cpp

📁 我学习C++ Primer Plus过程中写下的课后作业的编程代码
💻 CPP
字号:
// 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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -