リロードオペレータ学習1

1245 ワード

#include<iostream>

class Sales_item
{
	friend std::istream& operator>>(std::istream&, Sales_item&);
	friend std::ostream& operator<<(std::ostream&, const Sales_item&);
private:
	int a;
public:
	Sales_item(int b):a(b){};
	Sales_item():a(0){};
	//Sales_item operator +(const Sales_item&);
	Sales_item operator +=(const Sales_item& item1);
};

Sales_item operator+(const Sales_item& item1,const Sales_item& item2)
{
	Sales_item temp(item1);
	temp += item2;
	return temp;
}

Sales_item Sales_item::operator+=(const Sales_item& item1)
{
	Sales_item temp(*this);
	temp.a += item1.a;
	return temp;
}

std::istream& operator>>(std::istream& is, Sales_item& item)
{
   is >> item.a;
   return is;
}

std::ostream& operator<<(std::ostream& os, const Sales_item& item)
{
   os << item.a;
   return os;
}
int main()
{
   Sales_item a(11),b(12),c;
   std::cout << a + b << std::endl;
}