note
In C++ we define our own data structures by defining a class.
A class defines a type along with a collection of operations that are related to that type.
Header file names are derived from the name of a class defined in that header.
Header files that we write usually have a suffix of .h, but some programmers use .H, .hpp, or .hxx.
The standard library headers typically have no suffix at all.
This program starts with two #include directives, one of which uses a new form. Headers from the standard library are enclosed in angle brackets (< >). Those that are not part of the library are enclosed in double quotes (" ").
File redirect
$ addItems outfile Assuming
$ is the system prompt and our addition program has been compiled into an executable file named addItems.exe (or addItems on UNIX systems).
This command will read transactions from a file named infile and write its output to a file named outfile in the current directory.
Member functions are sometimes referred to as methods.
The dot operator applies only to objects of class type. The left-hand operand must be an object of class type, and the right-hand operand must name a member of that type. The result of the dot operator is the member named by the right-hand operand.
We call a function using the call operator (the () operator).
exercise
exercise 1.20
#include<iostream>
#include"Sales_item.h"
int main()
{
Sales_item s;
while(std::cin>>s)
{
std::cout<<s;
}
system("pause");
return 0;
}
其中用到了头文件中的
std::istream&
operator>>(std::istream& in, Sales_item& s)
{
double price;
in >> s.bookNo >> 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;
}
std::ostream&
operator<<(std::ostream& out, const Sales_item& s)
{
out << s.isbn() << " " << s.units_sold << " "
<< s.revenue << " " << s.avg_price();
return out;
}
exercise 1.21
#include<iostream>
#include"Sales_item.h"
int main()
{
Sales_item a,b;
std::cin>>a>>b;
if(compareIsbn(a,b))
{
std::cout<<a+b<<std::endl;
}
else
{
std::cerr<<"not the same ISBN"<<std::endl;
}
system("pause");
return 0;
}
用到头文件的输入输出重载符以及
bool compareIsbn(const Sales_item &lhs, const Sales_item &rhs)
{ return lhs.isbn() == rhs.isbn(); }
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
}
exercise 1.22
#include<iostream>
#include"Sales_item.h"
int main()
{
Sales_item tran,tran_curr,sum;
std::cin>>tran;
while(std::cin>>tran_curr)
{
if(compareIsbn(tran,tran_curr))
{
sum = tran + tran_curr;
tran = sum;
}
else
{
std::cerr<<"must be the same ISBN"<<std::endl;
}
}
std::cout<<sum<<std::endl;
system("pause");
return 0;
}
exercise 1.23
#include<iostream>
#include"Sales_item.h"
int main()
{
Sales_item item1,item2,item3,newItem;
std::cin>>item1;
while(std::cin>>newItem)
{
if(newItem.isbn() == item1.isbn())
{
item1 += newItem;
}
else if(newItem.isbn() == item2.isbn())
{
item2 += newItem;
}else if(newItem.isbn() == item3.isbn())
{
item3 += newItem;
} else
{
if (item2.isbn() == "")
{
item2 = newItem;
}else
{
item3 = newItem;
}
}
}
std::cout<<item1<<std::endl<<item2<<std::endl<<item3<<std::endl;
system("pause");
return 0;
}
exercise 1.24
exercise 1.25