练习7.11:
#include <iostream>
#include <cstring>
using namespace std;class Sales_data {public:Sales_data() = default;Sales_data(const std::string &book): bookNo(book) {}Sales_data(const std::string &book, const unsigned num, const double sellp, const double salep);Sales_data(std::istream &is);public:std::string isbn()const {return bookNo;}public:std::string bookNo;//书籍编号unsigned units_sold = 0;//销售量double sellingprice = 0.0;//原始价格double saleprice = 0.0;//实际价格double discount = 0.0;//折扣
};std::istream &operator>>(std::istream &in, Sales_data &s) {in >> s.bookNo >> s.units_sold >> s.sellingprice >> s.saleprice;if (in && s.sellingprice != 0)s.discount = s.saleprice / s.sellingprice;elses = Sales_data();return in;
}std::ostream &operator<<(std::ostream &out, const Sales_data &s) {out << s.isbn() << " " << s.units_sold << " " << s.sellingprice << " " << s.saleprice << " " << s.discount;return out;
}Sales_data::Sales_data(const std::string &book, const unsigned num, const double sellp, const double salep) {bookNo = book;units_sold = num;sellingprice = sellp;saleprice = salep;if (sellingprice != 0)discount = saleprice / sellingprice;
}Sales_data::Sales_data(std::istream &is) {is >> *this;
}int main() {Sales_data data1;Sales_data data2("978-7-121-15535-2");Sales_data data3("978-7-121-15535-2", 100, 128, 109);Sales_data data4(cin);cout << "书籍的销售情况:" << endl;cout << data1 << endl;cout << data2 << endl;cout << data3 << endl;cout << data4 << endl;return 0;}
在类的定义中,我们设计了4个构造函数。
第一个构造函数是默认构造函数,它使用了C++11新标准提供的=default。它的参数列表为空,即不需要我们提供任何数据也能构造一个对象。
第二个构造函数只接受一个const string&,表示书籍的ISBN编号,编译器赋予其他数据成员类内初始值。
第三个构造函数接受完整的销售记录信息,const string&表示书籍的ISBN编号,const unsigned表示销售量,后面两个const double分别表示书籍的原价和实际售价。
最后一个构造函数接受istream&并从中读取书籍的销售信息。
我们在main函数中创建4个Sales_data对象并依次输出其内容,上面定义的构造函数各被用到了一次。
测试结果:
练习7.12:
class Sales_data {public:Sales_data() = default;Sales_data(const std::string &book): bookNo(book) {}Sales_data(const std::string &book, const unsigned num, const double sellp, const double salep);Sales_data(std::istream &is) {is >> *this};public:std::string isbn()const {return bookNo;}public:std::string bookNo;//书籍编号unsigned units_sold = 0;//销售量double sellingprice = 0.0;//原始价格double saleprice = 0.0;//实际价格double discount = 0.0;//折扣
};
练习7.13:
#include <iostream>
#include "Sales_data.h"
using namespace std;int main()
{cout<<"请输入交易记录(ISBN,销售额,原价,实际售价):"<<endl;Sales_data total(cin);if (cin)){Sales_data trans(cin);do{if (total.isbn()==trans.isbn())total.combine(trans);else{print(cout,total);cout<<endl;total = trans;}}while(read(cin,trans));print(cout,total);cout<<endl;}else{cerr<<"No data?!"<<endl;return -1;}return 0;
}
练习7.14:
Sales_data (const std::string &book):bookNo(book),units_sold(0),sellingprice(0),saleprice(0),discount(0){
}
练习7.15:
#include <iostream>
#include <string>
using namespace std;
class Person {private:string strName;string strAddress;public:Person() = default;Person(const string &name,const string &add){strName = name;strAddress = add;}Person(std::istream &is){is>>*this;} //这里的<<需要重载public:string getName()const{return strName;}string getAddress() const{return strAddress;}
};