dct1063 programming 2 chapter 5 advanced data type (part 1) mohd nazri bin ibrahim faculty of...
TRANSCRIPT
DCT1063 Programming 2
CHAPTER 5 ADVANCED DATA TYPE (part 1)
Mohd Nazri Bin IbrahimFaculty of Computer Media and Technology
TATi University [email protected]
• 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.
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.
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.
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
};
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;
Structures(5)
• It is important to understand that each structure variable contains its own copies of the structure’s members.
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;
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];
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;
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.
Passing Structures to Functions(2)// Pass a structure to a function.#include <iostream>using namespace std;// define a structure typestruct 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( ).
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 <iostream>
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;
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;
}
This program displays the following output:
Structures before assignment.
svar1: 10 10
svar2: 20 20
Structures after assignment.
svar1: 10 10
svar2: 10 10
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.
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.
Pointers to Structures and the Arrow Operator(3)
• You must use the –> operator to accesses balance through p:
p->balance
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
}
Exercise(2)
• 2.#include<iostream.h>
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;
• cout<<fruzt.rooms<< sign-> stories<<endl;• cout<<fruzt.address<<endl;• cout<<sign-
>address[3]<<fruzt.address[4]<<endl;
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.