CISC 1600 Lecture 23: structure basics uses:initialize,array, nested, loop

4 Pages
Unlock Document

Fordham University
Computer and Information Sciences
CISC 1600
Michael M.Kadri

Structure does not define any variables, it simply gives an outline of how things are organized/look. struct course { //name of structure/ tag string discipline; // same type can be in comma-separated list: courseTitle; int courseNumber; short credits; }; //note the semi-colon here Defining variable → put the tag then the name of variable you want to call. course pChem; course colonialHist; // both pChem and colonialHist will have the same structure, ie each of them will has 4 members like discipline, courseNumber,couseTitle, and credits. Nested Structures a structure within another structure as a member: struct PersonInfo { string name, address, city; }; struct Student { int studentID; PersonInfo pData; //PersonInfo is nested inside Student short yearInSchool; double gpa; }; Use the dot (.) operator to refer to members of struct variables: variable name.tag cin >> stu1.studentID; //user defined getline(cin,; //user defined string if (a.studentID == b.studentID) //comparing cout << bill.yearInSchool; //display Student s; //nested structure = "Joanne"; = "Tulsa"; Initialize structure: cannot skip members of a structure during the initialization process unless all are left uninitialized. You CANNOT initialized in inside the declaration of structure because structure does not define any variables! 1. At definition: Student s = {11465, "Joan", 2, 3.75}; 2.member-by-member after definition: = "Joan"; s.gpa = 3.75; Passing struct var to functions: uses the tag to pass by value or by reference ● pass members of struct variables to functions: computeGPA(stu.gpa); ● pass entire struct variables to functions: showData(stu); ● reference parameter /pass by reference struct InventoryItem pass by value slows down a program, { waste space int partNum; // Part number string description; // Item description pass by reference parameter will speed int onHand; // Units on hand up program, but function may change double price; // Unit price data in structure }; void showItem(const InventoryItem); const reference parameter allows read- // Argument passed by value only access void getItem(InventoryItem&); // Argument passed by reference //void showItem(const InventoryItem&); Array in Structures: ● circle circn[100]; an array name circn with a structure of circle and 100 elements in it, ie 100 with same structure ● circn[0] is the first structure in the array and the last structure in the array is circn[99]. ● To access a member of one of these array elements, we still use the dot operator. For instance, circn[9].circumference gives the circumference member of circn[9]. ● loop the first 30 circles: for (int count = 0; count < 30; count++) { cout << circn[count].centerX << endl; cout << circn[count].centerY << endl; cou
More Less

Related notes for CISC 1600

Log In


Don't have an account?

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.