2 What is a class?

A C++ class is a cookie cutter that can be used to stamp out ``cookies''. Each cookie cut out by the same cookie cutter has the same basic structure. However, each cookie has its own state.

Consider 1996 Mazda Miata as a ``class''. It describes all the attributes that are common to 1996 Mazda Miatas (Miatae?). For example, ``1996 Mazda Miata'' suggests a gas tank with a capacity of 12 gallons. It also suggests a capacity of 4 quarts of motor oil.

However, each individual 1996 Mazda Miata has its own state. For example, my Miata may have 4 gallons in the gas tank, and 3.9 quarts of motor oil. Another 1996 Mazda Miata on the road probably has a different amount of gas and motor oil.

Another way to look at a class is a definition in a dictionary. Let us consider the definition of a shark (from the perspective of biology). A shark is a fish that has soft cartilage, tough skin with toothlike scales, and some other features.

The definition of a shark from a biology textbook cannot bite you, eat fish, swim, reproduce, and etc. This is because the definition of a shark is a description, not an actual animal.

However, a shark in the ocean swims, breath, lives and dies. It also has its own states, such as weight, age, number of teeth, DNA and etc. At the same time, all sharks in the ocean and aquariums are described by the definition of sharks, such as soft cartilage, tough skin with toothlike scales and etc.

In the world of object-oriented programming, the definition of a shark in a biology textbook is a class definition. Each individual shark is called an ``object''. In other words, each object is an entity that exists in memory, has its own state, lifespan and etc. At the same time, each object is also described by the class to which it belongs.

Copyright © 2006-09-26 by Tak Auyeung