Concepts / Classes and User-Defined Types

Classes and User-Defined Types

The class is therefore the tool that turns a problem's concept into a type the compiler can check.

  • Programming

Two Ways to Describe a Program's Work

Imagine you are designing a program that tracks customer orders. You could describe the work as a series of steps, such as checking a price or updating a running total. Or you could describe the work in terms of the things involved, such as an order, a customer, and a price list. C++ gives you tools for both views. The first view is expressed through functions, and the second is expressed through classes. This article begins with the first view and then turns to the second, because the classes are what give a program's computations their structure.

What do you think happens?

If a program's design is written in a separate document and the code is not connected to it, what happens when someone changes the code later?

  • The document is updated automatically by the compiler

  • The document can drift away from what the program actually does

  • The compiler warns the programmer that the document is out of date

Reveal answer
Answer:

The document can drift away from what the program actually does

Neither the compiler nor any other tool checks a design document or comments against the code. Nothing forces them to stay in agreement with the program.

Where an Idea Lives During Design

A useful idea in a program's design can live in several places, and each place is less reliable than the one after it. First, the idea exists only in the designer's head. Second, it may be written down in a design document. Third, it may appear as comments next to the code. None of these three places is checked by the compiler, so the idea can drift away from what the program actually does. The recommended move is to take the idea one step further and represent it as a class in the program itself, so the idea is present in the code where the compiler and other programmers can read and use it.

Designer's headidea exists only mentallyDesign documentwritten down, not checkedComments beside codenear the code, not checkedClass in the programidea present in the codeitselfwritten downannotated in coderepresented as a class
Where a design idea lives as it moves from a person's head into the program. Each stage moves the idea closer to something the compiler and other programmers can actually read and use.
In the real world

Consider the idea of a customer order. In a design document, it might be described as a record that holds the items bought, the total, and the date. In comments, a programmer might write that the total must be updated whenever an item is added. Nothing stops the code from doing something different. When the idea is represented as a class, the same description lives in the program: the class has a name, it groups the order's information together, and the compiler and other programmers can work with that class directly.

Built-In Types Versus User-Defined Types

Definition

A user-defined type is a type that the programmer creates to match a concept from the problem being solved. A class is the main way C++ lets a programmer define such a type. Built-in types, such as int and float, come with the language and are not created by the programmer.

intprovided by the languageClasscreated by the programmerfloatprovided by the languageConcept from theproblemsuch as an order or acustomer
Built-in types arrive with the language, while user-defined types are created to match a concept from the problem being solved.

The source puts the emphasis on the programmer's choice. When the design contains a useful concept, idea, or entity, the advice is to represent it as a class so that it appears in the code rather than remaining in a separate document or in comments. The class is therefore the tool that turns a problem's concept into a type the compiler can check.

Why Well-Chosen Classes Help

A program built from a well-chosen set of classes is easier to understand and get right than one built only from built-in types. A reader who sees a class named for a concept such as an order does not have to rebuild that concept from scattered variables and loose operations. Instead, the reader holds one named idea in mind and looks inside it only when needed. The fewer loose pieces a reader must track at once, the easier the program is to follow.

The source also explains that much of the language exists to support classes. Apart from the fundamental types, operators, and statements, almost every other language facility exists to help define better classes or to use them more conveniently. The goals it names for those facilities are being more correct, easier to maintain, more efficient, more elegant, easier to use, easier to read, and easier to reason about.

Functions and Function Objects in the Framing

The passage that introduces this topic says that computations on user-defined and built-in types are represented as functions. Those functions are sometimes generalized to template functions and function objects. Templates, which parameterize types and algorithms with other types and algorithms, are the subject of the following chapter. The introductory passage does not develop functions in detail, so the discussion in this material concentrates on the class, which is treated as the central language feature of C++ and the main tool for representing concepts.

Three Important Kinds of Classes

The source lists three kinds of classes that it treats as foundational: concrete classes, abstract classes, and classes in class hierarchies. These three kinds are variations of one concept, the class, rather than unrelated topics. The source claims that a very large share of useful classes belong to one of these three kinds, and that even more classes can be treated as simple variants of them or as implemented by combining the techniques used for them.

Classuser-defined type for aconceptConcrete classesAbstract classesClasses in classhierarchieskind ofkind ofkind of
The three foundational kinds of classes all sit under the general idea of a class, so each is a variation of one concept.

At this stage the goal is only to recognize the three names and see how they fit together. The source gives each kind a cross-reference to later sections, which develop each one in detail. Concrete classes, abstract classes, and classes in class hierarchies are the three kinds to keep in mind as the rest of the material unfolds.

Programming Styles the Language Supports

The source explains that programmers have very different needs and preferences. Because of that variety, support for classes is extensive, and the material narrows its focus to the basic support for the three kinds above. The class and related facilities are described as the language support for two named programming styles, both of which C++ supports.

Programming styleWhat the language support isWhat the source states
Object-oriented programmingLanguage support provided by the class and related facilitiesDescribed as supported by C++
Generic programmingLanguage support provided by the class and related facilitiesDescribed as supported by C++

The two programming styles that the source connects to classes and related facilities

The abstract idea of a style becomes concrete in later chapters, which present standard-library facilities and show how they are used. Until then, it is enough to know that classes are the common foundation for both styles.

Mistakes Beginners Make With Classes

  • Common mistake

    Treating a comment as proof that the design is implemented

    What it looks like

    A comment says the order total is updated whenever an item is added, but no code does this.

    Why it fails

    Comments are not checked by the compiler, so they can drift away from what the program actually does.

    Do this instead

    Represent the important idea as a class so the concept lives in the code itself.

  • Common mistake

    Assuming the design document will stay accurate on its own

    Why it fails

    A design document is also not checked by the compiler, so it can fall out of step with the program.

    Do this instead

    Move the idea into the program as a named type that the compiler can use.

  • Common mistake

    Believing built-in types are enough for a well-organized program

    Why it fails

    The source states that a program built from a well-chosen set of classes is easier to understand and get right than one built only from built-in types.

    Do this instead

    Create user-defined types for the concepts in the problem being solved.

Sorting Ideas Into Their Places

MEDIUM

A team is designing a program that tracks customer orders. Three ideas are being discussed: a rule that every order total must match the sum of its items, a note in a design document describing what an order is, and a comment beside the code that says the total is recalculated. For each idea, decide whether it currently lives only in a person's head, in a design document, in comments, or as a class in the program. Then explain which of these places the compiler checks, and what move the source recommends to bring the idea into the code.

Hints
  • Recall the four stages: designer's head, design document, comments beside code, and class in the program.
  • The source states that none of the first three places is checked by the compiler.
  • The recommended move is to represent the idea as a class so it appears in the code.

Key Takeaways

  • C++ represents computations as functions and function objects, and this material turns to classes because classes give those computations their structure.
  • A class is a user-defined type that represents a concept in the program, so the idea lives in the code rather than only in a designer's head, a document, or comments.
  • A program built from a well-chosen set of classes is easier to understand and get right than one built only from built-in types.
  • The three important kinds of classes are concrete classes, abstract classes, and classes in class hierarchies, and the material states that C++ supports object-oriented and generic programming through the class and related facilities.

Related concepts

More concepts starting with C