// Sales_iterm.h
#ifndef SALES_ITERM_H_INCLUDED
#define SALES_ITERM_H_INCLUDED
#include <iostream>
#include <string>
//using std::string;
//using std::istream;
//using std::ostream;
class Sales_iterm
{
public:
std::istream& input(std::istream &in);
std::ostream& output(std::ostream &out) const;
double avg_price() const;
// 1、**************************************************
bool same_isbn(const Sales_iterm &rhs) const
{
return isbn == rhs.isbn;
}
Sales_iterm(): unit_sold(0), revenue(0.0) {}
private:
std::string isbn;
unsigned unit_sold;
double revenue;
};
#endif // MYSALES_ITERM_H_INCLUDED
//Sales_iterm.cpp
#include "Sales_iterm.h"
std::istream& Sales_iterm::input(std::istream &in)
{
in >> isbn >> unit_sold >> revenue;
return in;
}
std::ostream& Sales_iterm::output(std::ostream &out) const
{
out << "ISBN: " << isbn << std::endl << "units sold: "
<< unit_sold << std::endl << "revenue: " << revenue << std::endl;
return out;
}
double Sales_iterm::avg_price() const
{
if (unit_sold)
{
return revenue/unit_sold;
}
return 0;
}
// main.cpp
#include <iostream>
#include "Sales_iterm.cpp"
using namespace std;
int main()
{
Sales_iterm salesiterm1, salesiterm2;
salesiterm1.input(cin);
salesiterm2.input(cin);
if (salesiterm1.same_isbn(salesiterm2))
{
cout << "X" << endl;
}
// saleiterm.output(cout);
// cout << saleiterm.avg_price();
return 0;
}
1、Sales_iterm
类将数据成员isbn
定义为private
,这并不意味着rhs.isbn是错误的。
2、涉及到多文件的编译两种方法:a. 创建一个工程 b. 包含相应的文件