pork.cpp[运算符重载][C++,类]

11 篇文章 0 订阅
7 篇文章 0 订阅

#include “pork.h”
#include

int pork::id = 0;

pork::pork(int weight)
{
this->weight = weight;
}

pork::pork(const char* name, int weight, int price)
{
if (!name)name = “空”;
this->name = new char[strlen(name)+1];//+1存放结束符,从0开始计算,到结束符停止,空串长度为0,但存放了’\0’,所以开辟的空要为1.
strcpy_s(this->name, strlen(name)+1, name);//这里如果结束符没有复制的话,就会出现使用左参时会指向其它地方出现崩溃或者bug
this->weight = weight;
this->price = price;
this->ID = ++id;
}

pork& pork::operator=(const pork& p)
{
if(this==&p)return *this;//防止自己给自己赋值
if (name)delete[] name;//释放原来内存;p1.name(“rock”)=p2.name(“rockkk”),空间不够
name = new char[strlen(p.name) + 1];
strcpy_s(name, strlen(p.name) + 1, p.name);
this->weight = p.weight;
this->price = p.price;
this->ID = p.ID+1;
return *this;
}

int pork::operator[](string i)
{//为了使程序更加的健壮,使用宏定义能够很好的避免错误
if (i == ID_KEY) { return ID; }
else if (i == WEIGHT_KEY) { return weight; }
else if (i == PRICE_KEY) { return price; }
else { return -1; }
}
int pork::operator[](int i)
{
if (i == id_key) { return ID; }
else if (i == weight_key) { return weight; }
else if (i == price_key) { return price; }
else { return -1; }
}
/*
char* pork::operator[](string i)
{
if (i == “姓名”) { return name; }
else { return NULL; }
}*/

/使用成员函数重载<<运算符
ostream& pork::operator<<(ostream& os) const
{
name ? os << “第” << ID << “头猪的名字:” << name << “\t猪的体重:” << weight << “\t猪的价格:” << price :
os << “猪的体重:” << weight;
return os;
}
/

pork::~pork()
{
if (name) {
delete[] name;
cout << “消\n”;
}
}

string pork::describe()
{
stringstream str;

name ? str << "第" << ID << "头猪的名字:" << name << "\t猪的体重:" << weight << "\t猪的价格:" << price :
	str << "猪的体重:" << weight;
return str.str();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值