- Basic characteristics of O-O languages
- Everything is an object.
- Object-orientation is a natural way of thinking about the world and of writing computer programs.
- Objects are all around us-people, animals, plants, cars, planes, buildings, computers, etc.
- Abstractions allow us to view screen images as objects such as people, planes, trees, etc. rather than as individual dots of color.
- Abstractions allow us to think in terms of beaches rather than grains of sand, houses rather than bricks.
- All objects have attributes such as size, shape, color, weight, etc.
- All objects exhibit various behaviors. A baby cries, sleeps, crawls, walks; a car accelerates, brakes, turns, etc.
- Humans learn about objects by studying their attributes and observing their behaviors.
- Different objects can have many of the same attributes and exhibit similar behaviors.
- Comparisons can be made between babies and adults, and between humans and chimpanzees.
- Cars, trucks, little red wagons, and roller blades have much in common.
- Object-oriented programming (OOP) models real-world objects with software counterparts.
- It takes advantage of class relationships where objects of a certain class, such as a class of vehicles, have the same characteristics.
- It takes advantage of inheritance relationships, and even multiple inheritance relationships, where newly created classes are derived by inheriting characteristics of existing classes, yet contain unique characteristics of their own.
- A program is a bunch of objects telling each other what to do, by sending messages.
- Each object has its own memory, and is made up of other objects.
- Every object has a type (class).
- All objects of the same type can receive the same messages.
- Objects
- An object has an interface, determined by the class it's an instance of.
- A class is an abstract data type (or user-defined data type).
- Defining a class requires defining its interface.
- What about built-in types?
- Think of an int
- What's its interface?
- How do you "send it messages"?
- How do you make (construct) one?
- The interface is the critical part, but the details (implementation) are important too
- Users use the interface (the "public part"), the implementation is hidden by "access control".
- C libraries have always been like this, sort of:
- The library designer invents a useful struct.
- Then she provides some useful functions for the struct.
- The user creates an instance of the struct, then applies library functions to it.
- C++ uses "access specifiers": public, protected, and private to determine who can use the attribute or function.
- Two Ways of Reusing Classes
- Composition: One class has another as a "part".
- Inheritance: One class is a specialized version of another
- Polymorphism: Different subclasses respond to the same message, possibly with different actions.
- Creating and Destroying Objects
- We usually get this for free with built-in types like int or char, we just say
- With user-defined types (the ones we make), we need to be explicit about what we want:
- constructor function
- destructor function
- C++ has new and delete (similar to malloc and free in C)
- This is a very important issue! What is a memory leak?
- A compiler typically does
- preprocessing
- first pass to make parse tree
- second pass to generate code
- The result is an object module (.obj file).
- A linker produces an .exe file by
- Resolving references between compilation units (i.e., separate source files)
- Adding code from libraries
- Adding special startup code
- Building the final executable file
- In C++, variables and functions must be both declared and defined. The rules:
- A declaration tells the compiler that you intend to use a variable/function with a certain name.
- A variable declaration specifies the type (int, float, etc.) so the compiler can check your usage.
- A variable declaration doesn't allocate space for the variable.
- A function declaration specifies the function name, argument types, and return type, so the compiler can check your usage.
- A function declaration doesn't allocate space for the function code.
- A variable definition causes memory to be allocated to hold its value. This can only be done (must be done) exactly once in the entire program. Why?
- And so for functions.
- Libraries are collections of compiled function definitions.
- Library header files (.h files, or files with no extension) are collections of (uncompiled e.g., ASCII) function declarations.
- includeing a header file is a fast and painless way of providing the declarations the compiler insists on.
- The compiler is happy, since it has declarations from the .h file(s)
- The linker is happy, because there is exactly one definition of a library function.
- The linker resolves references to variables/functions that are spread across files.
Figure 1.1:
Survey of Programming Techniques; unstructured, procedural, modular, and object-oriented programming.
|
- Survey of Programming Techniques (see Fig. 1.1)
- Unstructured programming.
- Simple sequence of command statements.
- Operates directly on global data.
- Not good for large programs.
- Repetitive statement segments are copied over.
- The repetitive sequences extracted and named so that they can be called and values returned leads to the idea of procedures.
- Procedural programming.
- Combines returning sequences of statements into one function.
- Procedure calls are used to invoke procedures.
- Programs are now more structured.
- Errors are easier to detect.
- Combining procedures into modules is the next logical extension.
- Modular programming.
- Procedures with common functionality are grouped into modules.
- Main program coordinates calls to procedures within modules.
- Each module has its own data and isolated for other modules.
- Object-oriented programming.
- Data and the functions that operate on that data are combined into an object.
- Programming is not function based but object based.
- Objects are base on three basic ideas: Encapsulation, Inheritance and Polymorphism.
Subsections
2004-07-29