Download presentation
Presentation is loading. Please wait.
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 scandir 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
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.h> what is a typedef and how is it used? make depend for Appointment/ostream? changes to Day force recompile for appointment clients?
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.cc #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.cc #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)
4
#include “foo.h” will be needed in .cc file, e.g., foo.cc and bar.cc
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.cc (handle-body idiom) factory: inheritance hierarchy, ABC
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 (see map example) 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
C++ idioms What happens with the statement myDay = d; ?
assignment is memberwise unless operator = overloaded copy constructor used in passing parameters by value If you need one of: destructor, assignment operator, copy constructor, you need all of them heuristic only: managing resources other than memory preventing objects from being copied what about non-copyable state, e.g., stream In assignment operator, watch for self-assignment Study implementation of string/vector
7
copy constructor Used for “first-time” creation Used for pass-by-value
Date d(1,1,2000); Date copy(d); Used for pass-by-value DoStuff(Date d); //… Date first(1,1,2000); DoStuff(first); what about use of myLength in code as opposed to length()? Template <class Item> Vector(const Vector<Item> & vec) // precondition: Item supports assignment // postcondition: return copy of vec { // allocate storage myList = new Item[myLength=vec.myLength]; assert(myList != 0); // copy elements for(int k = 0; k < vec.myLength; k++) { myList[k] = vec.myList[k]; }
8
The code doesn’t run Test classes in isolation, not as part of the complete program each class should (ideally) have its own test suite day.cc, testday.cc, wagreader.cc, testwagreader.cc, ... Use the debugger: gdb, ddd debugger is much faster than edit/compile/debug important for pointers to find out where the problem is run, break at, where, step (into), next (statement) Never define a variable, especially a pointer, without giving it a value Ask questions, use your consultant Post salient parts of problem, not “my code doesn’t work”
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.