Program to illustrates Mixed Inheritance Hierarchy with Abstract Classes


#include <iostream.h>
#include <string.h>
/*----------------------Class Interfaces-------------------------*/
class Account                                      // Abstract Base class
{
    protected :
        int number;
        char name[10];
    public :
        Account(int, char*);
        virtual void display() = 0;    // pure virtual function
};
class SavingsAccount : public Account
{
        int balance;
    public:
        SavingsAccount(int,char*,int);
        void display();
};
class Deposit                              // Abstract Base class
{
    protected :
        int amount;
        char maturity_date[9];
    public:
        Deposit(int, char*);
        virtual void display() = 0;    // pure virtual function
};
class DepositAccount : public Account, public Deposit
{                       
    protected :
        char opening_date[9];
    public:
        DepositAccount(int,char*,int,char*,char*);
        virtual void display() = 0;    // pure virtual function
};
class ShortTerm : public DepositAccount
{
        int no_of_months;
    public:
        ShortTerm(int,char*,int,char*,char*,int);
        void display();
};
class LongTerm : public DepositAccount
{
        int no_of_years;
        int loan_taken;
    public:
        LongTerm(int,char*,int,char*,char*,int,int);
        void display();
};
/*----------------------Class Implementations---------------------*/
// member function definitions for 'Account'
void Account::Account(int a, char* b)
{
    number = a;
    strcpy(name, b);
}
// member function definitions for 'SavingsAccount'
void SavingsAccount::SavingsAccount(int a, char* b, int c) :
                                    Account(a,b)
{
    balance = c;
}
void SavingsAccount::display()
{
    cout << "Savings Account Details --- \n";
    cout << "Number : " << number << "\n";
    cout << "Name   : " << name << "\n";
    cout << "Balance : " << balance << "\n";
}
// member function definitions for 'Deposit'
void Deposit::Deposit(int a, char* b)
{
    amount = a;
    strcpy(maturity_date, b);
}
// member function definitions for 'DepositAccount'
void DepositAccount::DepositAccount(int a, char* b, int c, char*
d, char* e)
                                : Account(a,b), Deposit(c,d)
{
    strcpy(opening_date, e);
}
// member function definitions for 'ShortTerm'
void ShortTerm::ShortTerm(int a, char* b, int c, char* d, char*
e, int f)
                                : DepositAccount(a,b,c,d,e)
{
    no_of_months = f;
}
void ShortTerm::display()
{
    cout << "Short Term Deposit Account Details --- \n";
    cout << "Number : " << number << "\n";
    cout << "Name   : " << name << "\n";
    cout << "Amount : " << amount << "\n";
    cout << "Maturity Date : " << maturity_date << "\n";
    cout << "Date of Opening : " << opening_date << "\n";
    cout << "Duration in Months : " << no_of_months << "\n";
}
// member function definitions for 'LongTerm'
void LongTerm::LongTerm(int a, char* b, int c, char* d, char* e,
int f, int g)
                                : DepositAccount(a,b,c,d,e)
{
    no_of_years = f;
    loan_taken = g;
}
void LongTerm::display()
{
    cout << "Long Term Deposit Account Details --- \n";
    cout << "Number : " << number << "\n";
    cout << "Name   : " << name << "\n";
    cout << "Amount : " << amount << "\n";
    cout << "Maturity Date : " << maturity_date << "\n";
    cout << "Date of Opening : " << opening_date << "\n";
    cout << "Duration in Years : " << no_of_years << "\n";
    cout << "Loan Taken        : " << loan_taken << "\n";
}
/*----------------------Class definitions ends here---------------*/
void main(void)
{
    Account *top1;            // base class pointer

    top1 = new SavingsAccount(1323, "Stavan", 10000);
    top1->display();
    delete top1;
    cout << "\n";

    DepositAccount *top2;         // base class pointer

    cout << "Deposit Display using pointer";
    cout <<    " of type base class 'DepositAccount' - \n";

    top2 = new ShortTerm(17099, "Kush", 25000, "12/05/02",     "12/02/02", 3);
    top2->display();
    delete top2;
    cout << "\n";

    top2 = new LongTerm(17169, "Samarth", 30000, "15/03/04",
                               "15/03/02", 2, 1000);
    top2->display();
    delete top2;
    cout << "\n";
}
/*
ouput
Savings Account Details ---
Number : 1323
Name   : Stavan
Balance : 10000

Deposit Display using pointer of type base class 'DepositAccount' -
Short Term Deposit Account Details ---
Number : 17099
Name   : Kush
Amount : 25000
Maturity Date : 12/05/02
Date of Opening : 12/02/02
Duration in Months : 3

Long Term Deposit Account Details ---
Number : 17169
Name   : Samarth
Amount : 30000
Maturity Date : 15/03/04
Date of Opening : 15/03/02
Duration in Years : 2
Loan Taken        : 1000

*/

0 comments:

Post a Comment

 
 
 
 


Copyright © 2012 http://codeprecisely.blogspot.com. All rights reserved |Term of Use and Policies|