int operator-(Time a, Time b)
{ return a.seconds_from(b);
}
Time now;
Time morning(9, 0, 0);
long seconds_elapsed = now - morning;
long seconds_elapsed = operator-(now, morning);
Syntax 17.1 : Overloading Operator Definition return_type operatoroperator_symbol(parameters)
|
some_return_type operator+(Time a, Time b);
Time operator+(Time a, int sec)
{ Time r = a;
r.add_seconds(sec);
return r;
}
Time now;
Time later = now + 60; /* 60 seconds later */
bool operator==(Time a, Time b)
{ return a.seconds_from(b) == 0;
}
bool operator!=(Time a, Time b)
{ return a.seconds_from(b) != 0;
}
bool operator<(Time a, Time b)
{ return a.seconds_from(b) < 0;
}
ostream& operator<<(ostream& out, Time a)
{ out << a.get_hours() << ":";
if (a.get_minutes() < 10) out << "0";
out << a.get_minutes() << ":";
if (a.get_seconds() < 10) out << "0";
out << a.get_seconds();
return out;
}
really meanscout << now << "\n";
that is(cout << now) << "\n";
operator<<(cout, now) << "\n";
istream& operator>>(istream& in, Time& a)
{ int hours;
int minutes;
int seconds;
in >> hours >> minutes >> seconds;
a = Time(hours, minutes, seconds);
return in;
}
++x;
x++;
int i = 0;
int j = 0;
vector<double> s(10);
double a = s[i++]; /* a is s[0], i is 1 */
double b = s[++j]; /* b is s[1], j is 1 */
void operator++(Time& a) /* prefix operator */
. . .
void operator++(Time& a, int dummy) /* postfix operator */
bool operator==(Iterator a, Iterator b)
bool Iterator::operator==(Iterator b) const
string Iterator::operator*() const
string Iterator::operator*() const
{ assert(position != NULL);
return position->data;
}
bool Iterator::operator==(Iterator b) const
{ return position == b.position;
}
void Iterator::operator++(int dummy) const
{ assert(position != NULL);
return = position->next;
}
bool Iterator::operator!=(Iterator b) const
{ return !(*this == b); // calls operator ==
}
class Department {
...
private:
string name;
Employee* receptionist;
};
Department::Department(string n, Employee)
{ name = n;
receptionist = new Employee(e.get_name(), e.get_salary());
}
/* second constructor */
Department::Department(string n)
{ name = n;
receptionist = NULL;
}
Department::~Department()
{ delete receptionist;
}
Syntax 17.2 : Destructor Definition Class_name::~Class_name()
|
{ Department dept;
...
} // dept.~Department() automatically invoked here
...
Department* p = new Department(...);
...
delete p; // p->~Department() automatically invoked here
Department qc("Qualitiy Control", Employee("Tester, Tina", 50000));
Department dept("Shipping", Employee("Hacker, Harry", 35000));
dept = qc;
Department& Department::operator=(const Department& b)
{ if (this != & b)
{ name = b.name;
delete receptionist;
if (b.receptionist == NULL)
receptionist == NULL;
else
receptionist = new Employee(b.receptionist->get_name(),
b.receptionist->get_salary());
}
return *this;
}
z = y = x;
Department dept = qc;
Department dept(qc)
Department::Department(const Department& b)
{ name = b.name;
if (b.receptionist == NULL)
receptionist = NULL;
else
receptionist = new Employee(b.receptionist->get_name(),
b.receptionist->get_salary());
}