C++ structures are similar to classes, except that all members of a structure are public by default, whereas members of a class are private by default. A structure is defined in C++ using the keyword struct followed by the structure name. The body of the structure is enclosed in curly braces and the definition is terminated by a semicolon.
C++ structures can contain data members and member functions and can also be initialised when objects are created. They can therefore provide many of the same features as classes, although structures are commonly used when the main purpose is to group related data together.
#include <iostream>
using namespace std;
struct box
{
int length;
int height;
int width;
};
int main()
{
box box1;
box1.height = 5;
box1.length = 30;
box1.width = 20;
cout << "Dimensions of box1 are - "
<< box1.height * box1.length * box1.width;
box box2;
box2.height = 5;
box2.length = 30;
box2.width = 20;
cout << "\nDimensions of box2 are - "
<< box2.height * box2.length * box2.width;
return 0;
}