Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Составные типы данных_лекция_161012.docx
Скачиваний:
6
Добавлен:
19.11.2019
Размер:
3.27 Mб
Скачать

Структуры

// structur.cpp -- a simple structure

#include <iostream>

struct inflatable // structure declaration

{

char name[20];

float volume;

double price;

};

int main()

{

using namespace std;

inflatable guest =

{

"Glorious Gloria", // name value

1.88, // volume value

29.99 // price value

}; // guest is a structure variable of type inflatable

// It's initialized to the indicated values

inflatable pal =

{

"Audacious Arthur",

3.12,

32.99

}; // pal is a second variable of type inflatable

// NOTE: some implementations require using

// static inflatable guest =

cout << "Expand your guest list with " << guest.name;

cout << " and " << pal.name << "!\n";

// pal.name is the name member of the pal variable

cout << "You can have both for $";

cout << guest.price + pal.price << "!\n";

getchar();

return 0;

}

// assgn_st.cpp -- assigning structures

#include <iostream>

struct inflatable

{

char name[20];

float volume;

double price;

};

int main()

{

using namespace std;

inflatable bouquet =

{

"sunflowers",

0.20,

12.49

};

inflatable choice;

cout << "bouquet: " << bouquet.name << " for $";

cout << bouquet.price << endl;

choice = bouquet; // assign one structure to another

cout << "choice: " << choice.name << " for $";

cout << choice.price << endl;

getchar();

return 0;

}

того же типа.

// arrstruc.cpp -- an array of structures

#include <iostream>

struct inflatable

{

char name[20];

float volume;

double price;

};

int main()

{

using namespace std;

inflatable guests[2] = // initializing an array of structs

{

{"Bambi", 0.5, 21.99}, // first structure in array

{"Godzilla", 2000, 565.99} // next structure in array

};

cout << "The guests " << guests[0].name << " and " << guests[1].name

<< "\nhave a combined volume of "

<< guests[0].volume + guests[1].volume << " cubic feet.\n";

getchar();

return 0;

}

Объединения

Перечисления