MATLAB forces very little on us in the way of requirements. This is both good and bad. To the good, it means that we have a lot of freedom to make the implementation match our particular need. It also means that we do not have to devote a lot of time toward learning an intricate set of requirements, nor do we have to devote effort toward implementing the requirements with our code.
To the bad, it means we must blaze our own trail without the benefit of requirements to keep us on track. Loose requirements provide enough rope to hang ourselves. It is too easy to weave a path that is difficult to maintain. In this chapter, we will learn how to meet requirements in a way that supports the needs of the group of eight. This helps keep MATLAB object-oriented programming on the straight and narrow.
A. VARIABLES, TYPES, CLASSES, AND OBJECTS
In every specialty, there are certain words that carry special meaning. At first glance, the sheer number of special words associated with object-oriented programming appears overwhelming. The sad fact that these words are sometimes misused does not help the situation. An additional burden comes in understanding slight differences among words that appear to be describing the same thing. Fortunately, mastering the vocabulary is not difficult. Most of the differences are anchored to the normal programming vocabulary.
In discussing object-oriented programming, the words class and object seem to suffer the most abuse. When you look closely at what these words actually represent, it is easy to understand why. After we carefully define both words, you will be able to follow discussions where the language is sloppy. This knowledge will also allow you to determine the competency of self professed experts.
The easiest way to explain the differences between class and object is to relate them to things you probably already know. Look at the MATLAB command-line listing provided in Code Listing.
1. First, let me reassure you. If the command syntax and results in Code Listing are familiar, you stand an excellent chance of taking full advantage of MATLAB object-oriented programming.
Look carefully at the information displayed by the whos command and you will notice, perhaps for the first time, a heading titled Class . I hope you find this particular word choice very interesting. You might complain that char and double are not classes but rather types. There’s no cause for alarm. In this particular context, class and type mean almost the same thing. Class is a slightly more specific term, one with special meaning to object-oriented programmers. In fact, the connection between class and type is so close that the term user-defined type is often used as a substitute for class.
You might reasonably wonder why the object-oriented pioneers felt the need to coin a new term. The short answer is that class represents a new category of a variable’s type in the same vein as array, cell, or structure. When we identify a variable as a double, its type attaches certain expectations to the variable. A class is more complicated than one of the simple types like double, char, or integer, but it still represents a type. We are comfortable with many of the simple or built in types because we know what to expect. By comparison, identifying a variable’s type as class is uncomfortable unless we already know what that means. Like me, you have probably forgotten that once upon a time, even the so-called simple types were not so simple. It took time and effort to arrive at a comfortable level of understanding for double, char, and cell. The same is true for class. Before long, the idea of class as simply another variable type will seem natural.
To the bad, it means we must blaze our own trail without the benefit of requirements to keep us on track. Loose requirements provide enough rope to hang ourselves. It is too easy to weave a path that is difficult to maintain. In this chapter, we will learn how to meet requirements in a way that supports the needs of the group of eight. This helps keep MATLAB object-oriented programming on the straight and narrow.
A. VARIABLES, TYPES, CLASSES, AND OBJECTS
In every specialty, there are certain words that carry special meaning. At first glance, the sheer number of special words associated with object-oriented programming appears overwhelming. The sad fact that these words are sometimes misused does not help the situation. An additional burden comes in understanding slight differences among words that appear to be describing the same thing. Fortunately, mastering the vocabulary is not difficult. Most of the differences are anchored to the normal programming vocabulary.
In discussing object-oriented programming, the words class and object seem to suffer the most abuse. When you look closely at what these words actually represent, it is easy to understand why. After we carefully define both words, you will be able to follow discussions where the language is sloppy. This knowledge will also allow you to determine the competency of self professed experts.
The easiest way to explain the differences between class and object is to relate them to things you probably already know. Look at the MATLAB command-line listing provided in Code Listing.
1. First, let me reassure you. If the command syntax and results in Code Listing are familiar, you stand an excellent chance of taking full advantage of MATLAB object-oriented programming.
Look carefully at the information displayed by the whos command and you will notice, perhaps for the first time, a heading titled Class . I hope you find this particular word choice very interesting. You might complain that char and double are not classes but rather types. There’s no cause for alarm. In this particular context, class and type mean almost the same thing. Class is a slightly more specific term, one with special meaning to object-oriented programmers. In fact, the connection between class and type is so close that the term user-defined type is often used as a substitute for class.
You might reasonably wonder why the object-oriented pioneers felt the need to coin a new term. The short answer is that class represents a new category of a variable’s type in the same vein as array, cell, or structure. When we identify a variable as a double, its type attaches certain expectations to the variable. A class is more complicated than one of the simple types like double, char, or integer, but it still represents a type. We are comfortable with many of the simple or built in types because we know what to expect. By comparison, identifying a variable’s type as class is uncomfortable unless we already know what that means. Like me, you have probably forgotten that once upon a time, even the so-called simple types were not so simple. It took time and effort to arrive at a comfortable level of understanding for double, char, and cell. The same is true for class. Before long, the idea of class as simply another variable type will seem natural.
Learning about object-oriented programming will require some time and effort, and we certainly don’t want to take on all of the special properties at once. After all, even with numbers we started with 1, 2, and 3 before moving on to π and e.
Before moving on, go back to the whos display in Code Listing 1 and examine the information associated with variable x . Think about the low-level details that you usually take for granted. Reading from left to right, x is the name of the variable. What is a variable name? The variable’s name provides a human-readable reference to a value stored in memory. The variable’s size is listed as 1×1. From the size, we know that x is scalar. From the variable’s type, double, we know that x is a member of the set of real numbers. The type establishes limits on which functions to use and on the expected accuracy. At a glance, we know how x should behave and we take many details for granted.
So, what is x exactly? Is it a variable? a memory location? a scalar? a real number? … Of course, this is a trick question. Indeed, x represents all of those things and more. For algorithm design, the fact that x is double rather than complex or char is the primary focus. During code implementation, the variable’s name, structure, and indices become increasingly more important. During execution, MATLAB’s memory manager needs to know the physical address, the number of bytes, and so forth. No one is shocked that the meaning of x radically changes depending on context. This is exactly how we naturally cope with complexity. We avoid confusion by choosing to focus only on the features that are important to us right now.
Now I tell you that x is an object . Is it still a variable? Still located in memory? Still a scalar? … Of course! The new information that identifies x as an object does not change the fact that it is still double precision. Saying that x is an object merely attaches yet another feature to the variable x. In the whos display, Class simply puts built-in types and user-defined types on an equal footing. Some programmers might bristle at the use of class to describe common built-in types, but with MATLAB this attitude is misguided. The fact that double array is a class and x is an object opens many interesting options. We will examine many of these options as we progress through the various examples.
Class is simply another description used to organize variables. The choice of the word “class” must imply something, or one of the more common terms would have been used. A class is a formal description of something general, possibly a reusable data structure, an abstract concept, or a tangible thing. While there are often other supporting documents, the ultimate class description exists as class’ executable code. A class defines data elements and defines a set of functions used to operate on the elements. The data represent features or attributes and as a collection form the class’ outward appearance. MATLAB uses a structure to define the data elements. The class’ functions manipulate the data contained in the class’ structure. Functions are implemented using m-files. What makes a class different from a normal structure and set of m-files are the objectoriented rules that associate the data structure and the m-files in a way that they always exist together as a whole. In short, a class defines a data structure and an inseparable set of m-files designed to operate on that structure.
There must also be a difference between a class and an object. Objects relate to classes in the same way variables relate to types. During the course of a program’s execution, objects are created, used, and destroyed. The data structure for each object is unique and exists as a set of values stored in memory. Object x is different from object y because each occupies a different memory location. The structure of the data might be the same but the values can be different. All objects of the same class use the same set of class-specific m-files. The object’s data are always passed into these functions. In this way, the behavior is always consistent with a particular object’s data. In short, an object is a run-time entity that includes a type and individualized data.
Before moving on, go back to the whos display in Code Listing 1 and examine the information associated with variable x . Think about the low-level details that you usually take for granted. Reading from left to right, x is the name of the variable. What is a variable name? The variable’s name provides a human-readable reference to a value stored in memory. The variable’s size is listed as 1×1. From the size, we know that x is scalar. From the variable’s type, double, we know that x is a member of the set of real numbers. The type establishes limits on which functions to use and on the expected accuracy. At a glance, we know how x should behave and we take many details for granted.
So, what is x exactly? Is it a variable? a memory location? a scalar? a real number? … Of course, this is a trick question. Indeed, x represents all of those things and more. For algorithm design, the fact that x is double rather than complex or char is the primary focus. During code implementation, the variable’s name, structure, and indices become increasingly more important. During execution, MATLAB’s memory manager needs to know the physical address, the number of bytes, and so forth. No one is shocked that the meaning of x radically changes depending on context. This is exactly how we naturally cope with complexity. We avoid confusion by choosing to focus only on the features that are important to us right now.
Now I tell you that x is an object . Is it still a variable? Still located in memory? Still a scalar? … Of course! The new information that identifies x as an object does not change the fact that it is still double precision. Saying that x is an object merely attaches yet another feature to the variable x. In the whos display, Class simply puts built-in types and user-defined types on an equal footing. Some programmers might bristle at the use of class to describe common built-in types, but with MATLAB this attitude is misguided. The fact that double array is a class and x is an object opens many interesting options. We will examine many of these options as we progress through the various examples.
Class is simply another description used to organize variables. The choice of the word “class” must imply something, or one of the more common terms would have been used. A class is a formal description of something general, possibly a reusable data structure, an abstract concept, or a tangible thing. While there are often other supporting documents, the ultimate class description exists as class’ executable code. A class defines data elements and defines a set of functions used to operate on the elements. The data represent features or attributes and as a collection form the class’ outward appearance. MATLAB uses a structure to define the data elements. The class’ functions manipulate the data contained in the class’ structure. Functions are implemented using m-files. What makes a class different from a normal structure and set of m-files are the objectoriented rules that associate the data structure and the m-files in a way that they always exist together as a whole. In short, a class defines a data structure and an inseparable set of m-files designed to operate on that structure.
There must also be a difference between a class and an object. Objects relate to classes in the same way variables relate to types. During the course of a program’s execution, objects are created, used, and destroyed. The data structure for each object is unique and exists as a set of values stored in memory. Object x is different from object y because each occupies a different memory location. The structure of the data might be the same but the values can be different. All objects of the same class use the same set of class-specific m-files. The object’s data are always passed into these functions. In this way, the behavior is always consistent with a particular object’s data. In short, an object is a run-time entity that includes a type and individualized data.
Andy H. Register
Tags:
2 comments: on "MATLAB’s Requirements"
You can access frequently asked interview question on c , c++ , java , data structure , databases on
http://www.gotaninterviewcall.com/
Thank you... for information.. :)
Post a Comment