structure.cpp: a simple structure #include <iostream> struct inflatable { char name[20]; float volume; double price; }; int main() { using namespace std; inflatable guest = { "Glorious Gloria", 1.88, 29.99 }; inflatable pal = { "Audacious Arthur", 3.12, 32.99 }; cout << "Expand your guest list with " << guest.name; cout << " and " << pal.name << "!\n"; cout << "You can have both for $"; cout << guest.price + pal.price << "!\n"; return 0; } 1. 编译输出: Expand your guest list with Glorious Gloria and Audacious Arthur! You can have both for $62.98! 2. 代码详解:
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; cout << "choice: " << choice.name << " for $"; cout << choice.price << endl; return 0; } 1. 编译输出: bouquet: sunflowers for $12.49 choice: sunflowers for $12.49 2. 代码详解:
struct perks { int key_number; char car[12]; } mr_smith, ms_jones;
struct perks { int key_number; char car[12]; } mr_glitz = { 7, "Packard" };
arrstruct.cpp: an array of structures #include <iostream> struct inflatable { char name[20]; float volume; double price; }; int main() { using namespace std; inflatable guests[2] = { {"Bambi", 0.5, 21.99}, {"Godzilla", 2000, 565.99} }; cout << "The guests " << guests[0].name << " and " << guests[1].name << "\nhave a combined volume of " << guests[0].volume + guests[1].volume << " cubic feet.\n"; return 0; } 1. 编译输出: The guests Bambi and Godzilla have a combined volume of 2000.5 cubic feet. 2. 代码详解:
|
|