C + + Third day

Source: Internet
Author: User


Things to do today:

Main Line

1, reviewed the contents of yesterday while (Std::cin >> value) scan "standard input"

2. Download the sales_item.h code below online

#ifndef Salesitem_h#defineSalesitem_h#include<iostream>#include<string>classsales_item{ Public: Sales_item (ConstSTD::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 &,ConstSales_item &); Public: Sales_item&operator+=(Constsales_item&); Public: DoubleAvg_price ()Const; BOOLSAME_ISBN (ConstSales_item &AMP;RHS)Const {  returnISBN = =RHS.ISBN;} Sales_item (): Units_sold (0), Revenue (0.0){} Public: std::stringISBN; unsigned units_sold;Doublerevenue;};usingStd::istream;usingStd::ostream; Sales_itemoperator+(ConstSales_item &ConstSales_item &); inlineBOOL operator==(ConstSales_item &AMP;LHS,ConstSales_item &RHS) { returnLhs.units_sold = = Rhs.units_sold && lhs.revenue = = Rhs.revenue &&LHS.SAME_ISBN (RHS);} InlineBOOL operator!=(ConstSales_item &AMP;LHS,ConstSales_item &RHS) { return! (LHS = =RHS);} Inline Sales_item& Sales_item::operator+=(ConstSales_item &RHS) {Units_sold+=Rhs.units_sold; Revenue+=rhs.revenue;return* This;} Inline Sales_itemoperator+(ConstSales_item &AMP;LHS,ConstSales_item &RHS) {sales_item ret (LHS); RET+=RHS;returnret;} Inline IStream&operator>> (IStream &inch, Sales_item &1) { DoublePrice ;inch>> S.ISBN >> s.units_sold >>Price ;if(inch) S.revenue= S.units_sold *Price ;Elses=Sales_item ();return inch;} Inline Ostream&operator<< (Ostream & out,ConstSales_item &1) {  out<< S.ISBN <<"\ t"<<s.units_sold <<"\ t"<< s.revenue <<"\ t"<<S.avg_price ();return  out;} InlineDoubleSales_item::avg_price ()Const{ if(Units_sold)returnrevenue/Units_sold;Else  return 0;}#endif

3. Use file redirection, for example: $ prog1<input.txt>output.txt

Regional

Things to plan for tomorrow: None

Problems encountered:

1, in C++primer e5, Access to ISBN is ITEM1.ISBN (),

In the header file I downloaded, the ISBN was directly defined as public and did not write the ISDN () function. So in the code below, you access the ISDN of the object directly with ITEM1.ISDN.

2, in addition, Std::cerr << "xxx" is not affected by redirection, XXX Direct output in the CMD interface

Exercise 1.19

#include <iostream>intMain () {std::cout<<"Enter The numbers:"<<Std::endl; intV1 =0, V2 =0; Std::cin>> v1 >>v2; inttemp =0; if(V1 >v2) {Temp=v1; V1=v2; V2=temp; }     for(; V1 <= v2; + +v1) {Std::cout<< v1 <<Std::endl; }    return 0;}

Harvest: 艹

C + + Third day

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.