Structures as Members of a Structure

Since members of structures can be of any type, members can be other structures:
struct CarpetCost     
{
  InputCost input;   // represents input costs
  TotalCost total;   // represents calculated costs
};
Now, we can declare a variable of type CarpetCost:
CarpetCost cost;
Accessing members is only slightly more involved. Example:
int yards = 17;
float price = 12.75;

cost.input.sqYards = yards;
cost.input.costPerSqYard = price;
cost.total.carpetCost = yards * price;
To use this structure in Lab #2, we would do something like this:
void getData(int customer, CarpetCost &cost);
void calculateCosts(CarpetCost &cost);
void printResults(int customer, CarpetCost cost);
Previous page

Back to Lesson 14 Index
Back to Outline