(operator overloading) , . , , :
D = A + B * C
, ++ . - . , , , .
, -. , , , . , .
, , . , , , -, .
:
● . - /;
● .* - / ;
● :: - ;
● ?: - .
, .
// !
bool CoffeeMachine::operator! ()
{
if(GetGrainWeidhtLeft()<4)
return 0;
return 1;
}
13. . . /.
● ==:
class Date
{ //
public:
//
bool operator == (const Date & d) const;
};
//
|
|
inline bool Date::operator == (const Date & d) const
{
// ,
return m_Year == d.GetYear() &&
m_Month == d.GetMonth() &&
m_Day == d.GetDay();
}
● ==:
//
inline bool operator == (const Date & d1, const Date & d2)
{
return d1.GetYear() == d2.GetYear() &&
d1.GetMonth() == d2.GetMonth() &&
d1.GetDay() == d2.GetDay();
}
. , private- , , , . , , , .
<<, >>
, , C, . - . , , - , , .
, , .
. , , -, . :
std::ostream& operator << (std::ostream& o, const Date & d)
{
o << d.GetYear() << / << d.GetMonth() << / << d.GetDay();
return o;
}
, .. <<, , .
, - , , :
std::istream & operator >> (std::istream & i, Date& d)
{ //
char buf[ 100 ];
i >> buf;
d = Date(buf); // ,
return i;
}
int main ()
{ Date d;
std::cin >> d; //...
}