Print This Page

Object Composition

What are Classes and Objects? | What is Composition? | C++ Syntax for Class Composition | Summary


What are Classes and Objects? Back to Top

So far in the term, we have not seen many significant ways that object orientation reduces development time. In fact, much of what we have done so far has taken more time and has been more complex than the procedural programs we used in previous classes. So, what are the advantages of using object-oriented programming, as it takes longer than procedural programming?

Real-world objects often have similar attributes. For example, many electronic devices have on and off switches, digital clocks, volume controls, and so forth. One way to simplify the programming model for these types of devices is to define the common attributes once, eliminating the duplication of code.

Look around the room that you are sitting in right now--what do you see? You might see a computer, a table, a chair, possibly a printer, and several other objects that make up your office or study area. What you are observing is that the world, and the way that we perceive it, is made up of objects, whether these objects are human made, natural, or just concepts that we can categorize, describe, create, organize, manipulate, and destroy. How we see or categorize things or objects in our surroundings is how we think as human beings, so it is only natural that we use the same thought process when creating software applications. Any real-world system, by its very nature, is composed of interacting complex objects. To be able to define a model of a complex system that results in a desired functionality, we need to identify only the objects that contribute to the specified functionality--no more and no less.

Yet, what is the difference between a class and an object? A class is a definition of a data type that consists of attributes, operations, relationships, and semantics used for that data type, and all the objects instantiated from that class will and must share.

What we are doing is classifying all of the objects with the same operations and attributes into a single specification, or class, that is an abstraction describing all of the objects in the class. For example, we classify the furniture in our rooms as chairs, desks, bookshelves, and so forth, and we may have multiple objects belonging to each of the different classes of furniture. What this really means is that a class is a blueprint for an object in the same way that a house blueprint specifies how a house will be built. That is, the blueprint is an abstraction of a house (with only the design specifications), but it is not an actual house. In order to actually live in a house, we must build it based on the blueprint. This is the same approach that we will take in our object-oriented designs. We will classify the things, or objects, in our system and create a class blueprint of them, but before we actually work with any of these objects, we will use the class blueprint to create the object.

An object is the instantiation (creation) of a class based on the class specifications. The house that you live in was built using blueprints, and it is one of many instantiations of the same blueprint. If you live in a housing development, you see several houses built from the same set of blueprints; each house, however, is a different object based on the same blueprints, or class specifications. A reference is how you identify and manipulate a specific object. For example, a house address is used as a reference for how mail is sent and received. You can tell the difference between the houses in your development by their addresses and by the values of the various attributes (color, size, location, etc.).

In the vocabulary of object-oriented systems, a class specifies the structure and the services that each object of that class will provide. An object is the physical instantiation of the class, and a reference to an object allows us to manipulate a specific object. In order for our objects to provide services in our programs, we construct operations that the object must perform in order to provide the specified functionality and functions to implement the operations. For example, many houses have laundry services, kitchen services, bathroom services, and so on. We then use attributes in a class definition to hold values that describe the physical object. In the house example, some attributes are the house address and the color of the house. Each house has specific values assigned to the properties. For example, say I build a house with the address 1234 Evergreen Ave. and the color brown, and then, I build another house with the address 3456 Evergreen Ave. and the color pink. These specific attributes, or states, set one house apart from another.

In short,

Let's consider an example that illustrates the concept of classes and objects. Suppose I am writing a program to keep track of information in a local school. I want to keep track of all of the sections of a 5th-grade class, and I want to keep track of the following information for each section.

First, we can define a Student class that describes each student. Then, for each Grade 5 object, we will create an array of Student objects for each student in the class.

If the school is large, there will be more than one Grade 5 sessions running at any time. For this example, let's say that there are three 5th-grade sessions going on during the school year. We will create, or instantiate, three Grade 5 objects from the Grade 5 class definition. When we do this, we create specific instances of the Grade 5 class and assign specific values for each of the attributes of the objects. (To save space, we will list only the Teacher's Name and the Students.)

In summary, we defined two classes: (a) a Grade 5 class and (b) a Student class. Then, we created three instances of the Grade 5 class. Subsequently, each Grade 5 object will have a list of students; in this example, we show 25 students per class.

A class simply defines what an object looks like, and a class does not exist in memory. However, we can build an object using the class definition, called instantiating an object. Once we instantiate the object, we can manipulate the object using the methods defined in the class definition.

This should all make sense now. If not, keep asking questions until it does; this may be the most fundamental concept in object-oriented programming, so let's make sure that we understand it.


What is Composition? Back to Top

In Week 3, we learned about class inheritance, which may simply be described as an is a relationship between one or more classes. Back in high school biology class, you may have classified the plant and animal kingdom using such terms as Kingdom, Phylum, Class, Order, and so on.

If we were to classify a garter snake we found in our front yard, a very much simplified example (apologies to all biology teachers) might be something like Animal:Reptile:Snake:Garter Snake. Using the inheritance definition, this could be read back as something like "a Garter Snake is a Snake is a Reptile is an Animal." The Garter Snake inherits all the characteristics of a snake and has the specific characteristics (i.e., the data and functions) that make it unique from other snakes. All Snakes, in turn, inherit the characteristics of Reptiles plus the specific characteristics that differentiate them from other Reptiles, such as Lizards and Turtles. Last, Reptiles inherit the characteristics of Animals plus the characteristics that differentiate them from other Animals, such as Birds and Mammals.

The ability to use class definitions to move from specific to general (i.e., up the classification hierarchy from Garter Snake to Animal) or to go from general to specific (i.e., down the classification hierarchy from Animal to Garter Snake) demonstrates some of the advantages of object-oriented programming through classification and reusability. For example, if I discover a new species of snake eating a beetle in my garden (call it the tomato-plant-saving, beetle-eating garden snake), I am able to create a new class for the new species using inheritance, and I have to add only the data and the function members to the new class that is derived from the class Snake. This saves a tremendous amount time (and money) for code development and testing.

Inheritance, though, is only one side of the object-oriented coin, with the other being composition (which is sometimes also called aggregation). Composition also has a simple definition, which is a has a relationship.

As stated earlier, how we see or categorize things or objects in our surroundings is how we think as human beings. We often see the things or objects that surround us as the sum or composite of other things or objects. You are most likely using a computer to read this lecture. A computer is an object made up of several other objects. You could describe the computer by saying what the parts are and by stating that the computer “has a” monitor, “has a” circuit board, “has a” keyboard, “has a” mouse, “has a” disk drive, and so on. In other words, your computer is a composition of all of its parts or objects.

Object-oriented programming languages are said to use composition when an object of one class is included as a data member of another class. If the classes for the other objects have already been developed, tested, and accepted for programming use, then composition also greatly simplifies the development effort of a new class that uses those objects as some of its data members. This is another example of reusability as a benefit to using object-oriented programming.

The scale of composition is also very much dependent upon how it is to be used in designing and modeling a system in software. A person building a computer from components, such as keyboards, monitors, and disk drives, would view a disk drive as a single object. However, an engineer designing a disk drive would require a much more detailed description of the disk drive and would also probably use the composition of a much lower level to describe the disk drive. For example, the engineer might state that the disk drive “has a” drive motor, “has a” magnetic read/write head, “has a” microcontroller circuit, “has a” voltage regulator, and so on. Which model of a disk drive and what level of composition depend on the need and the scale of the software project at hand.


C++ Syntax for Class Composition Back to Top

Classes that use the objects of other classes as their data members are called composite class. Let's consider the class Rectangle, which contains the x coordinate (for the top-left corner) and the y coordinate (for the bottom-right corner), as an example.

Click here to view the source code.

The client main() code can then define the objects of the class Rectangle by specifying the coordinates of their corners, the width, and the height.

Click here to view the source code.

Even for this simple example, the class internal structure seemed too complex; it is too much work to specify the values of the Rectangle object. The reason for this unnecessary complexity is because of the lack of implementation for a data component--a Point.

Let us now consider the same example; however, let's use a class Point this time.

Click here to view the source code.

This version of the class Rectangle now has two data members of class Point to denote the top-left and the bottom-right corners.

Click here to view the source code.


Summary Back to Top

In summary, object-oriented analysis and design allows us to model the structure of our system by defining classes, which are the blueprints that describe an object. Each class contains specifications for attributes that hold the information we need to retain for each of the objects and operations that provide the processing services for the objects. Therefore, a class defines what an object looks like, but a class does not exist in memory. We can, however, build an object using the class definition, which is called instantiating an object. Once we instantiate an object, we can manipulate the object's attributes by using the methods defined in the class definition.

Once a class is developed, it can be used to create another class. The derived class inherits the attributes and behaviors of the base class. It is also typical for the derived class to include additional attributes, or behaviors. The inheritance of these attributes and functions significantly streamlines the development of an application. Inheritance is a fundamental characteristic of OOP. A class may also be used as part of another class. This composition feature exhibits a has a relationship (every person object has a date of birth). In order to exploit these hierarchies and realize the benefit in reduced development time, we must learn to recognize them.

 




Back to Top