C++Primer第五版 7.3.1节练习

练习7.23:编写你自己的Screen类
答:见云盘程序 练习7.23.cpp

练习7.24:给你的Screen类添加三个构造函数:一个默认的构造函数;另一个构造函数接受宽和高的值,然后将contents初始化成给定数量的空白;第三个构造函数接受宽和高的值以及一个字符,该字符作为初始化之后屏幕的内容。
答:见云盘程序 练习7.24.cpp

练习7.25:Screen能安全地依赖于拷贝和赋值操作的默认版本吗?如果能,为什么?如果不能,为什么?
答:不能
假设有Screen对象s1(2,3,‘F’)
使用默认版本的拷贝和赋值
等价于 s1.height = 2; s1.width = 3 //这两个操作没有问题
而s1.contents = ‘F’错误,因为contents是string类型。‘F’是一个字符,不能够赋值,必须通过一定合理操作,初始化s1.contents

练习7.26:将Sales_data::avg_price定义成内联函数。
答:见云盘程序 练习7.26.cpp

练习7.23

/*
*练习7.23 
*日期:2015/7/7
*问题描述:练习7.23:编写你自己的Screen类
*功能;写一个Screen类 
*作者:Nick Feng 
*邮箱:nickgreen23@163.com 
*/


#include <iostream>
#include <string>

using namespace std;

class Screen{
public:
    typedef std::string::size_type pos;
    Screen() = default;
    Screen(pos ht,pos wd, char c) : height(ht),width(wd),contents(ht*wd,c){}
    char get() const
    {
        return contents[cursor];
    }
    inline char get(pos ht, pos wd) const;
    Screen &move(pos r, pos c);
private:
    pos cursor = 0;
    pos height = 0, width = 0;
    std::string contents;   
}; 

inline Screen& Screen::move(pos r, pos c)
{
    pos row = r * width;
    cursor = row + c;
    return *this;
}

char Screen::get(pos r, pos c) const
{
    pos row = r * width;
    return contents[row + c];
}

int main()
{
    Screen s1(20,20,'F');
    cout << s1.get(2,3) << endl;
    return 0;
}

练习7.24

/*
*练习7.24
*日期:2015/7/7
*问题描述:练习7.24:给你的Screen类添加三个构造函数:一个默认的构造函数;另一个构造函数接受宽和高的值,然后将contents初始化成给定数量的空白;第三个构造函数接受宽和高的值以及一个字符,该字符作为初始化之后屏幕的内容。
*功能;写三个构造函数 
*作者:Nick Feng 
*邮箱:nickgreen23@163.com 
*/

#include <iostream>
#include <string> 

using namespace std;

class Screen{
public: 
    typedef std::string::size_type pos;
    Screen() = default;                                                      //一个默认的构造函数 
    Screen(pos ht, pos wd) : height(ht), width(wd), contents(ht*wd,' '){}     //第二个构造函数 
    Screen(pos ht, pos wd,char c) : height(ht),width(wd),contents(ht*wd,c){}  //第三个构造函数 
    char get() const
    {
        return contents[cursor];
    }
    inline char get(pos r, pos c) const;
    Screen &move(pos r, pos c);
private:
    pos cursor;
    pos height = 0, width = 0;
    std::string contents; 
};

char Screen::get(pos r, pos c) const
{
    pos row = r * width;
    return contents[row + c];
}

inline 
Screen& Screen::move(pos r, pos c)
{
    pos row = r * width;
    cursor = row + c;
    return *this;
}

int main()
{
    Screen s2(2,3);
    cout << s2.get(1,1) << endl;

    Screen s1(2,3,'F');
    cout << s1.get(1,1) << endl;
    return 0;
}

练习7.26

/*
*练习7.26 
*日期:2015/7/7 
*问题描述:练习7.26:将Sales_data::avg_price定义成内联函数。
*功能;说明在7.16的基础上简单改动 
*说明: 一种改法将inline加在类外部,隐式内联;还一种将inline加在类内部,显示内联;第三种,不加inline,在类里面直接定义函数体,等同于内联效果  
*作者:Nick Feng 
*邮箱:nickgreen23@163.com 
*/


/*
*练习7.13
*日期:2015/7/6 
*问题描述:练习7.13:使用istream构造函数重写第229页的程序。
*功能;改造main函数 ,说明只在total地方用了构造函数,下面的没有变 
*作者:Nick Feng 
*邮箱:nickgreen23@163.com 
*/

#include <iostream>
#include <string>

using namespace std;

struct Sales_data{
    friend istream &read(istream &, Sales_data &);//一个友元函数read,便于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 &is){
    read(is,*this);
    }


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

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

Sales_data add(const Sales_data&, const Sales_data&);
std::ostream &print(std::ostream&, const Sales_data&);
std::istream &read(std::istream&, Sales_data&);

inline                                      //一种改法将inline加在类外部,隐式内联;还一种将inline加在类内部,显示内联;第三种,不加inline,在类里面直接定义函数体,等同于内联效果 
double Sales_data::avg_price() const{
    if (units_sold)
        return revenue/units_sold;
    else 
        return 0;
}

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

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;
}

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

/* 
Sales_data::Sales_data(std::istream &is)
{
    read(is,*this);
}
*/

int main()
{
    Sales_data total(cin);//这里用了新的构造函数 
    if (total.units_sold) //这里作了改动 
    {
        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;
    }
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值