Structured Data Type
A structured data type is a type in which each value is a collection of component items.
C++ Structured Type
thisAnimal
anotherAnimal
struct AnimalType
enum HealthType { Poor, Fair, Good, Excellent } ;
struct AnimalType // declares a struct data type
{ // does not allocate memory
long id ;
string name ;
string genus ;
string species ; struct members
string country ;
int age ;
float weight ;
HealthType health ;
} ;
AnimalType thisAnimal ; // declare variables of AnimalType
AnimalType anotherAnimal ;
struct
type DeclarationSYNTAX
struct TypeName // does not allocate memory
{
MemberList
} ;
MemberList
SYNTAXDataType MemberName ;
DataType MemberName ;
.
.
.
struct
type DeclarationThe struct declaration names a type and names the members of the struct.
It does not allocate memory for any variables of that type!
You still need to declare your struct variables.
More about
If the struct type declaration precedes all functions it will be visible throughout the rest of the file. If it is placed within a function, only that function can use it.
It is common to place struct type declarations with TypeNames in a (.h) header file and #include that file.
It is possible for members of different struct types to have the same identifiers. Also a non-struct variable may have the same identifier as a structure member.
Accessing
struct MembersDot ( period ) is the member selection operator.
After the struct type declaration, the various members can be used in your program only when they are preceded by a struct variable name and a dot.
EXAMPLES
thisAnimal.weight
anotherAnimal.country
Valid operations on a struct member depend only on its type
thisAnimal.age = 18;
thisAnimal.id = 2037581;
cin >> thisAnimal.weight;
getline ( cin, thisAnimal.species );
thisAnimal.name = "giant panda";
thisAnimal.genus[ 0 ] = toupper (thisAnimal.genus[ 0 ] ) ;
thisAnimal.age++;
Aggregate Operation
Aggregate
struct Operationsassignment to another struct variable of same type,
pass to a function as argument
(by value or by reference),
return as value of a function
Examples of
aggregate
anotherAnimal = thisAnimal ; // assignment
WriteOut(thisAnimal); // value parameter
ChangeWeightAndAge(thisAnimal); // reference parameter
thisAnimal = GetAnimalData( ); // return value of function
NOW WE’LL WRITE THE 3 FUNCTIONS USED HERE . . .
void WriteOut( /* in */ AnimalType thisAnimal)
// Prints out values of all members of thisAnimal
// Precondition: all members of thisAnimal are assigned
// Postcondition: all members have been written out
{
cout << "ID # " << thisAnimal.id << thisAnimal.name << endl ;
cout << thisAnimal.genus << thisAnimal.species << endl ;
cout << thisAnimal.country << endl ;
cout << thisAnimal.age << " years " << endl ;
cout << thisAnimal.weight << " lbs. " << endl ;
cout << "General health : " ;
WriteWord ( thisAnimal.health ) ;
}
Passing a
struct Type by Referencevoid ChangeAge ( /* inout */ AnimalType& thisAnimal )
// Adds 1 to age
// Precondition: thisAnimal.age is assigned
// Postcondition: thisAnimal.age == thisAnimal.age@entry + 1
{
thisAnimal.age++ ;
}
AnimalType GetAnimalData ( void )
// Obtains all information about an animal from keyboard
// Postcondition:
// Function value == AnimalType members entered at kbd
{
AnimalType thisAnimal ;
char response ;
do { // have user enter all members until they are correct
.
.
.
} while (response != ‘Y’ ) ;
return thisAnimal ;
}
Hierarchical Structures
The type of a struct member can be another struct type. This is called nested or hierarchical structures.
Hierarchical structures are very useful when there is much detailed information in each record.
FOR EXAMPLE . . .
struct MachineRec
Information about each machine in a shop contains:
an idNumber,
a written description,
the purchase date,
the cost,
and a history (including failure rate, number of days down, and date of last service).
struct DateType
{ int month ; // Assume 1 . . 12
int day ; // Assume 1 . . 31
int year ; // Assume 1900 . . 2050
};
struct StatisticsType
{ float failRate ;
DateType lastServiced ; // DateType is a struct type
int downDays ;
} ;
struct MachineRec
{ int idNumber ;
string description ;
StatisticsType history ; // StatisticsType is a struct type
DateType purchaseDate ;
float cost ;
} ;
MachineRec machine ;