/************************* STRUCT3.CPP ****************************************
* Structures example 1                                             1997-11-21 *
*                                                                   Agner Fog *
*                                                                             *
* There are different ways of passing a structure to a function:              *
*                                                                             *
* Example STRUCT1.CPP passes a structure to a function by copying it into a   *
* parameter. This is convenient, but inefficient if the structure is big or   *
* has a constructor.                                                          *
*                                                                             *
* Example STRUCT2.CPP passes a structure to a function through a pointer.     *
*                                                                             *
* Example STRUCT3.CPP passes a structure to a function implicitly by making   *
* the function a member of the structure.                                     *
******************************************************************************/
#include <iostream.h>
#include <string.h>
#include <conio.h>

// this structure defines a commodity stored in a shop
struct commodity
{
   char name[80];
   int stock;
   float price;
   void SellOut (void);
};

// this function sets a commodity on sale if the stock is too high
void commodity::SellOut ()
{
   if (stock > 1000)
   {  // stock too high
      price *= 0.75;  // reduce price by 25%
      cout << name << " now on sale for only " << price;
   }
}

void main()
{
   commodity teddybears, dolls, etc;
   strcpy (teddybears.name, "Teddybears");
   teddybears.stock = 5000;
   teddybears.price = 99.00;
   teddybears.SellOut();
   getch();
}
