User Defined Types  «Prev  Next»
Lesson 5 Objects are instances of classes
Objective Describe how Objects instantiate Classes

Describe how Objects instantiate Classes

A class defines a type. In general, there can be many different instances of the class, each of which may have different values for the attributes of the class. These instances are called objects. For example, the origin is a Point object whose x attribute is 0.0 and whose y attribute is 0.0. This can be represented by an object diagram.
Object diagrams are useful when giving examples of values for all the attributes or showing how an object changes as the system runs. An object diagram is deliberately similar to a class diagram. It is divided into two compartments, like this:
  • Object Diagram Example
    Object diagram
    Object diagram consisting of 2 variables
    1. An object diagram is deliberately similar to a class diagram. It is divided into two compartments. The top compartment holds the name of the object, followed by a colon and the name of the class. Both the name of object and the name of the class are underlined.
    2. The bottom compartment holds the names of the object's attributes and their values, here 0.0 and 0.0. The type is omitted, though it is often implicit in the values. Using the equal sign makes it clear you are showing the value.
  • UML Object Diagram:
    An object diagram in the Unified Modeling Language (UML), is a diagram that shows a complete or partial view of the structure of a modeled system at a specific time. An object diagram focuses on some particular set of objects and attributes, and the links between these instances. A correlated set of object diagrams provides insight into how an arbitrary view of a system is expected to evolve over time. In early UML specifications the object diagram is described as:
    An object diagram is a graph of instances, including objects and data values. A static object diagram is an instance of a class diagram; it shows a snapshot of the detailed state of a system at a point in time. The use of object diagrams is fairly limited, namely to show examples of data structure.

    Object diagrams and class diagrams are closely related and use almost identical notation. Both diagrams are meant to visualize the static structure of a system. Object diagrams displays instances of classes (objects) Object diagrams are more concrete than class diagrams. They are often used to provide examples or act as test cases for class diagrams.

Object-Oriented Analysis Design with Applications

Anonymous Objects

Occasionally, you will see diagrams for anonymous objects. These are objects for which no name is obvious or needed.
For anonymous objects, the name is omitted, but the colon is retained, like this:
:Point
x=50.0
y=32.0

Anonymous objects are commonly used in diagrams that show one object contained in another object. The name of the object is taken from the containing class. They can also be used to show samples of an object that may not be used in any particular part of the code.
An anonymous object is basically an object which the compiler creates a class for. Anonymous objects are objects without a name.
Example:
class Foo {
};

void bar(Foo const &foo) {
}

int main() {
    Foo(); // creates an anonymous Foo and immediately destroys it
    bar(Foo()); // creates an anonymous Foo which lives until bar returns
}

Video Store Course Example

In the video store course example, objects of the Title class might include the Gone With the Wind title or the Bambi title. Objects of the Customer class might include "Jo Smith" Here's how that object might look:
Jo Smith: Customer
Name: Jo W. Smith
Address: 123 Main Street, Ourtown
Phone: 555-1212
Balance: 1.00

SEMrush Software