struct to define a structure.
struct car
{
char name[20]; // an array member
float volume; // a float member
double price; // a double member
};
Note: There is a semicolon (;) at the end of the struct definition!
After you have the template, you can create variables of that type:
car RollsRoyce; // RollRoyce is a structure variable of type car
car Mercedes; // type car variable
car Ferrari; // type car variable
Unlike C, C++ allows you to drop the keyword struct when you declare
structure variables:
struct car Toyota; // keyword struct required in C car Toyota; // keyword struct not required in C++
Mercedes is type car, you use the membership
operator(.) to access individual members.
Mercedes.volume refers to the volume member of the structure,
and Mercedes.price refers to the price member.
// car_structure.cpp -- A simple structure #includeusing namespace std; struct car { char name[20]; // an array member float volume; // a float member double price; // a double member }; // note the semicolon int main() { car Ferrari = // always try to initialise all the members, { // seperating them by commas. "Ferrari 2.0", 350.0, 24000 // last member doesn't need a comma }; car Mercedes = { "Mercedes Benz", // a name value 340.0, // volume value 30000 // a price value }; cout << "The latest Ferrari model \"" << Ferrari.name << "\" costs $" << Ferrari.price << endl; cout << "The latest Mercedes model \"" << Mercedes.name << "\" costs $" << Mercedes.price << endl; cout << "If you're thinking of buying both models, it'll cost you $" << Ferrari.price + Mercedes.price << endl; return 0; } OUTPUT: The latest Ferrari model "Ferrari 2.0" costs $24000 The latest Mercedes model "Mercedes Benz" costs $30000 If you're thinking of buying both models, it'll cost you $54000
main(), its
called an external declaration.
// car_assignment.cpp -- Assigning one structure to another of the same type #includeusing namespace std; struct car { char name[20]; float volume; double price; }; int main() { car Ferrari = { "Ferrari 2.0", 350.0, 24000 }; car Mercedes = { "Mercedes Benz", 340.0, 30000 }; // Before Assignment cout << "Before assigning Mercedes to Ferrari, " << "Ferrari.name was " << Ferrari.name << endl; // Assign Mercedes to Ferrari Ferrari = Mercedes; // After assignment cout << "After assigning Mercedes to Ferrari, " << "Ferrari.name is " << Ferrari.name << endl; return 0; } OUTPUT: Before assigning Mercedes to Ferrari, Ferrari.name was Ferrari 2.0 After assigning Mercedes to Ferrari, Ferrari.name is Mercedes Benz
You can combine the definition of a structure form with the creation of structure variables. To do so, follow the closing brace with the variable name or names.
struct car
{
char name[20];
float volume;
double price;
} Ferrari, Mercedes;
You can initialize a variable you create in this fashion.
struct car
{
char name[20];
float volume;
double price;
} Ferrari =
{
"Ferrari 2.0",
350.0,
24000
};
Its possible to create arrays whose elements are structures. The technique is exactly the
same as for creating arrays of the fundamental types. For example, to create an array of
100 car types, do the following:
car Mitsubishi[100]; // an array of 100 car structures
Each element of the array, such as Mitsubishi[0] or Mitsubishi[99],
is a car object and can be used with the membership operator:
cin >> Mitsubishi[0].volume; // use volume member of first struct
cout << Mitsubishi[99].price << endl; // display price member of the last struct
To initialize an array of structures, combine the rule for initializing arrays ( a brace-
enclosed, comma-separated list of values for each element) with the rule for structures
(a brace-enclosed, comma-separated list of values for each member). Such as:
car Hovercraft[2] = // initializing an array of structs
{
{"Hover1", 0.5, 21.99}, // first structure in an array
{"Hover2", 2000, 564.4} // next structure in an array
};