
int operator-{Time a, Time b)
{
return a.seconds_from(b);
}
Time now; Time morning(9, 0, 0); long seconds_elapsed = now - morning;
|
Syntax 17.1 : Overloading Operator Definition return_type operatoroperator_symbol(parameters)
{
statements
}
|
some_return_type operator+(Time a, Time b);
Time operator+(Time a, int sec)
{
Time r = a;
r.add_seconds(sec);
return r;
}
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 << not) << "\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()
{
statements
}
|
{
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());
}
List::~List()
{
free();
}
void List::free()
{
while(begin() != end())
erase(begin());
}
List& List::operator=(const List& b)
{
if (this != &b)
{
free();
copy(b);
}
return *this;
}
List::List(const List& b)
{
first = NULL;
last = NULL;
copy(b);
}
vector<int> v_i; vector<double> v_d; vector<Employee> v_e;
template<typename T>
class Pair
{
public:
Pair(T a, T b);
T get_first() const;
T get_second() const;
private:
T first;
T second;
};
|
Syntax 17.3 : Template Class Definition template<typename type_variable>
class class_name
{
features
};
|
template<typename T>
Pair<T>::Pair(T a, T b)
{
first = a;
second = b;
}
template<typename T>
T Pair<T>::get_first() const
{
return first;
}
template<typename T>
T Pair<T>::get_second() const
{
return second;
}
|
Syntax 17.4 : Template Member Function Definition template<typename type_variable>
return_type class_name<type_variable>::function_name(parameters) constopt
{
statements
}
|
template<typename T>
class List
{
public:
List();
void push_back(T s);
void insert(Iterator<T> pos, T s);
void erase(Iterator<T> pos);
Iterator<T> begin();
Iterator<T> end();
private:
Node<T>* first;
Node<T>* last;
}
template<typename T>
Iterator<T> List<T>::begin()
{
Iterator<T> iter;
iter.position = first;
iter.position = last;
return iter;
}
list<string>::iterator pos = staff.begin();
vector<double>::iterator p = a.begin(); list<string>::iterator q = b.begin();
class List
{
...
class Iterator;
...
};
|
Syntax 17.5 : Nested Class Declaration class Outer_class_name
{
...
class Nested_class_name
{
...
};
...
}
|
List::Iterator::Iterator()
{
...
}
string List::Iterator::get() const
{
...
}
std::map /* standard library template map */ acme::map /* another class called map */
namespace acme
{
class map
{
...
};
void draw(map m);
}
namespace acme
{
class maze
{
...
}
};
|
Syntax 17.6 : Name Space Definition namespace name_space_name
{
feature1
feature2
...
featuren
};
|
ACME_Software_San_Jose_CA_US::map
namespace acme = ACME_Software_San_Jose_CA_US
|
Syntax 17.7 : Name Space Alias namespace alias_name = name_space_name;
|
double future_value(double initial_balance, double p, int n)
{
return initial_balance * pow(1 + p / 100, n);
}
double future_value(double initial_balance, double p, int n)
{
if (p < 0 || n < 0) return 0;
return initial_balance * pow(1 + p / 100, n);
}
double future_value(double initial_balance, double p, int n)
{
assert(p >= 0 && n >= 0);
return initial_balance * pow(1 + p / 100, n);
}
double future_value(double initial_balance, double p, int n)
{
if (p < 0 || n < 0)
{
logic_error description("illegal future_value parameter");
throw description;
}
return initial_balance * pow(1 + p / 100, n);
}
|
Syntax 17.8 : Throwing an Exception throw expression;
|
try
{
code
}
catch (logic_error& e)
{
handler
}
|
Syntax 17.9 : Try Block try
{
statements
}
catch (type_name1 variable_name1)
{
statements
}
catch (type_name2 variable_name2)
{
statements
}
...
catch (type_namen variable_namen)
{
statements
}
|
while (more)
{
try
{
code
}
catch (logic_error& e)
{
cout << "A logic error has occurred "
<< e.what() << "\n"
<< "Retry? (y/n)";
string input;
getline(cin, input);
if (input == "n" more = false;
}
}
class FutureValueError : public logic_error
{
public:
FutureValueError(const char reason[]);
};
FutureValueError::FutureValueError(const char reason[])
: logic_error(reason){}
if (p < 0 || n < 0)
throw FutureValueError("illegal parameter");
try
{
code
}
catch (FutureValueError& e)
{
handler1
}
catch (logic_error& e)
{
handler2
}
bool Product::read(fstream& fs)
{
getline(fs, name);
if (name == "") return false; // end of file
fs >> price >> score;
if (fs.fail())
throw runtime_error("Error while reading product");
string remainder;
getline(fs, remainder);
return true;
}
void process_products(fstream& fs)
{
list<Product> products;
bool more = true;
while (more)
{
Product p;
if (p.read(fs)) products.push_back(p);
else more = false;
}
do something with products
}
Product* p = new Product();
if (p->read())
{
...
}
delete p; // never executes if read throws an exception
Product* p = NULL;
try
{
p = new Product();
if (p->read())
{
...
}
delete p;
}
catch(...)
{
delete p;
throw;
}
void process_products(fstream& fs) throw (UnexpectedEndOfFile, bad_alloc)
void process_products(fstream& fs) throw ()
|
Syntax 17.10 : Exception Specification return_type function_name(parameters) throw (type_name1, type_name2, ..., type_namen)
|