#include #include /*************************************************** * Default arguments: addresses * * Suppose we are writing a function that prints a * Canadian address. * * Some information is required: * * - the street number; * - the street name. * * Other information may have useful default values. * For example, suppose most of the addresses we are * printing are in Waterloo, Ontario, Canada. * * We can therefore use the default arguments * * apartment = "" * city = "Waterloo" * province = "Ontario" * country = "Canada". * * The empty string indicates that there is no * apartment number. * * * OMITTING DEFAULT ARGUMENTS * * Default arguments can only be omitted from the * right. * * Thus, * * print_address( "599", "Winterberg Walk" ); * * uses all four default arguments. * * Similarly, * * print_address( "499", "Albert St.", "10" ); * * specifies an apartment number but uses the * default city, province, and country. * * However, suppose we want an address in * St. Catharines. We cannot simply skip the * apartment argument. * * Instead, we must explicitly provide the empty * string: * * print_address( * "99", "Belton Blvd.", "", "St. Catharines" * ); * * The default arguments for Ontario and Canada can * still be omitted because they are to the right. ***************************************************/ // Function declarations int main(); void print_address( std::string const number, std::string const street, std::string const apartment = "", std::string const city = "Waterloo", std::string const province = "Ontario", std::string const country = "Canada" ); // Function definitions int main() { print_address( "99", "Belton Blvd.", "", "St. Catharines" ); std::cout << std::endl; print_address( "99", "Latenda Pl.", "", "Guelph" ); std::cout << std::endl; print_address( "499", "Albert St.", "10" ); std::cout << std::endl; print_address( "599", "Winterberg Walk" ); std::cout << std::endl; print_address( "199", "Wissler Rd.", "300" ); std::cout << std::endl; print_address( "699", "Coldstream Dr." ); std::cout << std::endl; print_address( "299", "Hages Ln.", "", "Prospect Village", "Nova Scotia" ); return 0; } /*************************************************** * print_address * * Print an address using the supplied arguments. * * Notice that the default arguments appeared in * the function declaration. They are not repeated * in the function definition. ***************************************************/ void print_address( std::string const number, std::string const street, std::string const apartment, std::string const city, std::string const province, std::string const country ) { if ( apartment != "" ) { std::cout << apartment << "-"; } std::cout << number << " " << street << std::endl; std::cout << city << ", " << province << std::endl; std::cout << country << std::endl; }