...already defined in....obj什么情况?
本帖最后由 u011873969 于 2013-10-08 03:32:22 编辑 写了这样的两个代码:
这个叫做Sales_item.h
#include <iostream>
#include <string>
using std::string;
using std::cin;
using std::cout;
using std::endl;
class Sales_item
{
public:
// operations on Sales_item objects
double avg_price() const;
bool same_isbn(const Sales_item &rhs) const
{return isbn == rhs.isbn;}
// an equivalent way to defien this function is as follows:
// {return this->isbn == rhs.isbn;}
// read the items stored in Sales_item
void read_item();
// default constructor needed to initialize members of built-in type
Sales_item(): units_sold(0), revenue(0.0){}
// private members as before
private:
string isbn;
unsigned units_sold;
double revenue;
};
//
#include "Sales_item.h"
void keep_window_open();
int main()
{
Sales_item sale_one;
sale_one.read_item();
keep_window_open();
}
void keep_window_open()
{
cout << "\nPress any key to exit:";
getchar();
}
void Sales_item::read_item()
{
cout<<"\nThis is the items of your current Sales_item:\n";
cout<<"Its ISBN number:\t"<<isbn<<"\n"
<<endl<<"and "<< units_sold<<" of it has been sold.\n"
<<"For now, we have earned "<<revenue<<" $ in total!\n";
}
double Sales_item::avg_price() const
{
if(units_sold)
return revenue/units_sold;
else
return 0;
}