dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: Standard C++98 Banking System example with OOPS

  1. #1
    Join Date
    Jul 2001
    Posts
    27

    Exclamation Standard C++98 Banking System example with OOPS

    Hello friends can anyone able to give me link for simple banking system example in C++ but with OOPS concept like Inheritance, and saving data with vector or map. Because i found many many examples but all are using old C style and conio.h file.

    I want pure standard C++98 example not a old TC example with only cin and cout.

    Kindly refer.

    Thank you.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,525

    Re: Standard C++98 Banking System example with OOPS

    That's a really vague specification. You should design your system and then look for examples of the principles of your design. The first step is to figure out the basic structure of your app rather than looking for something kind of similar and hoping it fits. Real banking systems would be quite complex.

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,761

    Re: Standard C++98 Banking System example with OOPS

    Quote Originally Posted by P_N_Data_System
    I want pure standard C++98 example not a old TC example with only cin and cout.
    Why not a C++11+ example?

    Anyway, I recommend that you attempt this yourself. I mean, I certainly don't have any "simple banking system example in C++ but with OOPS concept like Inheritance, and saving data with vector or map" lying around, and I am not going to create one or try and dig one up from the Internet specially for you. Yet, if you are willing to do your best towards writing your own code for this, people here might be willing to help you along.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #4
    Join Date
    Jul 2001
    Posts
    27

    Re: Standard C++98 Banking System example with OOPS

    Sorry Friends you are right that i have to write code my own and i did that also but some how that code is not working according to my expectation and very long so instead of post all code i just asking for links. I just want to compare my code with others and like to check that i am going proper or not.

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,761

    Re: Standard C++98 Banking System example with OOPS

    What is your current code and how does it not work?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,525

    Re: Standard C++98 Banking System example with OOPS

    Quote Originally Posted by P_N_Data_System View Post
    Sorry Friends you are right that i have to write code my own and i did that also but some how that code is not working according to my expectation and very long so instead of post all code i just asking for links. I just want to compare my code with others and like to check that i am going proper or not.
    When writing code, it's much better to write and debug small pieces of it one at a time than trying to write the entire project at once. Post what you have that doesn't work and we'll help.

  7. #7
    Join Date
    Jul 2001
    Posts
    27

    Re: Standard C++98 Banking System example with OOPS

    Hello friends, here is my dummy code please tell me it is right or not and if i like to put class A, B, C, D in File1.cpp and main in File2.cpp then what to do?
    How i can link File1.cpp and File2.cpp?

    Code:
    #include <iostream>
    using namespace std;
    
    class A
    {
    private:
        string name;
    
    protected:
        void setName(string n)
        {
            name = n;
        }
        string getName()
        {
            return name;
        }
        virtual int display(int)=0;
    };
    
    class B : virtual public A
    {
    private:
        static int noB;
    
    public:
        B()
        {
            ++noB;
        }
        int display(int i)
        {
            cout << "i: " << i << endl;
            ++i;
            return i;
        }
        static int getNoB()
        {
            return noB;
        }
    
    };
    
    int B::noB = 0;
    
    
    class C : virtual public  A
    {
    private:
        static int noC;
    
    public:
        C()
        {
            ++noC;
        }
        int display(int i)
        {
            cout << "i: " << i << endl;
            ++i;
            return i;
        }
        static int getNoC()
        {
            return noC;
        }
    
    };
    
    int C::noC = 0;
    
    class D
    {
    private:
    
    public:
        D()
        {
    
        }
        int create()
        {
            int ch;
            B objB, objB1;
    
            C objC;
            cout << "1. Call B" << endl;
            cout << "2. Call C" << endl;
    
            cin >> ch;
    
            switch(ch)
            {
            case 1:
                cout << objB.display(1) << endl;
                break;
            case 2:
                cout << objC.display(2) << endl;
                break;
            }
        }
        void Test()
        {
            cout << "Total B count: " << B::getNoB() << endl;
            cout << "Total C count: " << C::getNoC() << endl;
        }
    };
    
    
    int main()
    {
        D d;
    
        d.create();
        d.Test();
    
        return 0;
    }

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,008

    Re: Standard C++98 Banking System example with OOPS

    This is a homework assignment, isn't it?

  9. #9
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,728

    Re: Standard C++98 Banking System example with OOPS

    please tell me it is right or not
    For what? and what has this to do with a c++98 banking system?

    Note that D::create() needs to return a value.

    As multiple inheritance is not used, why are you using virtual base classes?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.1.6)

  10. #10
    Join Date
    Mar 2001
    Posts
    2,527

    Re: Standard C++98 Banking System example with OOPS

    I find the wording of the phrase banking system to be suspect.

    Do you mean like the UK banking system?
    Or an accounting system?
    Or a computer system
    that runs a single bank?
    Or a whole chain of them?
    ahoodin
    To keep the plot moving, that's why.

  11. #11
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,525

    Re: Standard C++98 Banking System example with OOPS

    Quote Originally Posted by P_N_Data_System View Post
    Hello friends, here is my dummy code please tell me it is right or not and if i like to put class A, B, C, D in File1.cpp and main in File2.cpp then what to do?
    How i can link File1.cpp and File2.cpp?

    Code:
    #include <iostream>
    using namespace std;
    
    class A
    {
    private:
        string name;
    
    protected:
        void setName(string n)
        {
            name = n;
        }
        string getName()
        {
            return name;
        }
        virtual int display(int)=0;
    };
    
    class B : virtual public A
    {
    private:
        static int noB;
    
    public:
        B()
        {
            ++noB;
        }
        int display(int i)
        {
            cout << "i: " << i << endl;
            ++i;
            return i;
        }
        static int getNoB()
        {
            return noB;
        }
    
    };
    
    int B::noB = 0;
    
    
    class C : virtual public  A
    {
    private:
        static int noC;
    
    public:
        C()
        {
            ++noC;
        }
        int display(int i)
        {
            cout << "i: " << i << endl;
            ++i;
            return i;
        }
        static int getNoC()
        {
            return noC;
        }
    
    };
    
    int C::noC = 0;
    
    class D
    {
    private:
    
    public:
        D()
        {
    
        }
        int create()
        {
            int ch;
            B objB, objB1;
    
            C objC;
            cout << "1. Call B" << endl;
            cout << "2. Call C" << endl;
    
            cin >> ch;
    
            switch(ch)
            {
            case 1:
                cout << objB.display(1) << endl;
                break;
            case 2:
                cout << objC.display(2) << endl;
                break;
            }
        }
        void Test()
        {
            cout << "Total B count: " << B::getNoB() << endl;
            cout << "Total C count: " << C::getNoC() << endl;
        }
    };
    
    
    int main()
    {
        D d;
    
        d.create();
        d.Test();
    
        return 0;
    }
    That's just random code you found on the internet. It has nothing to do with any kind of banking system. You seem to be missing the point that you're going to actually have to put in some effort to get help around here.

  12. #12
    Join Date
    Jul 2001
    Posts
    27

    Re: Standard C++98 Banking System example with OOPS

    Hello friends, I tried to create code now i have dynamic cast problem in this code kindly help me.

    1. Check for dynamic cast problem
    2. If your find any coding style problem please tell me because i am very new in C++ programming.
    3. My goal for this program is showing power of OOPs and STL

    Account.h

    Code:
    // Account.h
    
    #include <iostream>
    using namespace std;
    
    class Account
    {
    private:
        static int acNo;
        string name;
        string status;
        double amount;
    
    protected:
        void setName(string n)
        {
            name = n;
        }
        string getName()
        {
            return name;
        }
        double getAmount()
        {
            return amount;
        }
    
    public:
        int create()
        {
            cout << "Account Holder Name: ";
            getline(cin, name);
    
            cout << "Opening Amount: ";
            cin >> amount;
    
            return ++acNo;
        }
        void deposit(int)
        {
    
        }
        void withdrawal(int)
        {
    
        }
    };
    
    int Account::acNo = 0;
    Saving.h

    Code:
    // Saving.h
    
    #include <iostream>
    using namespace std;
    
    class Saving : public Account
    {
    private:
        int intRate;
    
    public:
        Saving()
        {
            intRate = 5;
        }
        void setIntRate(int r)
        {
            intRate = r;
        }
        int getIntRate()
        {
            return intRate;
        }
    };
    Current.h
    Code:
    // Current.h
    
    #include <iostream>
    using namespace std;
    
    class Current : public Account
    {
    private:
        int intRate;
    
    public:
        Current()
        {
            intRate = 2;
        }
        void setIntRate(int r)
        {
            intRate = r;
        }
        int getIntRate()
        {
            return intRate;
        }
    
    };
    FixedDeposit.h
    Code:
    // FixedDeposit.h
    
    #include <iostream>
    using namespace std;
    
    class FixedDeposit : public Account
    {
    private:
        int intRate;
    
    public:
        FixedDeposit()
        {
            intRate = 10;
        }
        void setIntRate(int r)
        {
            intRate = r;
        }
        int getIntRate()
        {
            return intRate;
        }
        void create()
        {
    
        }
    
    };
    Bank.h
    Code:
    // Bank.h
    
    #include <iostream>
    #include <fstream>
    #include <map>
    #include <string>
    #include <stdlib.h>
    
    #include "Account.h"
    #include "Saving.h"
    #include "Current.h"
    #include "FixedDeposit.h"
    
    using namespace std;
    
    class Bank
    {
    private:
        string bankName;
        typedef map<int, Account*> bankMap;
        bankMap data;
    
        void setBankName()
        {
            fstream infoFile;
            infoFile.open("BankInfo.dat",ios::in|ios::binary);
    
            if(infoFile.fail())
            {
                cout << endl << endl
                     << "-------------------------" << endl
                     << "  First Time Use" << endl
                     << "-------------------------" << endl << endl
                     << "Bank Name: ";
                getline(cin,bankName);
    
                infoFile.open("BankInfo.dat",ios::out|ios::binary);
                infoFile.write(bankName.c_str(), bankName.length()+1);
                infoFile.close();
            }
            else
            {
                getline(infoFile,bankName,'\0');
                infoFile.close();
            }
        }
        void setAccountData()
        {
    
        }
        void getAccountData()
        {
    
        }
        void menuHead(string menuName)
        {
            system("cls");
            cout << endl << endl
                 << "==========================================================" << endl
                 << "   " << bankName << endl
                 << "==========================================================" << endl << endl
                 << "   " << menuName << endl
                 << "   -----------------------------" << endl << endl;
        }
        void createNewAccount()
        {
            char ch;
    
            menuHead("New Account Menu");
    
            cout << " S. Saving Account" << endl
                 << " C. Current Account" << endl
                 << " F. Fixed Deposit Account" << endl << endl;
    
            cin >> ch;
    
            switch(ch)
            {
            case 's':
            case 'S':
            {
    
                Saving sb;
                int no = sb.create();
                if(no == 0)
                {
                    cout << "Account creation fail." << endl;
                    system("pause");
                }
                else
                {
                    data[no]=&sb;
                    cout << "Account created." << endl
                         << "Your account no: " << no << endl;
                    system("pause");
                }
            }
            break;
            case 'c':
            case 'C':
            {
    
                Current cur;
                int no = cur.create();
                if(no == 0)
                {
                    cout << "Account creation fail." << endl;
                    system("pause");
                }
                else
                {
                    data[no]=&cur;
                    cout << "Account created." << endl
                         << "Your account no: " << no << endl;
                    system("pause");
                }
            }
            break;
            case 'f':
            case 'F':
            {
    
                FixedDeposit fd;
                fd.create();
            }
            break;
            default:
                cout << endl << "Please enter first letter for menu" << endl
                     << "like 'S' for Saving Account." << endl << endl;
                system("pause");
            }
        }
        void depositMoney()
        {
            menuHead("Deposit Money Menu");
            system("pause");
        }
        void withdrawalMoney()
        {
            menuHead("Withdrawal Money Menu");
            system("pause");
        }
        void checkBalance()
        {
            menuHead("Check Balance Menu");
            system("pause");
        }
        void closeAccount()
        {
            menuHead("Account Close Menu");
            system("pause");
        }
        void printAccounts()
        {
            menuHead("Accounts Print Menu");
    
            int sbCount=0;
            int curCount=0;
            int fdCount=0;
    
            for(bankMap::iterator i = data.begin(); i != data.end(); ++i)
            {
                Saving *sb = dynamic_cast<Saving *>(*i);
                Current *cur = dynamic_cast<Current *>(*i);
                FixedDeposit *fd = dynamic_cast<FixedDeposit *>(*i);
    
                if(sb)
                {
                    sbCount++;
                }
                else if(cur)
                {
                    curCount++;
                }
                else if(fd)
                {
                    fdCount++;
                }
            }
            cout << "Total Savings Accounts: " << sbCount << endl;
            cout << "Total Current Accounts: " << curCount << endl;
            cout << "Total Fixed Deposit Accounts: " << fdCount << endl;
            system("pause");
        }
    
    
    
    public:
        Bank()
        {
            setBankName();
            getAccountData();
    
        }
        ~Bank()
        {
            // Removing map data from memory
            /*        for(bankMap::iterator i = data.begin(); i != data.end(); ++i)
                    {
                        delete *i;
                        *i = NULL;
                    }
            */
            // Saving map data into file
            cout << "Saving data...." << endl;
            setAccountData();
            system("pause");
        }
        void menu()
        {
            char ch;
    
            do
            {
                menuHead("Main Menu");
    
                cout << " N. Create New Account" << endl
                     << " D. Deposit Money" << endl
                     << " W. Withdrawal Money" << endl
                     << " B. Check Balance" << endl
                     << " C. Close Account" << endl
                     << " P. Print Accounts" << endl
                     << " X. Exit" << endl << endl;
    
                cin >> ch;
    
                switch(ch)
                {
                case 'n':
                case 'N':
                    createNewAccount();
                    break;
                case 'd':
                case 'D':
                    depositMoney();
                    break;
                case 'w':
                case 'W':
                    withdrawalMoney();
                    break;
                case 'b':
                case 'B':
                    checkBalance();
                    break;
                case 'c':
                case 'C':
                    closeAccount();
                    break;
                case 'p':
                case 'P':
                    printAccounts();
                    break;
                case 'x':
                case 'X':
                    return;
                default:
                    cout << endl << "Please enter first letter for menu" << endl
                         << "like 'N' for Create New Account." << endl << endl;
                    system("pause");
                }
            }
            while(1);
        }
    };
    main.cpp
    Code:
    // Main.cpp
    
    #include <iostream>
    #include <stdlib.h>
    #include "Bank.h"
    
    using namespace std;
    
    int main()
    {
        Bank b;
    
        b.menu();
    
        return 0;
    }

  13. #13
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,525

    Re: Standard C++98 Banking System example with OOPS

    Quote Originally Posted by P_N_Data_System View Post
    Hello friends, I tried to create code now i have dynamic cast problem in this code kindly help me.

    1. Check for dynamic cast problem
    2. If your find any coding style problem please tell me because i am very new in C++ programming.
    3. My goal for this program is showing power of OOPs and STL
    Which line gives the error?

    You should read up on virtual functions and only override the functions you that have different behavior in the derived class than the base class.
    Last edited by GCDEF; January 19th, 2016 at 01:37 PM.

  14. #14
    Join Date
    Jul 2001
    Posts
    27

    Re: Standard C++98 Banking System example with OOPS

    Error is in Bank.h line no 163, 164 and 165.
    here is error:
    cannot dynamic_cast 'i.std:: _Rb_tree_iterator<_Tp>:: operator*<std:: pair<const int, Account*> >()' (of type 'struct std:: pair<const int, Account*>') to type 'class Saving*' (source is not a pointer)

  15. #15
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,728

    Re: Standard C++98 Banking System example with OOPS

    The issue is here
    Code:
    for (bankMap::iterator i = data.begin(); i != data.end(); ++i)
    {
         Saving *sb = dynamic_cast<Saving *>(*i);
         Current *cur = dynamic_cast<Current *>(*i);
         FixedDeposit *fd = dynamic_cast<FixedDeposit *>(*i);
    There are a couple of issues here. i is an iterator to a map class
    Code:
    typedef map<int, Account*> bankMap;
    bankMap data;
    which has first and second values - so *i doesn't make sense. If you want the map data (type Account*) then use i->second rather than *i.

    The second issue is that Account is not a polymorphic type so using dynamic_cast<> here is not allowed. The classes Saving, Deposit and FixedDeposit are derived from Account but are not polymorphic classes. For more info about polymorphism see http://www.cplusplus.com/doc/tutorial/polymorphism/
    Last edited by 2kaud; January 20th, 2016 at 05:52 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.1.6)

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)