180 likes | 363 Views
CSIS 113A Lecture 13. Structures. What Is A Structure. An abstract data type that the programmer defines You have used int, char, float, etc Now you can create your own type Person, BankAccoun, etc Precursor to creating classes
E N D
CSIS 113A Lecture 13 Structures Glenn Stevenson CSIS 113A MSJC
What Is A Structure • An abstract data type that the programmer defines • You have used int, char, float, etc • Now you can create your own type • Person, BankAccoun, etc • Precursor to creating classes • Understanding structures goes a long way to helping you understand classes Glenn Stevenson CSIS 113A MSJC
Defining a structure myStruct is a newly defined type it does not occupy any space You have to create an instance of it! Glenn Stevenson CSIS 113A MSJC
Creating an Instance • intvariable x is created • myStruct variables y, aStruct are created Glenn Stevenson CSIS 113A MSJC
Accessing Member Variables • You access member variables using the . (dot) operator Glenn Stevenson CSIS 113A MSJC
An Example #include <iostream>#include <ctime> using namespace std; struct TIME{int hour;int minute;int second; }; int main(){TIME t;int hour; cout << "Enter hour, minute, second " << endl; cin >> t.hour >> t.minute >> t.second; hour = t.hour; if(hour > 12) //Convert military hour to standard time hour-=12; cout << "The time you entered is " << hour << ":" << t.minute << ":" << t.second << endl; } Glenn Stevenson CSIS 113A MSJC
Initializing At Declaration struct TIME{int hour;int minute;int second; }; Glenn Stevenson CSIS 113A MSJC
Example #include <iostream>#include <ctime> using namespace std; struct TIME{int hour;int minute;int second; }; int main(){TIME t = {12,20,22}; cout << "The is " << t.hour << ":" << t.minute << ":" << t.second << endl; } Glenn Stevenson CSIS 113A MSJC
Passing Structures To Functions • Pass Structures that same way you do any other variable • Can pass by pointer, reference, value Glenn Stevenson CSIS 113A MSJC
ByValue #include <iostream>#include <ctime> using namespace std; void printTime(TIME tm); int main(){TIME t = {12,20,22};printTime(t); } void printTime(TIME tm){ cout << "The is " << tm.hour << ":" << tm.minute << ":" << tm.second << endl; } Glenn Stevenson CSIS 113A MSJC
ByReference #include <iostream>#include <ctime> using namespace std; void printTime(TIME &tm); int main(){TIME t; getTime(t); cout << t.hour << “ “ << t.minute << “ “ << t.second << endl; return 0; } void getTime(TIME &tm){ tm.hour = 12; tm.minute = 30; tm.second = 25; } Glenn Stevenson CSIS 113A MSJC
Pass By Pointer • Passing by pointer is a little different • When dealing with a pointer to a structure, you don’t use the dot operator • Instead you use the arrow operator • -> • A dash followed by greater than Glenn Stevenson CSIS 113A MSJC
By Pointer #include <iostream>#include <ctime> using namespace std; void printTime(TIME *tm); int main(){TIME t; getTime(&t); cout << t.hour << “ “ << t.minute << “ “ << t.second << endl; return 0; } void getTime(TIME *tm){ tm->hour = 12; tm->minute = 30; tm->second = 25; } Glenn Stevenson CSIS 113A MSJC
Structure & Arrays • Handled Just Like any other type array! Glenn Stevenson CSIS 113A MSJC
Accessing The Data • You use the subscript operator Glenn Stevenson CSIS 113A MSJC
Initialization At Declaration • Just like any other array • Watch your braces Glenn Stevenson CSIS 113A MSJC
Example #include <iostream>#include <ctime> using namespace std; struct TIME{int hour;int minute;int second; }; int main(){TIME t[3] = { {12,20,22}, {1,10,12}, {5,15,0},}; for(int i = 0; i < 3; i++) cout << "The time is " << t[i].hour << ":" << t[i].minute << ":" << t[i].second << endl; } Glenn Stevenson CSIS 113A MSJC
Passing Arrays to Functions • Just like any other array void printArray(TIME time[], int size);void fillArray(TIME time[], int size); int main(){ fillArray(t,3); printArray(t, 3); } void fillArray(TIME Time[], int size){ srand(time(0)); for(int i = 0; i < size; i++) { Time[i].hour = rand() % 13; Time[i].second = rand() % 61; Time[i].minute = rand() % 61; } } Glenn Stevenson CSIS 113A MSJC