home *** CD-ROM | disk | FTP | other *** search
- /*----------------------------------------------------------------------------*/
- /* vdemo.cpp */
- /* */
- /* Virtual base class / multiple deriviation example */
- /* Demonstrates constructor firing pattern and order, and */
- /* the use of the virtual keyword in derivations. */
- /* */
- /* Change the definition of __VIRTUAL__ to "virtual" to make the derivations */
- /* virtual; leave it as-is to see what happens in multiple derivations from */
- /* the same base class. */
- /* */
- /* (c) Larry Morley, 1994 */
- /*----------------------------------------------------------------------------*/
-
- #define __VIRTUAL__ virtual
- //#define __VIRTUAL__
-
- #include <stdio.h>
-
- int main(void);
-
- /*----------------------------------------------------------------------------*/
-
- class Account
- {
- private:
-
- int balance;
-
- public:
-
- Account()
- {
- printf("Constructor for Account.\n");
- }
-
- int &Balance()
- {
- return balance;
- }
- };
-
- /*----------------------------------------------------------------------------*/
-
- class CheckingAccount : public __VIRTUAL__ Account
- {
- public:
-
- CheckingAccount()
- {
- printf("Constructor for Checking Account.\n");
- Balance() = 10;
- }
- };
-
- /*----------------------------------------------------------------------------*/
-
- class SavingsAccount : public __VIRTUAL__ Account
- {
- public:
-
- SavingsAccount()
- {
- printf("Constructor for Savings Account.\n");
- Balance() = 20;
- }
- };
-
- /*----------------------------------------------------------------------------*/
-
- class CheckingAndSavingsAccount : public CheckingAccount, public SavingsAccount
- {
- public:
-
- CheckingAndSavingsAccount()
- {
- printf("Constructor for CheckingAndSavings Account.\n");
- }
- };
-
- /*----------------------------------------------------------------------------*/
-
- int main()
- {
- CheckingAndSavingsAccount account;
-
- printf("Balance 1: %d.\n",account.SavingsAccount::Balance());
- printf("Balance 2: %d.\n",account.CheckingAccount::Balance());
-
- return 0;
- }
-
- /*----------------------------------------------------------------------------*/