Here's where I explain the way you take and use the seperate pieces of one structure. Below is a small sample program that does not use a structure. It declares three integer variables that make up a date (year
, month
, and day
) and then gets user input to fill these variables with values.
/* -example 1--------------------------------------------------------------- */
#include <iostream.h>
void main()
{
int year, month, day;
cout << "Please input the following: ";
cout << "Year :";
cin >> year;
cout << "Month :";
cin >> month;
cout << "Day :";
cin >> day;
}
/* ------------------------------------------------------------------------- */
Now the following is an example program that does the same thing except it uses a structure. Take a look, and explanation follows.
/* -example-2--------------------------------------------------------------- */
#include <iostream.h>
struct date
{
int year;
int month;
int day;
};
void main()
{
date dt;
cout << "Please input the following: ";
cout << "Year :";
cin >> dt.year;
cout << "Month :";
cin >> dt.month;
cout << "Day :";
cin >> dt.day;
}
/* ------------------------------------------------------------------------- */
We declared the structure date
to contain three pieces, which of course are year
, month
, and day
. Now then in main()
we create a variable called dt
using our date
structure. In essence this creates those three pieces under that one name. So dt
has three pieces that call it momma:
dt
|-- year
|-- month
\-- day
Now we can use these pieces almost just like normal variables. Because that's all they are! They're normal variables just "grouped" under the one object dt
. To access variables within a structure you put the name of the struct
followed by a period (.
) and then lastly the name of the variable within that you wish to access:
[struct].[variable]
So say we want to use the year
part of dt
: dt.year
. Or month
: dt.month
.
How does this become useful to us? Well, what if you want to have the date of birth for three people. You could do it without structs, most definetly. The following is an example of that:
/* -example-3--------------------------------------------------------------- */
#include <iostream.h>
void main()
{
int neil_dob_year, neil_dob_month, neil_dob_day;
int missy_dob_year, missy_dob_month, missy_dob_day;
int jae_dob_year, jae_dob_month, jae_dob_day;
neil_dob_year = 1979;
neil_dob_month= 8;
neil_dob_day = 19;
missy_dob_year = 1977;
missy_dob_month= 10;
missy_dob_day = 30;
}
/* ------------------------------------------------------------------------- */
But this is so unelegant. It requires us to declare many variables even though each person's date of birth contain common things. So below is the same thing except using structs.
/* -example-4--------------------------------------------------------------- */
#include <iostream.h>
struct date;
{
int year, month, day;
}
void main()
{
date neil_dob;
date missy_dob;
date jae_dob;
neil_dob.year = 1979;
neil_dob.month= 8;
neil_dob.day = 19;
missy_dob.year = 1977;
missy_dob.month= 10;
missy_dob.day = 30;
}
/* ------------------------------------------------------------------------- */
Actually no ... structs and functions are a good combination. Though they may seem difficult, it's actually quite simple and very convenient. Say for example we want to make a function that tells you the date when you pass three parameters: y
(year), m
(month), and d
(day). Well, below is a simple example of this.
/* -example-5--------------------------------------------------------------- */
#include <iostream.h>
void print_date(int y, int m, int d);
void main()
{
int year, month, day;
year = 1979;
month= 8;
day = 19;
print_date(year,month,day);
}
void print_date(int y, int m, int d)
{
cout << "The date is: " << m << '/' << d << '/' << y;
}
/* ------------------------------------------------------------------------- */
In main()
we create three integers (year
, month
, and day
). We pass them to print_date()
where y
becomes what we passed as year
, m
what we passed as month
, etc. And then in the function I simply say, "The date is: month/day/year"
. This requires that we pass three parameters which can become a hassle. Sometimes its just easier to pass one. So below is the version using a structure.
/* -example-6--------------------------------------------------------------- */
#include <iostream.h>
struct date
{
int year, month, day;
};
void print_date(date d);
void main()
{
date dt;
dt.year = 1979;
dt.month= 8;
dt.day = 19;
print_date(dt);
}
void print_date(date d)
{
cout << "The date is: " << d.month << '/' << d.day << '/' << d.year;
}
/* ------------------------------------------------------------------------- */