Structures Structure Basics A structure is a collection

Structures

Structure Basics • A structure is a collection of simple variables. • The variables in a structure can be of different types: Some can be int, some can be float, and so on. • unlike the array. • The data items in a structure are called the members of the structure

Example • • • • • • #include <iostream> using namespace std; //////////////////////////////// struct part //declare a structure { int modelnumber; //ID number of widget int partnumber; //ID number of widget part float cost; //cost of part }; //////////////////////////////// int main() { part 1; / /define a structure variable part 1. modelnumber = 6244; / /give values to structure members part 1. partnumber = 373; part 1. cost = 217. 55 F; //display structure members cout << “Model ” << part 1. modelnumber; cout << “, part ” << part 1. partnumber; cout << “, costs $” << part 1. cost << endl; return 0; }

• • • • int main() { //initialize variable part 1 = { 6244, 373, 217. 55 F }; part 2; //define variable //display first variable cout << “Model ” << part 1. modelnumber; cout << “, part ” << part 1. partnumber; cout << “, costs $” << part 1. cost << endl; part 2 = part 1; //assign first variable to second //display second variable cout << “Model “ << part 2. modelnumber; cout << “, part “ << part 2. partnumber; cout << “, costs $” << part 2. cost << endl; return 0; }

• • • #include &<iostream> using namespace std; //////////////////////////////// struct part //specify a structure { int modelnumber; //ID number of widget int partnumber; //ID number of widget part float cost; //cost of part }; ////////////////////////////////

• • • • • • • • #include <iostream> using namespace std; //////////////////////////////// struct Distance //English distance { int feet; float inches; }; //////////////////////////////// int main() { Distance d 1, d 3; //define two lengths Distance d 2 = { 11, 6. 25 }; //define & initialize one length //get length d 1 from user cout << “n. Enter feet: ”; cin >> d 1. feet; cout << “Enter inches: ”; cin >> d 1. inches; //add lengths d 1 and d 2 to get d 3. inches = d 1. inches + d 2. inches; //add the inches d 3. feet = 0; //(for possible carry) if(d 3. inches >= 12. 0) //if total exceeds 12. 0, { //then decrease inches by 12. 0 d 3. inches -= 12. 0; //and d 3. feet++; //increase feet by 1 } d 3. feet += d 1. feet + d 2. feet; //add the feet //display all lengths cout << d 1. feet << “’-” << d 1. inches << “” + ”; cout << d 2. feet << “’-” << d 2. inches << “” = ”; cout << d 3. feet << “’-” << d 3. inches << “”n”; return 0; }

Structure within Structure • Structures within structures can be used in Object Oriented Programming.

Example #include &<iostream> using namespace std; //////////////////////////////// struct Distance //English distance \ inner structure { int feet; float inches; }; //////////////////////////////// struct Room //rectangular area \ outer structure { Distance length; //length of rectangle \inner structure Distance width; //width of rectangle };

int main() { Room dining; //define a room dining. length. feet = 13; //assign values to room dining. length. inches = 6. 5; dining. width. feet = 10; dining. width. inches = 0. 0; //convert length & width float l = dining. length. feet + dining. length. inches/12; float w = dining. width. feet + dining. width. inches/12; //find area and display it cout << “Dining room area is ” << l * w << “ square feetn” ; return 0; }

Difference Structure Class • By default structure data is public. • A classes uses objects for instantiation. • Constructors and destructors are used for creating instance of class. • A structure is a collection of variables. It keeps information together. • Structure are value type • User defined data type which contains data and methods to manipulate data. • By default class data is private • Inheritance and polymorphism are used in classes. • Data encapsulation and Information hiding • An object is a variable of a Class. • Class are reference type.

Enumerations • user-defined data types • A different approach to defining your own data type is the enumeration. • Enumerated types work when you know in advance a finite (usually short) list of values that a data type can take on

Example • • • • #include &<iostream> using namespace std; //specify enum type enum days_of_week { Sun, Mon, Tue, Wed, Thu, Fri, Sat }; int main() { days_of_week day 1, day 2 ; //define variables //of type days_of_week day 1 = Mon; //give values to day 2 = Thu; //variables int diff = day 2 - day 1; //can do integer arithmetic cout << “Days between = “ << diff << endl; if(day 1 < day 2) //can do comparisons cout << “day 1 comes before day 2n”; return 0; }

• Structures are stored on stack because they are value type! • Classes are stored on heap because they are reference type. • Local variables are stored on the stack. Whereas global and stack variables are stored on heap.
- Slides: 13