CIS3355
HOW DOES STRUCT WORK?
|
THE FIRST THINK WE HAVE TO DO IS DECLARE THE
STRUCTURE. |
struct PayRoll
{
int empNumber;
char name[25];
float hours;
float payRate;
float grossPay;
};
|
HERE WE ARE DECLARING THE STRUCTURE AS
'PAYROLL' AND IT CONTAINS FIVE MEMBERS: empNumber, name, hours,
payRate, grossPay. |
|
HERE WE DON'T DECLARE A VARIABLE, WE ARE ONLY
DESCRIBING TO THE COMPILER OF WHAT THE STRUCTURE IS MADE OF, IN
OTHER WORDS WE CREATED A NEW DATA TYPE CALL PayRoll. TO DEFINE
A VARIABLE OF THE NEW DATA TYPE THAT WE CREATED WE SIMPLY DO THE
FOLLOWING: |
PayRoll deptHead;
|
TO DECLARE MORE VARIABLES OF THE NEW CREATED
DATA TYPE YOU WILL USE THE SAME FORMAT AS A INDEPENDENT
VARIABLE: |
PayRoll deptHead,employee,
accounting;
|
BY DOING THIS YOU ARE STATING TO THE COMPILER
THAT depHead, employee, and accounting are all the same data
type PayRoll. IT WILL LOOK SOMETHING AS FOLLOWS: |
deptHead
empNumber |
|
name |
|
hours |
|
payRate |
|
grossPay |
|
foreman
empNumber |
|
name |
|
hours |
|
payRate |
|
grossPay |
|
associate
empNumber |
|
name |
|
hours |
|
payRate |
|
grossPay |
|
| EACH OF THE DECLARE VARIABLES WILL HAVE ITS OWN
MEMBERS DECLARED IN THE STRUCTURE.
|
| IF YOU UNDERSTAND EVERYTHING UP TO THIS POINT THEN
YOU ARE READY TO DO A ARRAY OF STRUCTURES. FOR EXAMPLE CONSIDER
THE FOLLOWING STRUCTURE:
|
struct BookInfo
{
char title[50];
char author[30];
char publisher[25];
foat price;
};
| WHAT WE DID WAS CREATE A NEW DATA TYPE CALL BookInfo,
NEXT WE ARE GOING TO DECLARE AN ARRAY WITH 20 ELEMENTS CALL
bookList. |
BookInfo bookList[20];
| FROM HERE EVERYTHING ELSE WORKS MORE OR LESS LIKE A
REGULAR ARRAY, bookList[0] WILL BE THE FIRST STRUCTURE OF THE
ARRAY. |
| SO HOW DO WE ACCESS ANY MEMBER IN THE STRUCTURE ARRAY?
WE DO THE FOLLOWING, FOR EXAMPLE TO ACCESS bookList[4] IN TITLE
WE WILL USE THE FOLLOWING SYNTAX: |
bookList[4].title
| THE FOLLOWING WILL BE AN EXAMPLE OF A LOOP OF AN ARRAY TO
DISPLAY THE INFORMATION STORE IN EACH ELEMENT: |
for (int index = 0; index < 20; index++)
{
cout << bookList[index].title <<endl;
cout << bookList[index].author <<endl;
cout << bookList[index].publisher <<endl;
cout << bookList[index].price <<endl;
}
| AN IMPORTANT THINK TO MENTION HERE IS THAT title, author,
publisher, price ARE ALSO ARRAY SO THERE INDIVIDUAL ELEMENTS CAN
BE ACCESS TOO.
|
bookList[2].publisher[3] = 't'
| THESE WILL STORE CHARACTER 't' IN publisher WHICH IS A
MEMBER OF bookList[2]. |
HOPEFULLY THESE
INFORMATION ABOUT ABSTRACT DATA TYPE STRUCT HELP YOU UNDERSTAND A
LITTLE BETTER ABOUT STRUCTURES ARRAYS.
CIS3355
WHAT IS AN ARRAY OF STRUCTS
(ABSTRACT DATA TYPE)?
|
AN ABSTRACT DATA TYPE IS A
DATA TYPE CREATED BY THE PROGRAMMER, AND IS COMPOSED
OF ONE OR MORE PRIMITIVE DATA TYPES. THE PROGRAMMER
CAN DECIDE WHAT VALUES ARE ACCEPTABLE FOR THE DATA
TYPE. |
|
ONE OF THE RESTRICTION OF AN
ARRAY THAT WE HAVE TO DEAL WITH IS THAT ALL THE
ELEMENTS OF AN ARRAY NEED TO BE FROM THE SAME DATA
TYPE. STRUCTS AVOIDS THIS RESTRICTION. |
THIS COULD BE A GOOD
EXAMPLE THAT CAN HELP YOU GET A BETTER PICTURE:
SUPPOSE A PROGRAM IS CREATED
TO SIMULATE A TWELVE-HOUR CLOCK. THE PROGRAM COULD
CONTAIN THREE ADTs: HOURS, MINUTES AND SECONDS. THE
RANGE OF VALUES FOR THE HOURS DATA TYPE WOULD BE THE
INTERGERS 1 THROUGH 12. THE RANGE OF VALUES FOR THE
MINUTES AND SECONDS DATA TYPES WOULD BE 0 THROUGH 59.
IF AN HOURS OBJECT IS SET TO 12 AND THEN INCREMENTED, IT
WILL THEN TAKE ON THE VALUE 1. LIKEWISE IF A MINUTES
OBJECT OR A SECONDS OBJECT IS SET TO 59 AND THEN
INCREMENTED, THEY WILL TAKE ON THE VALUE 0.(standard
version of Starting Out with C++ 3rd Edition, Tony
Gaddis)
This is the sintax use for
structures:
struct name
{
variable
declaration;
// ... more
declarations may follow
};
|
What is an array of structs and how does it work? |
DEFINITION & EXAMPLES
HOW DOES IT WORK
LINKS
|
THIS WEBSITE WILL HELP YOU
UNDERSTAND THE QUESTION ABOVE. IT WILL NOT ONLY PROVIDE
YOU WITH THE BASIC DEFINITION, BUT IT WILL GIVE YOU
EXAMPLES AND HELPFUL TIPS.
|
|
|