📄 chapter1.htm
字号:
<head>
<title>Chapter 1 Objects and Components</title>
</head>
<body>
<h1>Objects and Components</h1>
<p>
Object oriented programming offers significant advantages when building large
programs. Unfortunately, object oriented compilers are large and complicated, and the
language generated is also usually quite large and complicate. Worse yet, object oriented
programs are often large and complicated when compared to an equivalent program
written in a procedural language. It is not expected that we will see a true object oriented
compiler that will work for a small microcontroller until there is serious improvement in
both the microcontroller technology and the compiler technology. These small machines
are held back by limited memory space for both program and data as well as limited on-
chip resources that make them unsatisfactory destinations for code created by a full scale
object oriented compiler.
<p>
The embedded systems community is experiencing an expansion of program size
and requirements. There has been some attempt to address these growing needs with the
construction of larger and more powerful microcontrollers. Motorola, for example, has
developed the M68HC16 and the M68300 families of microcontrollers to address the
needs of the community for larger machines. Even with the large memory spaces and the
vast on-chip resources of these machines, there is still the practical need to minimize the
system requirement for additional memory. Therefore, code created by an object oriented
compiler would work on a system based on these chips, but the total system would be
more complicated and more expensive than the equivalent system prepared with a
procedural language.
<p>
Regardless, there are still serious advantages that can be obtained through the use
of an object oriented approach to programming any large project. In the course of this
book, it is intended to show an object oriented approach that can be used with C. Many
of the advantages of object oriented programming can be had, but there are also several
important capabilities found with any object oriented language that cannot be obtained
directly from C. Let us therefore, talk of the code that we generated as being object
oriented like rather than object oriented.
<p>
The ideal code language for many embedded systems is C. C produces tight code
and it is really RAM efficient. It also can produce quite fast code. However, C does not
have many of the features to produce true object oriented code. Most of the ideas
associated with an object can be obtained in C. Those important features that are lost
cause the object oriented code and the objects created in C to deviate from the ideals that
we all have for an object oriented program. I am going to call these objects, written in C,
either components or objects interchangeably.
<p>
Object oriented programming is semantic in nature. Therefore, a reasonable
approach to examine our object oriented like programming technique is to look at the
semantics of true object oriented programming and determine the differences between
what we propose here and an object oriented program. The following few pages are
going to be a simple dictionary that shows important concepts of an object oriented
programming style along with discussions of our ability to implement these ideas with the
C programming language.
<p>
Most dictionaries order their contents in some sense. Usually the ordering is
alphabetical. The ordering here is structural rather than alphabetical. It is probably a
good idea to read through and understand the word definitions that follow because these
words will be used throughout the text. The meanings found here are the meanings
associated with these words.
<h2><a name="class">Class</a></h2>
A class is a template for an object. Classes are constructed of structures and are
therefore types. These structures will be given type names with the typedef keyword, and
they will be treated within a program the same way as any of the usual language defined
types.
<h2><a name="members">Members</a></h2>
The elements that make up a structure are called the structures or class members.
<h2><a name="attribute">Attribute</a></h2>
An attribute is a class member that is a variable. Class attributes are usually hidden
from the remainder of the program. Since C has no hiding provision, the class attributes in
a C program cannot be truly hidden from the remainder of the program. It is extremely
important, however, that a class attributes be untouched directly by the remainder of the
program. Therefore, programmers using a class must agree to access any class attributes
through the class methods only.
<h2><a name="method">Method</a></h2>
A method is a member function associated with a class. Methods are usually used
to access class attributes, but they are also used to accomplish any functional operations
that are related to their class. Since, these functions are members of the class, they will
usually appear as pointers to functions of the class rather than function names.
<h2>Member Functions</h2>
See methods above.
<h2><a name="constructor">Constructor</a></h2>
A constructor is a function associated with a class. The constructor is executed
automatically when a class comes into scope in an object oriented language. This code
creates and initializes an instance of the class type. In C, however, there is no provision
for this automatic execution. Therefore, it is necessary for the program to execute the
class constructor every time a class is created. The constructor in C returns a pointer to
an object that is an instance of the class and class members can be accessed through this
pointer.
<h2><a name="destructor">Destructor</a></h2>
The destructor is a function associated with a class. In an object oriented
language, the destructor is executed whenever an object goes out of scope. The
destructor deletes an object and frees memory in which the object is stored. C does not
provide for automatic execution of the destructor; therefore, the program must execute
the destructor of an object whenever an instance of the object is no longer needed.
<h2><a name="encapsulation">ENCAPSULATION</a></h2>
Encapsulation is a concept that describes the enclosing of member function and
attributes into a single unit that is a type. With the protection afforded by private and
protected modes within the class formed by an object oriented language, the only access to
the attributes of the class is through the class members. Since C does not provide the
protection of an object oriented language, encapsulation must be enforced by an
agreement between programmers to access class attributes only through class methods.
<h2><a name="private">Private</a></h2>
Private is a protection mode provided by an object oriented language like C++.
Class members that are private can be accessed only through class methods.
<h2><a name="protected">Protected</a></h2>
Protected is another protection mode provided by an object oriented language like
C++. Class members that are protected can be accessed through class methods and
through class methods of an inherited class.
<h2><a name="public">Public</a></h2>
Public is a protection mode in which any public attribute or method can be
accessed from anywhere in the program. All attributes and methods found in a C program
will be public automatically, and it is up the programmers to agree to treat any variables
that should not be public as having a higher level of protection.
<h2>Friend </h2>
A friend function is a function that can access any attribute or method, protected
or public, in a class.
<h2>Function Overload</h2>
Function overload is an object oriented concept that permits several functions to
have the same name. The correct choice for the function to be executed at any point in
the program is made by the compiler. Each function must have a unique argument
signature. The argument signature is the pattern of argument types, and the compiler will
choose the correct version of the function to execute even though there are several
versions of functions with the same names. C has no provision for function overload.
<h2>Operator Overload</h2>
The programmer is able to create new types at will in an object oriented program.
It is desirable that the programmer be able to perform operations on the new types with
the usual language operators. An object oriented language provides for operator overload
to implement this feature. With operator overload, the programmer can specify the
operation to be executed on the new types. The compiler will use the type information
inherent in the program to determine which operator code to use when it encounters an
overloaded operator. Operator overload is not available in C.
<h2><a name="inheritance">INHERITANCE</a></h2>
Inheritance is an object oriented language concept that allows a new class to be
created that will have access to some of the attributes and methods of an existing class.
This new class can implement additional features that were not present in the older class.
Usually inheritance can be implemented at link time and as such does not require access to
the source code of class being inherited from. Inheritance is an object oriented feature
that can be implemented completely in the C language.
<h2><a name="base">Base class</a></h2>
<h2><a name="super">Super class</a></h2>
<h2><a name="parent">Parent class</a></h2>
These names are assigned synonymously to a class from which another class will
be derived. In this text, the term base class is usually used.
<h2><a name="derived">Derived class</a></h2>
<h2><a name="child">Child class</a> </h2>
These classes are those created by inheritance from a base class.
<h2><a name="abstract">Abstract class</a></h2>
An abstract class is a class that is a base class only. No instance of an abstract
class will ever be created, however, an abstract class will be a base class from which other
derived classes will be inherited.
<h2><a name="instantiation">Instantiation</a></h2>
Instantiation is the code operation required to create an instance of a new object.
The class is a template of an object, and an object is an instance of a class. Therefore,
execution of the constructor will create an instance of the object.
<h2><a name="object">Object</a></h2>
An object is an instance of a type defined by a class.
<h2><a name="component">Component</a> </h2>
An object.
<h2><a name="polymorphism">POLYMORPHISM</a></h2>
Polymorphism means many forms. Cases will be found where specific different
methods with a common name and similar operations in several inherited classes can exits.
Under normal circumstances, use of such a set of functions is impossible because at link
time, all class method normally must be linked to any function that calls it. Here, the
particular method executed depends upon the object being accessed by the program. This
choice is not known at link time. Polymorphism is the mechanism that allows the correct
function to be chosen at run time. Polymorphism can be implemented in C.
<h2><a name=virtual">Virtual function</a></h2>
A virtual function is a function that is linked at run time rather than at link time.
Virtual functions are implemented in C.
<h2><a name="header">Header</a>, <a name="definition">definition file</a></h2>
A header or definition file usually contains the class definition, all of the function
prototypes required for the class, any macro definitions needed to implement the class, and
linkage, if any, to base classes from which the class is inherited. Header files usually
contain no executable code beyond macro definitions. The header file must be included in
any program that will use the class.
<h2><a name="implementation_file">Implementation file</a></h2>
An implementation file contains the code needed for all class methods, constructor,
destructor, and hidden functions associated with a class. The implementation file is usually
compiled separately and linked with the program.
<p>
In the above glossary, you will find three especially important concepts that form
the basis for all object oriented programming. These three, encapsulation, inheritance and
polymorphism, are identified by all capital letters. It is upon these three important
concepts that object oriented programming is based. You will note that both inheritance
and polymorphism can be implemented in C, and encapsulation is not enforced by the C
language. It is possible to hide functions that should be private and prevent their use from
outside of the class methods.
<p>
Let us look at what a class and objects instantiated from this class is. When we
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -