year
, month
, and day
:struct date
{
int year;
int month;
int day;
};
My simple analogy of this would be how you store files on your computer. Most of us eventually start making folders to group similar things in different places. For example you may have a folder called "cpp
" in which you store all your C++ source files:![]() |
... so using this analogy, struct date would look like ... |
![]() |
struct
keyword in the above example we're not creating any variables. What we're doing is constructing a blue print type dealy FOR making variables. To actually create variables using this "blue print" we simply put the name of the struct
you made, followed by the variable you want to create. So it follows this form:[struct type] [variable];
date dt;
date md;
date dob;
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;
}
/* ------------------------------------------------------------------------- */
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
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]
year
part of dt
: dt.year
. Or month
: dt.month
.
/* -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;
}
/* ------------------------------------------------------------------------- */
/* -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;
}
/* ------------------------------------------------------------------------- */
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;
}
/* ------------------------------------------------------------------------- */
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;
}
/* ------------------------------------------------------------------------- */
Page Content & Design ©1998 By Neil C. Obremski |
![]() home |
![]() prfce |
![]() loop |
![]() refs |
![]() ptrs |
![]() struct |
![]() class |
![]() array |
![]() rsrc |
![]() site |
![]() indx |
Wed May 12 21:45:26 1999 |