C++primer第四版例程中sales_item.h中的问题

u010227315 2015-03-14 09:10:58
Sales_item类中有这样一个语句,Sales_item(std::istream &is) { is >> *this; }
这个语句是什么意思呢,C++新手提问……
...全文
244 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
u010227315 2015-03-17
  • 打赏
  • 举报
回复
引用 7 楼 jiht594 的回复:
[quote=引用 6 楼 u010227315 的回复:] [quote=引用 4 楼 jiht594 的回复:] [quote=引用 3 楼 u010227315 的回复:] [quote=引用 1 楼 jiht594 的回复:] 构造函数 Sales_item item(std::cin);可以这么用
我把这条语句注释掉后发现也能运行,这是什么原因啊?[/quote] 你把Sales_item(std::istream &is) { is >> *this; }这句注释了? 还能运行Sales_item item(std::cin);这句? 我不信[/quote] 编译和运行都没有问题啊[/quote] 你看清楚 我说的是这句 Sales_item item(std::cin); 你加到main函数里试一下就知道了[/quote]明白了,多谢多谢,原来这是重载操作符 << 之外的另一种数据构造方法
jiht594 2015-03-16
  • 打赏
  • 举报
回复
Sales_item item(std::cin);就是一个构造函数,函数中使用了重载的>> 和Sales_item item; 然后再std::cin >> item; 效果一样
jiht594 2015-03-16
  • 打赏
  • 举报
回复
引用 6 楼 u010227315 的回复:
[quote=引用 4 楼 jiht594 的回复:] [quote=引用 3 楼 u010227315 的回复:] [quote=引用 1 楼 jiht594 的回复:] 构造函数 Sales_item item(std::cin);可以这么用
我把这条语句注释掉后发现也能运行,这是什么原因啊?[/quote] 你把Sales_item(std::istream &is) { is >> *this; }这句注释了? 还能运行Sales_item item(std::cin);这句? 我不信[/quote] 头文件注释掉后是这样
#ifndef SALESITEM_H
#define SALESITEM_H

// Definition of Sales_item class and related functions goes here


#include <iostream>
#include <string>

class Sales_item {
friend bool operator==(const Sales_item&, const Sales_item&);
// other members as before
public:
    // added constructors to initialize from a string or an istream
    Sales_item(const std::string &book):
              isbn(book), units_sold(0), revenue(0.0) { }
    //Sales_item(std::istream &is) { is >> *this; }
    friend std::istream& operator>>(std::istream&, Sales_item&);
    friend std::ostream& operator<<(std::ostream&, const Sales_item&);
public:
    // operations on Sales_item objects
    // member binary operator: left-hand operand bound to implicit this pointer
    Sales_item& operator+=(const Sales_item&);
    // other members as before
    
public:
    // operations on Sales_item objects
    double avg_price() const;
    bool same_isbn(const Sales_item &rhs) const
        { return isbn == rhs.isbn; }
    // default constructor needed to initialize members of built-in type
    Sales_item(): units_sold(0), revenue(0.0) { } 
// private members as before
private:
    std::string isbn;
    unsigned units_sold;
    double revenue;

};


// nonmember binary operator: must declare a parameter for each operand
Sales_item operator+(const Sales_item&, const Sales_item&);

inline bool 
operator==(const Sales_item &lhs, const Sales_item &rhs)
{
    // must be made a friend of Sales_item
    return lhs.units_sold == rhs.units_sold &&
           lhs.revenue == rhs.revenue &&
	   lhs.same_isbn(rhs);
}

inline bool 
operator!=(const Sales_item &lhs, const Sales_item &rhs)
{
    return !(lhs == rhs); // != defined in terms of operator==
}

using std::istream; using std::ostream;

// assumes that both objects refer to the same isbn
inline
Sales_item& Sales_item::operator+=(const Sales_item& rhs) 
{
    units_sold += rhs.units_sold; 
    revenue += rhs.revenue; 
    return *this;
}

// assumes that both objects refer to the same isbn
inline
Sales_item 
operator+(const Sales_item& lhs, const Sales_item& rhs) 
{
    Sales_item ret(lhs);  // copy lhs into a local object that we'll return
    ret += rhs;           // add in the contents of rhs 
    return ret;           // return ret by value
}

inline
istream& 
operator>>(istream& in, Sales_item& s)
{
    double price;
    in >> s.isbn >> s.units_sold >> price;
    // check that the inputs succeeded
    if (in)
        s.revenue = s.units_sold * price;
    else 
        s = Sales_item();  // input failed: reset object to default state
    return in;
}

inline
ostream& 
operator<<(ostream& out, const Sales_item& s)
{
    out << s.isbn << "\t" << s.units_sold << "\t" 
        << s.revenue << "\t" <<  s.avg_price();
    return out;
}

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


#endif
主文件是这样
#include <iostream>
#include "Sales_item.h"

int main() 
{
    Sales_item item1, item2;

    std::cin >> item1 >> item2;   //read a pair of transactions
    std::cout << item1 + item2 << std::endl; //print their sum

    return 0;
}
编译和运行都没有问题啊[/quote] 你看清楚 我说的是这句 Sales_item item(std::cin); 你加到main函数里试一下就知道了
u010227315 2015-03-16
  • 打赏
  • 举报
回复
引用 4 楼 jiht594 的回复:
[quote=引用 3 楼 u010227315 的回复:] [quote=引用 1 楼 jiht594 的回复:] 构造函数 Sales_item item(std::cin);可以这么用
我把这条语句注释掉后发现也能运行,这是什么原因啊?[/quote] 你把Sales_item(std::istream &is) { is >> *this; }这句注释了? 还能运行Sales_item item(std::cin);这句? 我不信[/quote] 头文件注释掉后是这样
#ifndef SALESITEM_H
#define SALESITEM_H

// Definition of Sales_item class and related functions goes here


#include <iostream>
#include <string>

class Sales_item {
friend bool operator==(const Sales_item&, const Sales_item&);
// other members as before
public:
    // added constructors to initialize from a string or an istream
    Sales_item(const std::string &book):
              isbn(book), units_sold(0), revenue(0.0) { }
    //Sales_item(std::istream &is) { is >> *this; }
    friend std::istream& operator>>(std::istream&, Sales_item&);
    friend std::ostream& operator<<(std::ostream&, const Sales_item&);
public:
    // operations on Sales_item objects
    // member binary operator: left-hand operand bound to implicit this pointer
    Sales_item& operator+=(const Sales_item&);
    // other members as before
    
public:
    // operations on Sales_item objects
    double avg_price() const;
    bool same_isbn(const Sales_item &rhs) const
        { return isbn == rhs.isbn; }
    // default constructor needed to initialize members of built-in type
    Sales_item(): units_sold(0), revenue(0.0) { } 
// private members as before
private:
    std::string isbn;
    unsigned units_sold;
    double revenue;

};


// nonmember binary operator: must declare a parameter for each operand
Sales_item operator+(const Sales_item&, const Sales_item&);

inline bool 
operator==(const Sales_item &lhs, const Sales_item &rhs)
{
    // must be made a friend of Sales_item
    return lhs.units_sold == rhs.units_sold &&
           lhs.revenue == rhs.revenue &&
	   lhs.same_isbn(rhs);
}

inline bool 
operator!=(const Sales_item &lhs, const Sales_item &rhs)
{
    return !(lhs == rhs); // != defined in terms of operator==
}

using std::istream; using std::ostream;

// assumes that both objects refer to the same isbn
inline
Sales_item& Sales_item::operator+=(const Sales_item& rhs) 
{
    units_sold += rhs.units_sold; 
    revenue += rhs.revenue; 
    return *this;
}

// assumes that both objects refer to the same isbn
inline
Sales_item 
operator+(const Sales_item& lhs, const Sales_item& rhs) 
{
    Sales_item ret(lhs);  // copy lhs into a local object that we'll return
    ret += rhs;           // add in the contents of rhs 
    return ret;           // return ret by value
}

inline
istream& 
operator>>(istream& in, Sales_item& s)
{
    double price;
    in >> s.isbn >> s.units_sold >> price;
    // check that the inputs succeeded
    if (in)
        s.revenue = s.units_sold * price;
    else 
        s = Sales_item();  // input failed: reset object to default state
    return in;
}

inline
ostream& 
operator<<(ostream& out, const Sales_item& s)
{
    out << s.isbn << "\t" << s.units_sold << "\t" 
        << s.revenue << "\t" <<  s.avg_price();
    return out;
}

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


#endif
主文件是这样
#include <iostream>
#include "Sales_item.h"

int main() 
{
    Sales_item item1, item2;

    std::cin >> item1 >> item2;   //read a pair of transactions
    std::cout << item1 + item2 << std::endl; //print their sum

    return 0;
}
编译和运行都没有问题啊
为争 2015-03-16
  • 打赏
  • 举报
回复
Sales_item(std::istream &is) { is >> *this; } sales_item这是一个构造函数 std::istream &is 这是构造函数的参加 is >> *this 把this指向的对象输入到is
jiht594 2015-03-14
  • 打赏
  • 举报
回复
引用 3 楼 u010227315 的回复:
[quote=引用 1 楼 jiht594 的回复:] 构造函数 Sales_item item(std::cin);可以这么用
我把这条语句注释掉后发现也能运行,这是什么原因啊?[/quote] 你把Sales_item(std::istream &is) { is >> *this; }这句注释了? 还能运行Sales_item item(std::cin);这句? 我不信
u010227315 2015-03-14
  • 打赏
  • 举报
回复
引用 1 楼 jiht594 的回复:
构造函数 Sales_item item(std::cin);可以这么用
我把这条语句注释掉后发现也能运行,这是什么原因啊?
fly_dragon_fly 2015-03-14
  • 打赏
  • 举报
回复
从istream输入构造对象
jiht594 2015-03-14
  • 打赏
  • 举报
回复
构造函数 Sales_item item(std::cin);可以这么用

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧