1 / 18

CSIS 113A Lecture 13

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

Download Presentation

CSIS 113A Lecture 13

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSIS 113A Lecture 13 Structures Glenn Stevenson CSIS 113A MSJC

  2. 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

  3. 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

  4. Creating an Instance • intvariable x is created • myStruct variables y, aStruct are created Glenn Stevenson CSIS 113A MSJC

  5. Accessing Member Variables • You access member variables using the . (dot) operator Glenn Stevenson CSIS 113A MSJC

  6. 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

  7. Initializing At Declaration struct TIME{int hour;int minute;int second; }; Glenn Stevenson CSIS 113A MSJC

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. Structure & Arrays • Handled Just Like any other type array! Glenn Stevenson CSIS 113A MSJC

  15. Accessing The Data • You use the subscript operator Glenn Stevenson CSIS 113A MSJC

  16. Initialization At Declaration • Just like any other array • Watch your braces Glenn Stevenson CSIS 113A MSJC

  17. 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

  18. 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

More Related