home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 13 / CDA13.ISO / cdactual / demobin / share / program / C / ANSICPP.ZIP / EX09021.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-24  |  1.0 KB  |  48 lines

  1. // ex09021.cpp
  2. // Virtual base classes
  3. #include <iostream.h>
  4. #include <string.h>
  5.  
  6. struct BillingItem    {
  7.     char name[25];
  8.     int cost;
  9. };
  10.  
  11. class Product : public virtual BillingItem    {
  12.     int qty_sold;
  13. public:
  14.     Product(char *nm, int qty, int cst)
  15.         { qty_sold = qty; strcpy(name, nm); cost = cst; }
  16.     virtual void display() { cout << qty_sold; }
  17. };
  18.  
  19. class Service : public virtual BillingItem    {
  20.     int manhours;
  21. public:
  22.     Service(char *nm, int mh, int cst)
  23.         { manhours = mh; strcpy(name, nm); cost = cst; }
  24.     virtual void display() { cout << manhours; }
  25. };
  26.  
  27. class Installation : public Product, public Service    {
  28. public:
  29.     Installation(char *nm, int qty, int hrs, int cst)
  30.         : Product(nm, qty, cst), Service(nm, hrs, cst)    { }
  31.     void display();
  32. };
  33. void Installation::display()
  34. {
  35.     cout << "\nInstalled ";
  36.     Product::display();
  37.      cout << ' ' << name << "s";
  38.     cout << "\nLabor: ";
  39.     Service::display();
  40.     cout << " hours";
  41.     cout << "\nCost: $" << cost;
  42. }
  43. main()
  44. {
  45.     Installation inst("refrigerator", 2, 3, 75);
  46.     inst.display();
  47. }
  48.