C++ Primer(第五版)第7章答案

第7章 类


练习7.1

#include <iostream>
#include <string>
using namespace std;

struct Sales_data
{
   
 string bookNo;
 unsigned units_sold = 0;
 double revenue = 0.0;
};

int main()
{
   
 Sales_data total;
 if (cin >> total.bookNo >> total.units_sold >> total.revenue)
 {
   
  Sales_data trans;
  while (cin >> trans.bookNo >> trans.units_sold >> trans.revenue)
  {
   
   if (total.bookNo == trans.bookNo)
   {
   
    total.units_sold += trans.units_sold;
    total.revenue += trans.revenue;
   }
   else
   {
   
    cout << total.bookNo << " " << total.units_sold << " " << total.revenue << endl;
    total = trans;
   }
  }
  cout << total.bookNo << " " << total.units_sold << " " << total.revenue << endl;
 }
 else
 {
   
  std::cerr << "No data?!" << std::endl;
  return -1;
 }
 return 0;
}

练习7.2

#include <string>

struct Sales_data
{
   
 std::string isbn() const {
    return bookNo; };
 Sales_data& combine(const Sales_data&);

 std::string bookNo;
 unsigned units_sold = 0;
 double revenue = 0.0;
};

Sales_data& Sales_data::combine(const Sales_data& rhs)
{
   
 units_sold += rhs.units_sold;
 revenue += rhs.revenue;
 return *this;
}

练习7.3

#include "ex7_2.h"
#include <iostream>
using namespace std;
int main()
{
   
 Sales_data total;
 if (cin >> total.bookNo >> total.units_sold >> total.revenue)
 {
   
  Sales_data trans;
  while (cin >> trans.bookNo >> trans.units_sold >> trans.revenue)
  {
   
   if (total.isbn() == trans.isbn())
   {
   
    total.combine(trans);
   }
   else
   {
   
    cout << total.bookNo << " " << total.units_sold << " " << total.revenue << endl;
    total = trans;
   }
  }
  cout << total.bookNo << " " << total.units_sold << " " << total.revenue << endl;
 }
 else
 {
   
  cerr << "No data?!" << endl;
 }
}

练习7.4

#include <iostream>
#include <string>

struct Person
{
   
 std::string name;
 std::string address;
};

练习7.5

#include <iostream>
#include <string>

struct Person
{
   
 std::string name;
 std::string address;
 std::string get_name() const {
    return name; }
 std::string get_address() const {
    return address; }
};

应该是const的,常量Person对象也需要用到这些操作

练习7.6

#include <string>
#include <iostream>

struct Sales_data
{
   
 std::string isbn() const {
    return bookNo; };
 Sales_data& combine(const Sales_data&);

 std::string bookNo;
 unsigned units_sold = 0;
 double revenue = 0.0;
};

Sales_data& Sales_data::combine(const Sales_data& rhs)
{
   
 units_sold += rhs.units_sold;
 revenue += rhs.revenue;
 return *this;
}
std::istream &read(std::istream &is, Sales_data &item)
{
   
 double price = 0;
 is >> item.bookNo >> item.units_sold >> price;
 item.revenue = price * item.units_sold;
 return is;
}

std::ostream &print(std::ostream &os, const Sales_data &item)
{
   
 os << item.isbn() << " " << item.units_sold << " " << item.revenue;
 return os;
}

Sales_data add(const Sales_data &lhs, const Sales_data &rhs)
{
   
 Sales_data sum = lhs;
 sum.combine(rhs);
 return sum;
}

练习7.7

#include "ex7_6.h"

using namespace std;

int main()
{
   
 Sales_data total;
 if (read(cin,total))
 {
   
  Sales_data trans;
  while (read(cin,trans))
  {
   
   if (total.isbn() == trans.isbn())
   {
   
    total.combine(trans);
   }
   else
   {
   
    print(cout, total) << endl;
    total = trans;
   }
  }
  print(cout, total) << endl;
 }
 else
 {
   
  cerr << "No data?!" << endl;
 }
}

练习7.8

因为read函数会改变对象的内容,而print函数不会

练习7.9

#include <iostream>
#include <string>

struct Person
{
   
 std::string name;
 std::string address
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

邻家的阿飞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值