Download presentation
Presentation is loading. Please wait.
1
Glenn Stevenson CSIS 113A MSJC
CSIS 113A Lecture 13 Structures Glenn Stevenson CSIS 113A MSJC
2
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
3
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
4
Glenn Stevenson CSIS 113A MSJC
Creating an Instance int variable 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
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
7
Initializing At Declaration
struct TIME { int hour; int minute; int second; }; Glenn Stevenson CSIS 113A MSJC
8
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
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
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
11
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
12
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
13
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
14
Glenn Stevenson CSIS 113A MSJC
Structure & Arrays Handled Just Like any other type array! Glenn Stevenson CSIS 113A MSJC
15
Glenn Stevenson CSIS 113A MSJC
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
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
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.