home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / virtl.cpp < prev    next >
Text File  |  1994-04-09  |  3KB  |  94 lines

  1. /*----------------------------------------------------------------------------*/
  2. /* vdemo.cpp                                                                           */
  3. /*                                                                            */
  4. /* Virtual base class / multiple deriviation example                          */
  5. /* Demonstrates constructor firing pattern and order, and                     */
  6. /* the use of the virtual keyword in derivations.                             */
  7. /*                                                                            */
  8. /* Change the definition of __VIRTUAL__ to "virtual" to make the derivations  */
  9. /* virtual; leave it as-is to see what happens in multiple derivations from   */
  10. /* the same base class.                                                       */
  11. /*                                                                            */
  12. /* (c) Larry Morley, 1994                                                     */
  13. /*----------------------------------------------------------------------------*/
  14.  
  15. #define __VIRTUAL__ virtual
  16. //#define __VIRTUAL__
  17.  
  18. #include <stdio.h>
  19.  
  20. int main(void);
  21.  
  22. /*----------------------------------------------------------------------------*/
  23.  
  24. class Account
  25. {
  26.    private:
  27.  
  28.       int balance;
  29.  
  30.    public:
  31.  
  32.       Account()
  33.       {
  34.          printf("Constructor for Account.\n");
  35.       }
  36.  
  37.       int &Balance()
  38.       {
  39.          return balance;
  40.       }
  41. };
  42.  
  43. /*----------------------------------------------------------------------------*/
  44.  
  45. class CheckingAccount : public __VIRTUAL__ Account
  46. {
  47.    public:
  48.  
  49.       CheckingAccount()
  50.       {
  51.          printf("Constructor for Checking Account.\n");
  52.          Balance() = 10;
  53.       }
  54. };
  55.  
  56. /*----------------------------------------------------------------------------*/
  57.  
  58. class SavingsAccount : public __VIRTUAL__ Account
  59. {
  60.    public:
  61.  
  62.       SavingsAccount()
  63.       {
  64.          printf("Constructor for Savings Account.\n");
  65.          Balance() = 20;
  66.       }
  67. };
  68.  
  69. /*----------------------------------------------------------------------------*/
  70.  
  71. class CheckingAndSavingsAccount : public CheckingAccount, public SavingsAccount
  72. {
  73.    public:
  74.  
  75.       CheckingAndSavingsAccount()
  76.       {
  77.          printf("Constructor for CheckingAndSavings Account.\n");
  78.       }
  79. };
  80.  
  81. /*----------------------------------------------------------------------------*/
  82.  
  83. int main()
  84. {
  85.    CheckingAndSavingsAccount account;
  86.  
  87.    printf("Balance 1: %d.\n",account.SavingsAccount::Balance());
  88.    printf("Balance 2: %d.\n",account.CheckingAccount::Balance());
  89.  
  90.    return 0;
  91. }
  92.  
  93. /*----------------------------------------------------------------------------*/
  94.