programming in the small medium large
play

Programming in the small, medium, large You must be able to write - PowerPoint PPT Presentation

Programming in the small, medium, large You must be able to write itoa to be able to write hyperwag You must understand the difference between a pointer and a reference to write hyperwag You must know about templates, copy constructors,


  1. Programming in the small, medium, large ● You must be able to write itoa to be able to write hyperwag ● You must understand the difference between a pointer and a reference to write hyperwag ● You must know about templates, copy constructors, arrays, pointers to functions to write igloo (whatever it is) ● You must know about built-in arrays and c-style strings to be able to implement a string and vector class, or be able to cope without them ● You must know about factories and proxies to design and implement using object-oriented principles ● You must know about patterns to communicate 6. 1 Duke CPS 108

  2. Classes, compilers, dependencies #include <string> #include “day.h” typedef string TimeRange; class ostream; class Appointment { public: TimeRange duration(); void print(ostream & output); private: Day myDay; }; ● why use class ostream instead of #include <stream> ● what is a typedef and how is it used? ● make depend for Appointment/ostream? ● changes to Day force recompile for appointment clients? 6. 2 Duke CPS 108

  3. .h guidelines, the preprocessor in action ● minimize #includes in every .h file ➤ avoid circular dependencies ➤ avoid re-compile by minimizing dependencies ● class Foo in foo.h, class Bar in bar.h, client foobar.cpp #ifndef _FOO_H #ifndef _BAR_H #define _FOO_H #define _BAR_H #include “bar.h” #include “foo.h” class Foo class Bar { { Bar getBar(); Foo getFoo(); // from foo.cpp #include “bar.h” #include “foo.h” void Foo::doStuff(const Bar & b)... ● Avoid #includes, use forward references, sometimes you must do this as shown above (even if you don’t want to) 6. 3 Duke CPS 108

  4. #include “foo.h” ● will be needed in .cpp file, e.g., foo.cpp and bar.cpp ● using pointers and references in .h files minimizes dependencies ➤ minimize recompiles when .h changes ➤ loose coupling: avoid implementation dependencies when possible ● avoid letting implementation leek into public view ➤ what about private section? ➤ opaque pointer: FooImpl * myImpl; • implementation of FooImpl is hidden, class can be implemented in foo.cpp (handle-body idiom) ➤ factory: inheritance hierarchy, ABC (more later) 6. 4 Duke CPS 108

  5. Has-a vs. has-a-pointer to ● benefits of string * myName vs string myName ? ● downside of using pointers? ● responsibilities for memory management, garbage collection ➤ it that news, deletes, not always possible ➤ use proxy class, stands in for pointer (Pstring) ➤ deletes cause bugs, introduce destructors into the system iteratively, chasing one bug at a time ● pointers and references are the same size, both allow one variable to refer to memory created/stored elsewhere ➤ reference values bound on construction, no change ➤ pointer values can be changed 6. 5 Duke CPS 108

  6. The best IO is no IO ● See the programs wcget.cpp , wcplus.cpp , wcplus2.cpp ➤ if you only need to count words, avoid storing them ➤ if you need to store words what can you do? • Strings are wrappers around char * strings (later) • Strings can be “smart” use pool of constants to save storage, use lazy copy/ assignment aka copy on write ➤ What interface do you program to? ● The Adapter pattern (aka Wrapper) ➤ Use when interface doesn’t match what you need, e.g., Init/Next iterator used in STL context (iteradapter.h) ➤ What are templated classes, how do they work? 6. 6 Duke CPS 108

Recommend


More recommend