|
|
|
|
|
|
|
declaring Height and Width to be unsigned short integers, you know that it is possible to add Height to Width and to assign that number to another number. |
|
|
|
|
|
|
|
|
The type of these variables tells you |
|
|
|
|
|
|
|
|
What information they can hold |
|
|
|
|
|
|
|
|
What actions can be performed on them |
|
|
|
|
|
|
|
|
A type is a category. One of the things that distinguishes humans is our ability to categorize. We don't see hundreds of shapes on the savanna; we see animals and trees. And we don't just see animals; we see gazelles, giraffes, elephants, water buffalo, and so forth. We create taxonomies, classifications, orders, groupings, divisions, and classes. In short, we think in terms of types of things. |
|
|
|
|
|
|
|
|
An orange is a citrus. A citrus is a fruit. A fruit is a plant. A plant is a living thing. |
|
|
|
|
|
|
|
|
Familiar types include car, house, person, and shape. In C++, a type is an object with a size, a state, and a set of abilities. |
|
|
|
|
|
|
|
|
A C++ programmer can create any type needed, and each of these new types can have all the functionality and power of the built-in types. |
|
|
|
|
|
|
|
|
Programs are usually written to solve real-world problems, such as keeping track of employee records or simulating the workings of a heating system. Although it is possible to solve complex problems by using programs written with only integers and characters, it is far easier to grapple with large, complex problems if you can create representations of the objects that you are talking about. In other words, simulating the workings of a heating system is easier if you can create variables that represent rooms, heat sensors, thermostats, and boilers. The closer these variables correspond to reality, the easier it is to write the program. |
|
|
|
|
|
|
|
|
New Term: You make a new type by declaring a class. A class is just a collection of variablesoften of different typescombined with a set of related functions. |
|
|
|
|
|
|
|
|
One way to think about a car is as a collection of wheels, doors, seats, windows, and so forth. Another way is to think about what a car can do: It can move, speed up, slow down, stop, park, and so on. |
|
|
|
|
|