Download presentation
Presentation is loading. Please wait.
Published byMargaret Miles Modified over 9 years ago
1
DCT1063 Programming 2 CHAPTER 5 ADVANCED DATA TYPE (part 1) Mohd Nazri Bin Ibrahim Faculty of Computer Media and Technology TATi University College nazri@tatiuc.edu.my
2
The ability to create your own data types is a powerful feature of C++. You can create your own data type using structures, union and class. In C++, structures and unions have both object-oriented and non-object-oriented attributes. This chapter discusses only their non-object-oriented features.
3
Structures In C++, a structure is a collection of variables that are referenced under one name. Structures are called aggregate data types because they consist of several different, yet logically connected, variables. Before a structure object can be created, the form of the structure must be defined. This is accomplished by means of a structure declaration. The variables that comprise the structure are called members (also called elements or fields) of the structure.
4
Structures(2) Generally, all members of the structure will be logically related to each other. We will define a structure that can hold the information relating to a company's inventory. An inventory record typically consists of several pieces of information. The following code fragment declares a structure that defines the item name, cost and retail price, number on hand, and resupply time for maintaining an inventory.
5
Structures(3) The keyword struct tells the compiler that a structure declaration is beginning. struct inv_type { char item[40]; // name of item double cost; // cost double retail; // retail price int on_hand; // amount on hand int lead_time; // number of days before resupply };
6
Structures(4) In the preceding declaration, no variable has actually been created. To declare an actual variable (i.e., a physical object) with this structure, you would write something like this: inv_type inv_var; You can also declare one or more variables at the same time that you define a structure, as shown here: struct inv_type { char item[40]; // name of item : } inv_varA, inv_varB, inv_varC;
7
Structures(5) It is important to understand that each structure variable contains its own copies of the structure’s members.
8
Accessing Structure Members Individual structure members are accessed through the use of a period (generally called the "dot" operator). The following code will assign the value 10.39 to the cost field of the structure variable inv_var, declared earlier. inv_var.cost = 10.39; Therefore, to print cost on the screen, you could write cout << inv_var.cost;
9
Accessing Structure Members(2) In the same fashion, the character array inv_var.item can be used to call gets( ), as shown here: gets(inv_var.item); If you want to access the individual elements of the array inv_var.item, you can index item. int t; for(t=0; inv_var.item[t]; t++) cout << inv_var.item[t];
10
Arrays of Structures Structures may be arrayed. To declare a 100- element array of structures of type inv_type (defined earlier), you would write inv_type invtry[100]; To access a specific structure within an array of structures, you must index the structure name. To display the on_hand member of the third structure, you would write cout << invtry[2].on_hand;
11
Passing Structures to Functions When a structure is used as an argument to a function, the entire structure is passed by using the standard call-by-value parameter passing mechanism. When using a structure as a parameter, remember that the type of the argument must match the type of the parameter. For example, the following program declares a structure called sample, and then a function called f1( ) that takes a parameter of type sample.
12
Passing Structures to Functions(2) // Pass a structure to a function. #include using namespace std; // define a structure type struct sample { int a; char ch; } ; void f1(sample parm); int main() { struct sample arg; // declare arg arg.a = 1000; arg.ch = 'X'; f1(arg); return 0; } void f1(sample parm) { cout << parm.a << " " << parm.ch << "\n"; } Here, both arg in main( ) and parm in f1( ) are of the same type. Thus, arg can be passed to f1( ).
13
Assigning Structures You can assign the contents of one structure to another as long as both structures are of the same type. // Demonstrate structure assignments. #include using namespace std; struct stype { int a, b; }; int main() { stype svar1, svar2; svar1.a = svar1.b = 10; svar2.a = svar2.b = 20; svar1.a=10; svar1.b=10; svar2.a=20; svar2.b=20;
14
cout << "Structures before assignment.\n"; cout << "svar1: " << svar1.a << ' ' << svar1.b; cout << '\n'; cout << "svar2: " << svar2.a << ' ' << svar2.b; cout << "\n\n"; svar2 = svar1; // assign structures cout << "Structures after assignment.\n"; cout << "svar1: " << svar1.a << ' ' << svar1.b; cout << '\n'; cout << "svar2: " << svar2.a << ' ' << svar2.b; return 0; }
15
This program displays the following output: Structures before assignment. svar1: 10 10 svar2: 20 20 Structures after assignment. svar1: 10 10 svar2: 10 10
16
Pointers to Structures and the Arrow Operator C++ allows pointers to structures in the same way that it allows pointers to any other type of variable. You declare a structure pointer as you would any other pointer variable, by putting an * in front of a structure variable's name. inv_type *inv_pointer; declares inv_pointer to be a pointer to data of that type: To find the address of a structure variable, you must place the & operator before the structure variable's name.
17
Pointers to Structures and the Arrow Operator(2) struct bal { float balance; char name[80]; } person; bal *p; // declare a structure pointer then p = &person; puts the address of person into the pointer p. The members of a structure can be accessed through a pointer to the structure.
18
Pointers to Structures and the Arrow Operator(3) You must use the –> operator to accesses balance through p: p->balance
19
Exercise Exercise on C primer Plus 5 th edition. Chapter 14. 1. What’s wrong with this template? structure { char itable; Int num[20]; Char * togs }
20
Exercise(2) 2. #include struct house float sqft; Int rooms; Int stories; Char address[40]; }; Int main(void) Struct house fruzt={1560.0,6,1,”22 Spiffo Road”); Struct house *sign; Sigh = &fruzt;
21
cout stories<<endl; cout<<fruzt.address<<endl; cout address[3]<<fruzt.address[4]<<endl;
22
sqftroomsstoriesaddress 156.06122 spiffo Road fruzt *sign Output??
23
Summary A C++ structure provide the means to store several data items, usually of different types, int the same object (name). The membership dot (.) operator enables you to access the individual members of a structures. If you have a pointer to a structure, you can use the pointer and the indirect membership ( arrow operator (->) instead of a name and the dot operator to access individual members.
24
Summary(2) The typedef facility enables you to create a group of symbolic integer constants (enumeration constant) and to define an associated enumeration type.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.