Computer Science Using C++
Ch. 12 Notes
Objective #1: Understand the difference between procedural programming and
object-oriented programming (OOP).
- For the first several decades of computer programming, languages were written
to support a procedural paradigm of programming. Programs were mainly
composed of functions that performed specific tasks to process data. Programmers
were able to manipulate the data by using global variables and passing parameters.
As long as the programmer understood how the data was stored and how the algorithms
worked, everything worked well. However, sometimes data can be corrupted due
to bad logic or input if a programmer forgets certain details.
- Procedural programming is still useful and will probably always be
used in many situations and languages. However the popularity of the object-oriented
programming (OOP) is increasing as hybrid languages like C++ and pure
OOP languages like Java & Smalltalk become used more widely.
- The object-oriented paradigm of programming is based on the idea that a
program is really a collection of objects, not functions. Each object is contains
its own members consisting of methods and member variables
(often called "data") . Methods perform the processes
that functions perform in procedural programming however the fact that the
data is also part of the object protects its integrity.
Objective #2: Understand the principles of object-oriented programming.
- Object-oriented programs are built with objects. Objects contain
members which include methods and variables (also called
member variables or data). (Remember, we studied member variables
as part of structs earlier.) The methods detail processes that occur with
the data that is stored in the object's variables. Usually, the variables
are designated as private and cannot be directly referenced by programmers
who use the object. This principle of "hidden" data is called encapsulation.
However, a programmer can use an object's public methods in order to
indirectly change its underlying, private data. Written into the object's
definition (known as a class) are rules and constraints that keep programmers
from using the methods inappropriately.
- When using an object's methods to manipulate data, messages are exchanged
with the object. You could say that calling an object's method is like sending
a message.
- The definition for an object is called a class. You can crudely compare
a class to a basic C++ data type. But it is a The class prescribes instructions
to the compiler regarding memory allocations for its object. When you actually
declare an object in a program, its called instanciating the object.
The object (essentially a variable) is called an instance of the class.
Objective #3: Learn how to read classes and use objects in C++.
Objective #4: Understand how objects help programmers reuse code.
- It is very efficient to build and use classes in object-oriented programming.
The fact that objects can be defined and created and then passed onto (or
sold) to other programmers encourage the careful and thorough development
of more classes. Reusing code in this way is called reusability. For
example, you can use the Microsoft Foundation Classes (MFC) to manipulate
common objects such as dialogue boxes, toolbars, and windows that are considered
to be standard to the Windows GUI (graphical user interface.) So, actually
programming (at least in the desktop environment) is becoming more high-level
than ever. One doesn't have to master the Assembly language and obscure hardware
technical requirements in order to write a commercial, user-friendly program.
However, you may have to pay a few dollars to use some classes.
Objective #5: Understand containment and inheritance in C++ object-oriented
programming.
- Containment is the relationship between certain objects in classes.
An object can use another class to instanciate objects within itself. For
example, a programmer could use the ClassPeriod class to instanciate 8 ClassPeriod
objects within one SchoolDay object. This arrangement is also referred to
as a has-a relationship because the SchoolDay object has a ClassPeriod object.
- Inheritance allows programmers to reuse code. If you have written
one class and later need to develop another class that would have a lot of
the same properties, you can use inheritance to extend the common properties
into the new class. This prevents you from having to separately code the common
properties a second time. The relationship between objects of these two classes
is called an is-a relationship. The original class is called the parent class
and the second one with the derived (or inherited properties) is called the
child (or derived) class. If your purpose for creating a class is to extend
it into several different child classes, you can call it a base class.
- Multiple inheritance allows objects to inherit properties from multiple
objects. In that case, an object could have two or more parent objects. Multiple
inheritance is not allowed in some pure OOP languages and is rarely used.
- Note that inheritance is not included on the AP Computer Science exam but
is still important with regard to OOP.
Computer Science Using C++ Home Page
| Mr. Minich's Wyo Home Page