C++Primer 第五版 习题答案 第七章 类

本文详细解答了C++ Primer第五版第七章关于类的习题,涵盖const成员函数、友元、封装、继承等多个主题。讨论了类的访问修饰符作用、接口与实现分离的优点、成员初始化顺序以及静态成员的特点等概念。
摘要由CSDN通过智能技术生成

7.1

#include <iostream>
#include <string>

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

int main()
{
   
    Sales_data total;
    double totalPrice;
    if (std::cin >> total.bookNo >> total.units_sold >> totalPrice)
    {
   
        total.revenue = total.units_sold * totalPrice;

        Sales_data trans;
        double transPrice;
        while (std::cin >> trans.bookNo >> trans.units_sold >> transPrice)
        {
   
            trans.revenue = trans.units_sold * transPrice;

            if (total.bookNo == trans.bookNo)
            {
   
                total.units_sold += trans.units_sold;
                total.revenue += trans.revenue;
            }
            else
            {
   
                std::cout << total.bookNo << " " << total.units_sold << " " << total.revenue << " ";
                if (total.units_sold != 0)
                    std::cout << total.revenue / total.units_sold << std::endl;
                else
                    std::cout << "(no sales)" << std::endl;

                total.bookNo = trans.bookNo;
                total.units_sold = trans.units_sold;
                total.revenue = trans.revenue;
            }
        }

        std::cout << total.bookNo << " " << total.units_sold << " " << total.revenue << " ";
        if (total.units_sold != 0)
            std::cout << total.revenue / total.units_sold << std::endl;
        else
            std::cout << "(no sales)" << std::endl;

        return 0;
    }
    else
    {
   
        std::cerr << "No data?!" << std::endl;
        return -1;  // indicate failure
    }
}


7.2

#ifndef SALES_DATA_H
#define SALES_DATA_H

#include<string>
struct Sale_data
{
   
    std::string bookNo;
    unsigned units_sold = 0;
    double revenue = 0.0;
    std::string  isbn() const {
   return bookNo};
    Sales_data& combine (const Sales_data&);
};

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

7.3

#include <iostream>
#include <string>
#include "Sales_data.h"

int main()
{
   
    Sales_data total;
    double totalPrice;

    if (std::cin >> total.bookNo >> total.units_sold >> totalPrice)
    {
   
        total.revenue = total.units_sold * totalPrice;

        Sales_data trans;
        double transPrice;
        while (std::cin >> trans.bookNo >> trans.units_sold >> transPrice)
        {
   
            trans.revenue = trans.units_sold * transPrice;

            if (total.isbn() == trans.isbn())
            {
   
                total.combine(trans);
            }
            else
            {
   
                std::cout << total.bookNo << " " << total.units_sold << " " << total.revenue << " ";
                if (total.units_sold != 0)
                    std::cout << total.revenue / total.units_sold << std::endl;
                else
                    std::cout << "(no sales)" << std::endl;

                total = trans;
            }
        }

        std::cout << total.bookNo << " " << total.units_sold << " " << total.revenue << " ";
        if (total.units_sold != 0)
            std::cout << total.revenue / total.units_sold << std::endl;
        else
            std::cout << "(no sales)" << std::endl;

        return 0;
    }
    else
    {
   
        std::cerr << "No data?!" << std::endl;
        return -1;  // indicate failure
    }
}

7.4

#ifndef PERSON_H_
#define PERSON_H_
#include <string>
struct Persson
{
   
std::string person;
std::string address;
};
#endif

7.5

应该是const,因为只需要读取成员对象,无需改变

ifndef PERSON_H_
#define PERSON_H_
#include <string>
struct Persson
{
   
std::string name;
std::string address;
std::string get_name() const{
   return name};
std::string get_address() const{
   return address};
};
#endif

7.7

#ifndef SALES_DATA_H_
#define SALES_DATA_H_

#include <string>

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

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

Sales_data& Sales_data::combine(const Sales_data& rhs)
{
   
    units_sold += rhs.units_sold;
    revenue += rhs.revenue;
    return *this;
}
double Sales_data::avg_price()const
{
   
if(units_sold)
	return revenue/units_sold;
else
	return 0;	
}
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;
retuen is;
}
std::ostream &print(ostream&os,const Sales_data &item)
{
   
os<<item.isbn()<<" "<<item.units_sold<<" "<<item.revenue<<" "<<item.avg_price();
return os;
}
Sales_data add(const Sales_data &lhs, const Sales_data &rhs)
{
   
Sales_data sum=lhs;
sum.combine(rhs);
return sum;
}
#endif

7.7

#include <iostream>
#include <string>
#include "Sales_data.h"

int main()
{
   
    Sales_data total;

    if (read(std::cin, total))
    {
   
        Sales_data trans;

        while (read(std::cin, trans))
        {
   
            if (total.isbn() == trans.isbn())
            {
   
                total.combine(trans);
            }
            else
            {
   
                print(std::cout, total);
                std::cout << std::endl;
                total = trans;
            }
        }
        print(std::cout, total);
        std::cout << std::endl;

        return 0;
    }
    else
    {
   
        std::cerr << "No data?!" << std::endl;
        return -1;  // indicate failure
    }
}

7.8

read需要改变成员对象;
print只需要读取成员对象。

7.9

ifndef PERSON_H_
#define PERSON_H_
#include <string>
struct Persson
{
   
std::string name;
std::string address;
std::string get_name() const{
   return name};
std::string get_address() const{
   return address};
};
std::istream &read(istream &is, Person &item)
{
   
return is>>item.name>>" ">>item.address;
}
std::ostream &print(ostream &os, Person &item)
{
   
return os<<item.name<<" "<<item.address;
}
#endif

7.10

读入data1和data2并判断是否为真

7.11

Sales_data_ex11.h

#ifndef SALES_DATA_H_
#define SALES_DATA_H_

#include <string>

struct Sales_data
{
   
	Sales_data()=default;
	Sales_data(const std::string &s):bookNo(s){
   }
	Sales_data(const std::string &s, unsigned n, double p): bookNo(s),units_sold(n),revenue(p*n){
   }
	Sales_data(std::istream &);
    std::string bookNo;
    unsigned units_sold = 0;
    double revenue = 0.0;

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

Sales_data& Sales_data::combine(const Sales_data &rhs)
{
   
	units_sold += rhs.units_sold;
	revenue += rhs.revenue;

	return *this;
}

double Sales_data::avg_price() const
{
   
	if(units_sold)
		return revenue / units_sold;
	else
		return 0;
}

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() << " " 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值