Just a few notes on object oriented programming.
Good article by Bjarne Stroustrup, inventor of C++ (pdf): What
is Object-Oriented Programming?
Summary
- Difference between enable and support
- Enable: some feature is possible, but not easy (i.e., type
checking in C)
- Support: feature is made easy to use by language
- Enabling doesn't really count -- support does
- Procedural programming
- Decide which procedures you want; use the best algorithms you
can find.
- Data hiding/modules
- Decide which modules you want; partition the program so that data
is hidden in modules.
- Modules aren't as good as built-in types; must handle own memory
management
- Data abstraction
- Decide which types you want; provide a full set of operations
for each type.
- Like built-in types (called user defined types or abstract data
types)
- Problems with data abstraction
- Inflexible: once class declared, does not change
- Object oriented
- Decide which classes you want;
provide a full set of operations for each class;
make commonality explicit by using inheritance.
- Don't pile everything into base class -- let derived classes extend
it, provide specifics
- Virtual functions: specify interface, but not implementation (derived
class provides implementation)
- OO programing usefulness varies
- Good for graphics
- Not great for arithmetic... if not useful, use data abstraction
- Can be hard to extract commonality, depends on design of system
- Notice that each class has functions that it defines, and act
on it.
- Different from making an outside function to modify an object
- Lesser details about OO and C++
- Initialization and construction in one step
- Destructor to deallocate, if necessary (a non-trivial type)
- Assignment, copy constructors, preventing assignment
- Template class
- Family of types; create class to use any type
- Error handling (throwing/catching errors)
- Type conversions (implicit, explicit)
- Iterators
- Loop through data structure without knowing its details
- Iterator is a separate object, initialized with the object to
iterate over
- More general than having a "current" variable inside
the class
- Class derivation (subclassing) important
- Avoid having a type field (int type...) in the class, and then
having to do a switch statement. Inelegant, not-scalable.
- Multiple inheritence
- Encapsulation: protected, friend
|