cs31 lecture 6&7

Cpp contains the implementation of functions libdivhh

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: definition: a library that provides functions to Precise divide numbers divide x Organization: libdiv.cpp: contains the implementation of functions libdiv.hh: the header containing the prototype of the functions README: a text file explaining some things about the lib. README: 28 libdiv.cpp iint libdiv_divint_base(int x, int y) { nt return x / y; } float libdiv_divfloat_base(float x, float y) { return x / y; } 29 libdiv.hh #ifndef _LIBDIV_LIBDIV_HH # define _LIBDIV_LIBDIV_HH int libdiv_divint_base(int x, int y); float libdiv_divfloat_base(float x, float y); #endif 30 A word about headers x Use the #ifdef / #define / #endif trick to avoid multiple Use inclusion inclusion x Be careful: it must define a unique name for the whole Be project (good habit: _libraryname_filename_extension) project x A common pitfall: multiple inclusions because the common protection is missing! protection 31 Using the library: foo.cpp #include “libdiv.hh” int main() { int divres = libdiv_divint_base(42, 51); int } 32 A few observations x If the prototype is missing, the compiler yields a warning If (not an error) with many compilers (not The missing prototype may be “replaced” with dummy int return The type and arguments type x If the function is used before the header is included, then If the above error arise (same as missing header) the x Multiple headers may define the same function: be careful! x The same header can be included multiple times The (recommend to correctly protect it) (recommend 33 Namespace x Namespaces roughly groups together functions under a Namespaces common name prefix common x Good programming habit: use a specific namespace for a Good library library x Be careful: multiple different namespaces can define the Be same function same 34 Example namespace libdiv { int libdiv_divint_base(int x, int y) { return x / y; } int } int main() { int std::cout << libdiv::libdiv_divint_base(12, 13) << std::cout; std::cout } 35 More on namespaces x Syntax for declaring a namespace and functions in it: namespace <name> { [function prototype/implementation] } x Syntax for using a function from a given namespace: <name>::<function call> x Shortcut: using keyword using namespace <name>; x Alternative syntax for function implementation: <return type> <name>::<function name>([type1 arg1] [,type2 arg2]) { <return [instructions] } [instructions] 36 Examples 37 Even more on namespaces x Namespaces can be nested namespace foo { namespace bar { namespace void blob(); void } } foo::bar::blob() x Can split the definition of the namespace into multiple Can regions x Can go to the “root” namespace with ::myfuc() 38 Potential issues x Multiple namespaces can define the same function x When using the using directive, ambiguity/unexpected When behavior may happen Example: std defines a function you have defined in your own Example: namespace namespace x Having multiple using directives 39 Another example namespace libdiv { int divint_base(int x, int y) { return x / y; } int } namespace libdiv { int divint_fast(int x, int y) { … } int } using namespace libdiv; void test() { divint_fast(4,5); } 40 Using glo...
View Full Document

This note was uploaded on 04/03/2014 for the course CS 31 taught by Professor Melkanoff during the Fall '00 term at UCLA.

Ask a homework question - tutors are online