?? goods.h
字號:
#include "string_con.h"
#include <string.h>
#include <stdlib.h>
class Goods{
string name;
int value;
public:
Goods():name(""),value(0){}
Goods(string &na,int va):name(na),value(va){}
Goods(const Goods &g):name(g.name),value(g.value){}
string GetName()const{
return name;
}
int GetValue()const{
return value;
}
void operator=(Goods &v){
name=v.name;
value=v.value;
}
Goods operator+(int cnt){//只有數(shù)量相加減才有意義
int x=value+cnt;
Goods temp(name,x);
return temp;
}
Goods operator-(int cnt){
int x=value-cnt;
Goods temp(name,x);
return temp;
}
int operator==(string &na){//只需要判斷貨物名稱
return name==na;
}
int operator!=(string &na){
return name!=na;
}
int operator==(Goods &g){
return name==g.name;
}
int operator!=(Goods &g){
return name!=g.name;
}
friend istream& operator>>(istream& in, Goods &g);
friend ostream& operator<<(ostream& out, Goods &g);
};
istream& operator>>(istream& in, Goods &g){
char digit[20];
int num;
cout<<"貨品名稱:"; in>>g.name;
do {//容錯(cuò)控制
cout<<"貨品數(shù)量(輸入不超過6位的數(shù)字字符,數(shù)值>0):"; in>>digit;
if (strlen(digit)>6) cout<<"對不起,數(shù)位太長,數(shù)據(jù)將溢出,請重輸;"<<endl;
else {
num=atoi(digit);
if (num>0) {
g.value=num; return in;
}
else cout<<"對不起,數(shù)據(jù)小于或等于0,請重輸;"<<endl;
}
}while(1);
}
ostream& operator<<(ostream& out, Goods &g){
out<<"貨品名稱:"<<g.name<<" ";
out<<"貨品數(shù)量:"<<g.value<<endl;
return out;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -