Студопедия
Случайная страница | ТОМ-1 | ТОМ-2 | ТОМ-3
АвтомобилиАстрономияБиологияГеографияДом и садДругие языкиДругоеИнформатика
ИсторияКультураЛитератураЛогикаМатематикаМедицинаМеталлургияМеханика
ОбразованиеОхрана трудаПедагогикаПолитикаПравоПсихологияРелигияРиторика
СоциологияСпортСтроительствоТехнологияТуризмФизикаФилософияФинансы
ХимияЧерчениеЭкологияЭкономикаЭлектроника

Introducing Structures

Читайте также:
  1. Chapter 1. Introducing Interpersonal Communication
  2. Configuration structures and the selection of configuration items
  3. CStructures with for, sinceand last
  4. Fig. 12. Schéma des paramètres de grue au cours de mise en oeuvre des structures qui se trouvent au-dessus du niveau de son stationnement
  5. GLOSSARY OF BASIC AND COMPLEX STRUCTURES
  6. Hyponymic (Hierarchical) Structures and Lexico-Semantic Groups
  7. Introducing sentence stress

Suppose you want to store information about a basketball player. You might want to store his or her name, salary, height, weight, scoring average, free-throw percentage, assists, and so on. You’d like some sort of data form that could hold all this information in one unit. An array won’t do. Although an array can hold several items, each item has to be the same type. The answer to your desire is the C++ structure. A structure is a more versatile data form than an array because a single structure can hold items of more than one data type. This enables you to unify your data representation by storing all the related basketball information in a single structure variable. If you want to keep track of a whole team, you can use an array of structures. The structure type is also a stepping stone to that bulwark of C++ OOP, the class.

A structure is a user-definable type, with a structure declaration serving to define the type’s data properties. After you define the type, you can create variables of that type. Thus, creating a structure is a two-part process. First, you define a structure description that describes and labels the different types of data that can be stored in a structure. Then, you can create struc-ture variables, or, more generally, structure data objects, that follow the description’s plan.

For example, suppose that Bloataire, Inc., wants to create a type to describe members of its product line of designer inflatables. In particular, the type should hold the name of the item, its volume in cubic feet, and its selling price. Here is a structure description that meets those needs:

 

struct inflatable { // structure declaration

char name[20];

float volume;

double price;

};

 

The keyword struct indicates that the code defines the layout for a structure. The identifier inflatable is the name, or tag, for this form; this makes inflatable the name for the new type. Thus, you can now create variables of type inflatable just as you create variables of type char or int. Next, between braces is the list of data types to be held in the structure. Each list item is a declaration statement. You can use any of the C++ types here, including arrays and other structures. Each individual item in the list is called a structure member (See Fig. 1).

After you have the template, you can create variables of that type:

 

inflatable hat; // hat is a structure variable of type inflatable

inflatable woopie_cushion; // type inflatable variable

inflatable mainframe; // type inflatable variable

 

Fig. 1. Parts of a structure description

 

Given that hat is type inflatable, you use the membership operator (.) to access individual members. For example, hat.volume refers to the volume member of the structure, and hat.price refers to the price member. Similarly, vincent.price is the price member of the vincent variable. In short, the member names enable you to access members of a structure much as indices enable you to access elements of an array. Because the price member is declared as type double, hat.price and vincent.price are both equivalent to type double variables and can be used in any manner an ordinary type double variable can be used.

 


Дата добавления: 2015-10-29; просмотров: 112 | Нарушение авторских прав


Читайте в этой же книге: ТЕМА 3. Применение операционных усилителей | Operator Precedence | Simple Type Conversion | Inline Functions | Multidimensional Arrays |
<== предыдущая страница | следующая страница ==>
Pointer Arithmetic| Introduction to Strings

mybiblioteka.su - 2015-2024 год. (0.006 сек.)