???C++??
???????????? ???????[ 2017/1/10 11:00:33 ] ??????????????????? C++
????C++?????????????????????????????????????á??????????????????
??????????????
???????????á?c++ primer?? ????????
struct Sales_data{
//?????????????
string isbn()const{return bookNo;} //?????????????????????
Sales_data& Combine(const Sales_data&); //?????????????????????
double avg_price()const; //?????????????????????
//????
string bookNo;
unsigned units_sold;
double revenue;
};
//3????????????
Sales_data add(const Sales_data&?? const Sales_data&);
ostream &print(ostream&?? const Sales_data&);
istream &read(istream&?? Sales_data&);
istream &read(istream &is?? Sales_data &item){
double price = 0;
is>>item.bookNo>>item.units_sold>>price;
item.revenue=price*item.units_sold;
return is;
}
ostream &print(ostream &os?? const Sales_data &item){
os<<item.isbn()<<" "<<item.units_sold<<" "
<<item.revenue<<" "<<item.avg_price();
return os;
}
double Sales_data::avg_price()const{
if(units_sold) {
return revenue/units_sold;
} else {
return 0;
}
}
Sales_data add(const Sales_data& a??const Sales_data &b){
Sales_data sum=a;
sum.Combine(b);
return sum;
}
Sales_data& Sales_data::Combine(const Sales_data &a){
units_sold+=a.units_sold;
revenue+=a.revenue;
return *this;
}
?????????????????????????????????????????????????????????????????????????????????
????1??this???
????????isbn??????
????string isbn()const{return bookNo;}
???????????????λ??bookNo???????????????????е?????????????????this????????????????????????????????????????
????string isbn()const{return (*this).bookNo;}
????????
????string isbn()const{return this->bookNo;}
?????????this????????????????????????????????this?????????????????????????????????????
????2??????const???????
???????isbn??????????б?????????const???????????£?
??????? ???????? this??????????????????????汾???????????????г???????У?this????????Sales_data *const?????????????this?????????????????????this????????????????isbn???????????????????????????this???????????????????this??????????????????????б??????c++???y??????????б???????const???this???????????????const???????????????????
??????????
???????????????????????????????????????????????????й?????
????????????з????
????????????????????const????????????const?????????????????????????????????????ó?????????????????const????????????п???????д???
????????????Sales_data???ж???3??????????????2???????ж??壬1?????????壺
structSales_data{
Sales_data(conststring&s):bookNo(s){}
Sales_data(conststring&s??unsignedn??doublep):bookNo(s)??units_sold(n)??revenue(p*n){}
Sales_data(istream&){}
......
};
???????????????б????????????????
???????????????
????????????÷??????η??????????Sales_data??
class Sales_data{
public:
Sales_data(const string &s):bookNo(s){}
Sales_data(const string &s??unsigned n??double p):bookNo(s)??units_sold(n)??revenue(p*n){}
Sales_data(istream &){}
string isbn()const{return bookNo;}
Sales_data& Combine(const Sales_data&);
private:
double avg_price()const {return units_sold?revenue/units_sold:0;}
string bookNo;
unsigned units_sold;
double revenue;
};
???????????????????????г????public??????struct?????????????private??????class
?????????????????в???????????????enjoy it
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11