Topic 17: StructuresNote that each individual piece of information (usually called "fields" or "members") might be of a different type. So, we can't group these fields together as elements in an array because all the elements of arrays must be of the same type.
struct stock_item
{
char item_code[5]; // one char for the null
char description[50];
int num_in_stock;
float price;
}; // semicolon is required
Once a structure type like this has been defined, you can declare variables of that type like this:
stock_item widgit; // creates one stock_item variable.
Now you can store all the information about one (only) item of stock. This is done like this:
widgit.num_in_stock = 17;
// there are 17 of these items on the shelf
widgit.price = 12.78; // they cost $12.78 each
strcpy( widgit.item_code, "MC36" );
// item_code is a STRING of 4 chars, so must use strcpy
strcpy( widgit.description, "Motorcycle Gloves (Pair)" );
Note the use of the full stop between the name of the structure variable and the field or member which is being accessed. This is always the case, so, to access any particular field of a struct, we write:
structvariablename.fieldname
Structure definitions are best thought of as the definition of a new type of data, in fact, they can be considered as user-defined-types. Because structures are types, they should be defined early in a C++ program so that all functions can use the new type. It is recommended that the structure definition be placed directly above the list of function prototypes used by the program, as shown here.
// Top of source code
#include <iostream.h>
// other includes
struct video
{
char title[40];
float price;
char rating;
};
// Function prototypes
void main( void )
{
video v;
v.price = 23.95;
// other code
}
// Function definitions
In a program which simulates a card game, it would be useful to design a structure which defines a new type called playing_card. A playing card has a suit (Hearts, Diamonds etc) and a numerical value (from 1 to 13)...
struct playing_card
{
char suit;
int value;
};
In the program we would create a card like this:
playing_card topcard; // topcard is a variable of type playing_card
and to make it the Ace of Diamonds, we could write:
topcard.suit = 'D'; topcard.value = 1;
Of course a program which deals with (excuse the pun) one card only is not going to be very useful - but we'll see how to fix that in the next topic.
struct point
{
int x;
int y;
};
To create a new point variable, and to set its values to represent the position (2,13) on a plane, you would write the code:
point A; A.x = 2; A.y = 13;
Further, if you have two points, you could calculate the distance between them with this function:
float distance( point A, point B )
{ // assumes <math.h> included
int delta_x = A.x - B.x;
int delta_y = A.y - B.y;
return sqrt( pow(delta_x, 2 ) + pow( delta_y, 2 ) );
}
struct customer
{
char pin[5]; // 4 chars and a null
int acc_num;
float balance;
};
If a customer with account number 12345, has $1388.76 in their account and their PIN is "help", then their structure could be created like this:
customer richie_rich; richie_rich.balance = 1388.76; richie_rich.acc_num = 12345; strcpy( richie_rich.pin, "help" ); // assumes <string.h>
Topic 17: Structures.
Ace of Diamonds
Of course, your function should work for all playing cards which could be found in a normal deck.
Write a program which allows the user to enter the details of (only) three CDs and prints a tabular report showing the details.
Write a boolean function which returns TRUE (1) if a given point is inside a given circle or FALSE (0) if the point is outside the circle.
Topic 17: Structures.