Learn C++ - C++ Structures






Here is a structure description that meets those needs:

struct Product   // structure declaration 
{ 
     char name[20]; 
     float volume; 
     double price; 
}; 

The keyword struct indicates that the code defines the layout for a structure.

The identifier Product is the name, or tag, for this form.

This makes Product the name for the new type.


#include <iostream> 
struct Product   // structure declaration 
{ //w w  w.  j a  v a  2  s .  c  o  m
    char name[20]; 
    float volume; 
    double price; 
}; 

int main() 
{ 
    using namespace std; 
    Product guest = 
    { 
         "C++",  // name value 
         1.8,               // volume value 
         29.9               // price value 
    };  
    Product pal = 
    { 
         "Java", 
         3.1, 
         32.9 
    };  // pal is a second variable of type Product 
    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; 
} 

The code above generates the following result.





Example

The following code shows how to assign structures.


#include <iostream> 
using namespace std; 
struct Product /*from   w  w  w.j  ava  2 s . c  om*/
{ 
     char name[20]; 
     float volume; 
     double price; 
}; 
int main() 
{ 
     
     Product bouquet = { "C++", 0.20, 12.49 }; 
     Product 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; 
     return 0; 
} 

The code above generates the following result.





Arrays of Structures

It's possible to create arrays whose elements are structures.

For example, to create an array of 100 Product struc- tures, you could do the following:

Product gifts[100];  // array of 100 Product structures 
cin >> gifts[0].volume;          // use volume member of first struct 
cout << gifts[99].price << endl; // display price member of last struct 

To initialize an array of structures, you combine the rule for initializing arrays with the rule for structures.

Because each element of the array is a structure, its value is represented by a structure initialization.

Product guests[2] =              // initializing an array of structs 
     { 
          {"A", 0.5, 21.99},      // first structure in array 
          {"B", 2000, 565.99}  // next structure in array 
}; 

The following code shows a short example that uses an array of structures.


#include <iostream> 
using namespace std; 
struct Product /* ww  w .  java2  s  .  com*/
{ 
     char name[20]; 
     float volume; 
     double price; 
}; 
int main() 
{ 
    Product guests[2] =          // initializing an array of structs 
    { 
         {"A", 0.5, 21.99},      // first structure in array 
         {"B", 2,   5.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"; 
    return 0; 
} 

The code above generates the following result.