3
\$\begingroup\$

I am asking this, because after studying I am confused that it is necessary to declare base class object, then declare derived class object and then store reference of derived class object in base class in c++ to use polymorphism. But what do you think, did I use polymorphism in Accounts, CurrentAccount, SavingAccount and also between Person, Customer, Manager classes or not?

The code runs correctly with no issues, but the concept is confusing.

Accounts.h

#pragma once
#include<iostream>
#include<string>
#include <vector>
using namespace std;
class Accounts
{
public:
    Accounts();
    ~Accounts();
    virtual void WithDraw(int) = 0;
    virtual void Deposit(int, string, int, int) = 0;//Also can be named as add Account
    virtual void Balance() {};
    virtual void DeleteAccount(int)=0;
    virtual int getAccountsNumber()=0;
};
//Definning classes methods
Accounts::Accounts()
{
    cout << "\nThe Accounts class started\n";
    //no need to initialize vectors.They work perfect without initializing.C++ has done work for it
}
Accounts::~Accounts()
{
    cout << "\nThe Accounts class Ended\n";
}

CurrentAccount.h

#pragma once
#include"Accounts.h"
class CurrentAccount:public Accounts 
{

public:
    CurrentAccount();
    ~CurrentAccount();
    void WithDraw(int);
    void Deposit(int, string, int, int);//Also can be named as add Account
    void Balance();
    void DeleteAccount(int);
    int getAccountsNumber();
protected:
    vector<int> Account_ID_Current;
    vector<string> AccountType_Current;
    vector<int> Customer_ID_Current;
    vector<int> Account_Balance_Current;
};

CurrentAccount::CurrentAccount()
{
    cout << "\nCreate Account Class started";
}

CurrentAccount::~CurrentAccount()
{
    cout << "\nCreate Account Class ENDED";
}

void CurrentAccount::Deposit(int AID, string AT, int CID, int AB)
{
    Account_ID_Current.push_back(AID);
    AccountType_Current.push_back(AT);
    Customer_ID_Current.push_back(CID);
    Account_Balance_Current.push_back(AB);

}
void CurrentAccount::WithDraw(int index)
{
    cout << "\nThe Account ID of " << (index + 1) << " person is equal to: "
        << Account_ID_Current[index] << endl;

    cout << "\nThe Account Type of " << (index + 1) << " person is equal to: "
        << AccountType_Current[index] << endl;

    cout << "\nThe Customer ID of " << (index + 1) << " person is equal to: "
        << Customer_ID_Current[index] << endl;

    cout << "\nThe Account Balance of " << (index + 1) << " person is equal to: "
        << Account_Balance_Current[index] << endl;

}
void CurrentAccount::DeleteAccount(int index)
{
    Account_ID_Current.erase(Account_ID_Current.begin() + index);
    AccountType_Current.erase(AccountType_Current.begin() + index);
    Customer_ID_Current.erase(Customer_ID_Current.begin() + index);
    Account_Balance_Current.erase(Account_Balance_Current.begin() + index);
    //Displaying that the account is successfully removed from the bank
    cout << "\nThe Account ID of " << (index + 1) << " was successfully removed from the bank";
    cout << "\nThe Account Type of " << (index + 1) << " was successfully removed from the bank";
    cout << "\nThe Customer ID of " << (index + 1) << " was successfully removed from the bank";
    cout << "\nThe Account Balance of " << (index + 1) << " was successfully removed from the bank";

}
//It will display all the balance in the bank
void CurrentAccount::Balance()
{
    //The static int is not changed on the iteration where ever used in the loop or where ever
    static int sum = 0;//To store the sum of all balance
    for (int i = 0; i < Account_ID_Current.size(); i++)
    {
        sum = sum + Account_Balance_Current[i];
    }
    cout << "\nThe total balance in the bank in the current account is equal to : " << sum;
}
int CurrentAccount::getAccountsNumber()
{
    return Account_ID_Current.size();
}

SavingAccount

#pragma once
#include"Accounts.h"
class SavingAccount :public Accounts
{

public:
    SavingAccount();
    ~SavingAccount();
    void WithDraw(int);
    void Deposit(int, string, int, int);//Also can be named as add Account
    void Balance();
    void DeleteAccount(int);
    int getAccountsNumber();
protected:
    vector<int> Account_ID_Saving;
    vector<string> AccountType_Saving;
    vector<int> Customer_ID_Saving;
    vector<int> Account_Balance_Saving;
};

SavingAccount::SavingAccount()
{
    cout << "\nSaving Account Class started";
}

SavingAccount::~SavingAccount()
{
    cout << "\nSaving Account Class ENDED";
}

void SavingAccount::Deposit(int AID, string AT, int CID, int AB)
{
    Account_ID_Saving.push_back(AID);
    AccountType_Saving.push_back(AT);
    Customer_ID_Saving.push_back(CID);
    Account_Balance_Saving.push_back(AB);
}
void SavingAccount::WithDraw(int index)
{
    cout << "\nThe Account ID of " << (index + 1) << " person is equal to: "
        << Account_ID_Saving[index] << endl;

    cout << "\nThe Account Type of " << (index + 1) << " person is equal to: "
        << AccountType_Saving[index] << endl;

    cout << "\nThe Customer ID of " << (index + 1) << " person is equal to: "
        << Customer_ID_Saving[index] << endl;

    cout << "\nThe Account Balance of " << (index + 1) << " person is equal to: "
        << Account_Balance_Saving[index] << endl;

}
void SavingAccount::DeleteAccount(int index)
{
    Account_ID_Saving.erase(Account_ID_Saving.begin() + index);
    AccountType_Saving.erase(AccountType_Saving.begin() + index);
    Customer_ID_Saving.erase(Customer_ID_Saving.begin() + index);
    Account_Balance_Saving.erase(Account_Balance_Saving.begin() + index);
    //Displaying that the account is successfully removed from the bank
    cout << "\nThe Account ID of " << (index + 1) << " was successfully removed from the bank";
    cout << "\nThe Account Type of " << (index + 1) << " was successfully removed from the bank";
    cout << "\nThe Customer ID of " << (index + 1) << " was successfully removed from the bank";
    cout << "\nThe Account Balance of " << (index + 1) << " was successfully removed from the bank";

}
//It will display all the balance in the bank
void SavingAccount::Balance()
{
    //The static int is not changed on the iteration where ever used in the loop or where ever
    static int sum = 0;//To store the sum of all balance
    for (int i = 0; i < Account_ID_Saving.size(); i++)
    {
        sum = sum + Account_Balance_Saving[i];
    }
    cout << "\nThe total balance in the bank is equal to : " << sum;
}
int SavingAccount::getAccountsNumber()
{
    return Account_ID_Saving.size();
}

Bank.h

#include"Accounts.h"
#include"CurrentAccount.h"
#include"SavingAccount.h"
#include"Customer.h"
#include"Manager.h"
using namespace std;
class Bank
{
    Customer customers;
    CurrentAccount accountsC;
    SavingAccount accountsS;
public:
    Bank();
    ~Bank();
    //Methods for current accounts
    void Add_Current_Account(int, string, int, int);
    int Get_Current_NoOfAccounts();
    void Delete_Current_Account(int);
    void getAll_current_Balance();
    //Methods for current accounts


    //Methods for saving accounts
    void Add_Saving_Account(int, string, int, int);
    int Get_Saving_NoOfAccounts();
    void Delete_Saving_Account(int);
    void getAll_saving_Balance();
    //Methods for saving accounts



    void AddCustomer(string, int);
    void DeleteCustomer(int);

    string GetCustomer_Name(int);
    void driverProgram();
};
Bank::Bank()
{
   
    cout << "\nThe program is in the bank class\n";
}

//Current Account
void Bank::Add_Current_Account(int AID, string AT, int CID, int AB)
{
    accountsC.Deposit(AID, AT, CID, AB);
}
void Bank::Delete_Current_Account(int index)
{
    accountsC.DeleteAccount(index);
}
int Bank::Get_Current_NoOfAccounts()
{
    int num = accountsC.getAccountsNumber();
    return num;
}
void Bank::getAll_current_Balance()
{
    accountsC.Balance();
}
//Current Account

//Saving Account
void Bank::getAll_saving_Balance()
{
    accountsS.Balance();
}
void Bank::Add_Saving_Account(int AID, string AT, int CID, int AB)
{
    accountsS.Deposit(AID, AT, CID, AB);
}
void Bank::Delete_Saving_Account(int index)
{
    accountsS.DeleteAccount(index);
}
int Bank::Get_Saving_NoOfAccounts()
{
    int num = accountsS.getAccountsNumber();
    return num;
}
//Saving Account


void Bank::AddCustomer(string name, int ID)
{
    customers.AddCustomer(name, ID);
}
void Bank::DeleteCustomer(int index)
{
    customers.DeleteCustomer(index);
}

string Bank::GetCustomer_Name(int index)
{
    string name = customers.getCustomerName(index);
    return name;
}
void Bank::driverProgram()
{
    Manager m1;
   

    //For Polymorphism and using virtual functions pointers or refrences one is necessary to use otherwise
 //we cannot use virtual functions and polymorphism
    Accounts* currentAccount;
    currentAccount = new CurrentAccount();
    //I am declaring current account pointer

    //Declaring Saving Account
    Accounts* savingAccount;
    savingAccount = new SavingAccount();
    //Declaring Saving Account


    Bank b1;

    char options;

    cout << "\n\nEnter what you want to do \n1 for entering the managers data,\n2 for showing the managers data "
        << "\n3 for adding a customer in the bank\n4 for adding an Account in the bank \n5 for deleting the customer\n"
        << "\n6 for deleting the Account\n7 for getting customer name\n8 for getting the No. of Accounts"
        << "\n9 for seeing all the balance in the bank\nPress 'e' for exit";

    cin >> options;


    switch (options)
    {
    case '1':
    {
        string name;
        string branch;
        int id;
        //The manager class data
        cout << "\nEnter the Data of managers Class: \n";
        cout << "\nEnter the name of manager: \t";
        cin >> name;
        cout << "\nEnter the branch of manager: \t";
        cin >> branch;
        cout << "\nEnter the Id of manager: \t";
        cin >> id;
        m1.TakeManagersData(name, branch, id);
        break;
    }
    case '2':
    {
        cout << "\nThe data of Manager is : ";
        m1.Print_ManagerDetails();
        break;
    }
    case '3':
    {
        string Cname;
        int CID;
        cout << "\nEnter the name of customer: ";
        cin >> Cname;
        cout << "\nEnter the Customer ID: ";
        cin >> CID;
        b1.AddCustomer(Cname, CID);

        break;
    }
    case '4':
    {
        char optionB;
        cout << "There are two options available for creating account.\nOne is saving account(INTEREST)\t"
            << "Press '1' for it and \nSecond one is Current Account(NO INTEREST)\tPress '2' for it\n";
        cin >> optionB;
        int AID;
        int CID;
        int AB;
        string AT;

        switch (optionB)
        {
        case '1':
        {
            cout << "\nEnter the Account ID: ";
            cin >> AID;
            cout << "\nEnter the Customer ID: ";
            cin >> CID;
            cout << "\nEnter the Account BALANCE: ";
            cin >> AB;
            cout << "\nEnter the Account Type: ";
            cin >> AT;

            b1.Add_Saving_Account(AID, AT, CID, AB);
            cout << "\nSuccessfully created a Saving account\tBut delete it as soon as possible\n";
            break;
        }
        case'2':
        {
            cout << "\nEnter the Account ID: ";
            cin >> AID;
            cout << "\nEnter the Customer ID: ";
            cin >> CID;
            cout << "\nEnter the Account BALANCE: ";
            cin >> AB;
            cout << "\nEnter the Account Type: ";
            cin >> AT;

            b1.Add_Current_Account(AID, AT, CID, AB);
            cout << "\nSuccessfully created a Current account\nKeep It as long as you want\n";
            break;
        }
        default:
            cout << "\nSorry Try Again!\nEnter right value only one or two\n";
            break;
        }


        break;
    }
    case '5':
    {
        int index;
        cout << "\nEnter the customer which you want to delete: ";
        cin >> index;
        b1.DeleteCustomer(index);

        break;
    }
    case '6':
    {
        char optionB;
        cout << "There are two options available for DELETING account.\nOne is saving account(INTEREST)\t"
            << "Press '1' for it and \nSecond one is Current Account(NO INTEREST)\tPress '2' for it\n";
        cin >> optionB;

        int index;

        switch (optionB)
        {
        case '1':
        {
            cout << "\nEnter the account number you want to delete\n";
            cin >> index;

            b1.Delete_Saving_Account(index);
            cout << "\nSuccessfully deleted Saving account at the given address\n";
            break;
        }
        case'2':
        {
            cout << "\nEnter the account number you want to delete\n";
            cin >> index;

            b1.Delete_Current_Account(index);
            cout << "\nSuccessfully deleted a Current account at current index\n";
            break;
        }
        default:
            cout << "\nSorry Try Again!\nEnter right value only one or two\n";
            break;
        }



        break;
    }
    case '7':
    {
        int cn;
        cout << "\nEnter the customer ID which you want to get name: ";
        cin >> cn;
        b1.GetCustomer_Name(cn);

        break;
    }
    case '8':
    {
        char optionB;
        cout << "There are two options available for getting number of account.\nOne is saving account(INTEREST)\t"
            << "Press '1' for it and \nSecond one is Current Account(NO INTEREST)\tPress '2' for it\n";
        cin >> optionB;

        switch (optionB)
        {
        case '1':
        {
     
            cout << "\nThe number of accounts of Saving account type are: \t" << b1.Get_Saving_NoOfAccounts() << endl;
            break;
        }
        case'2':
        {
            cout << "\nThe number of accounts of Current account type are: \t" << b1.Get_Current_NoOfAccounts() << endl;
            break;
        }
        default:
            cout << "\nSorry Try Again!\nEnter right value only one or two\n";
            break;
        }


        break;
    }
    case '9':
    {
        char optionB;
        cout << "There are two options available for getting the balance in the bank.\nOne is saving account(INTEREST)\t"
            << "Press '1' for it and \nSecond one is Current Account(NO INTEREST)\tPress '2' for it\n";
        cin >> optionB;

        switch (optionB)
        {
        case '1':
        {

            cout << "\nThe Balance of Saving account type is: \t" << endl;
            b1.getAll_saving_Balance();
            break;
        }
        case'2':
        {
            cout << "\nThe Balance of Current account type is: \t" << endl;
            b1.getAll_current_Balance();
            break;
        }
        default:
            cout << "\nSorry Try Again!\nEnter right value only one or two\n";
            break;
        }


        break;
    }
    case 'e':
    {
        cout << "The program is ending now: ";

        break;
    }
    default:
    {
        cout << "\n\nEnter right value please: \n";
    }
    delete currentAccount;
    delete savingAccount;
    currentAccount = nullptr;
    savingAccount = nullptr;
    }
}

Bank::~Bank()
{
    cout << "\nThe Bank class ended \n";
   
}

Customer.h

#pragma once
#include<iostream>
#include<string>
#include<vector>
#include"Person.h"
using namespace std;
class Customer:protected Person
{
public:
    Customer();
    void AddCustomer(string, int);
    void PrintAllCustomersData();
    void DeleteCustomer(int);
    void Print_CustomerDetails(int);
    string getCustomerName(int);
};
Customer::Customer()
{
    cout << "\nThe customer class started\n";
}
void Customer::AddCustomer(string n, int id)
{
    Name.push_back(n);
    ID.push_back(id);
    cout << "\nThe customer " << n << "with Id: " << id << " was successfully added in the Bank.";
}
void Customer::PrintAllCustomersData()
{
    for (unsigned int i = 0; i < ID.size(); i++)
    {
        cout << "\nThe ID of " << (i + 1) << "Customer is : " << ID[i] << " and NAME is : " << Name[i];
    }
}
void Customer::DeleteCustomer(int index)
{
    Name.erase(Name.begin() + index);
    ID.erase(ID.begin() + index);
    cout << "\nThe customer with Name : " << Name[index] << " and ID: " << ID[index] << " was successfully deleted\n";
}
void Customer::Print_CustomerDetails(int index)
{
    cout << "The Customer Name is : " << Name[index] << endl;
    cout << "The Id of Customer is : " << ID[index] << endl;
}
string Customer::getCustomerName(int index)
{
    return (Name[index]);
}

Manager.h

#pragma once
#include"Person.h"
class Manager:protected Person
{

public:
    void Print_ManagerDetails();
    void TakeManagersData(string, string, int);
};
void Manager::Print_ManagerDetails()
{
    cout << "\nName.size: " << Name.size();
    cout << "\nID.size: " << ID.size();
    cout << "\nBranch.size: " << Branch.size();
    for (int i = 0; i < Name.size(); i++)
    {
        cout << "\nThe ID of Manager is : " << ID[i] << endl;
        cout << "\nThe Name of Manager is : " << Name[i] << endl;
        cout << "\nThe Branch of Manager is : " << Branch[i] << endl;
    }
}
void Manager::TakeManagersData(string n, string b, int id)
{
    Name.push_back(n);
    Branch.push_back(b);
    ID.push_back(id);
}

Person.h

#pragma once
#include<iostream>
using namespace std;
class Person
{
protected:  
    vector<string> Name;
    vector<int> ID;
    vector<string> Branch;

public:
    Person();
    ~Person();
    
};

Person::Person()
{
    cout << "\nPerson class started\n";
}

Person::~Person()
{
    cout << "\nPerson class ENDED\n";
}

Source.cpp

#pragma once
#include<iostream>
#include"Bank.h"
#include"Customer.h"
#include"Manager.h"
#include"Accounts.h"

#include<vector>
#include<string>
using namespace std;

int main()
{
    Manager m1;

    //For Polymorphism and using virtual functions pointers or refrences one is necessary to use otherwise
    //we cannot use virtual functions and polymorphism
 
    //I am declaring current account pointer

    //Declaring Saving Account
 
    //Declaring Saving Account

    Bank b1;


    bool check = true;
    while (check == true)
    {

        /// <summary>
        /// 

        char options;

       
        cout << "\n\nEnter what you want to do \n1 for entering the managers data,\n2 for showing the managers data "
            << "\n3 for adding a customer in the bank\n4 for adding an Account in the bank \n5 for deleting the customer\n"
            << "\n6 for deleting the Account\n7 for getting customer name\n8 for getting the No. of Accounts"
            << "\n9 for seeing all the balance in the bank\nPress 'e' for exit";


        cin >> options;


        switch (options)
        {
        case '1':
        {
            string name;
            string branch;
            int id;
            //The manager class data
            cout << "\nEnter the Data of managers Class: \n";
            cout << "\nEnter the name of manager: \t";
            cin >> name;
            cout << "\nEnter the branch of manager: \t";
            cin >> branch;
            cout << "\nEnter the Id of manager: \t";
            cin >> id;
            m1.TakeManagersData(name, branch, id);
            break;
        }
        case '2':
        {
            cout << "\nThe data of Manager is : ";
            m1.Print_ManagerDetails();
            break;
        }
        case '3':
        {
            string Cname;
            int CID;
            cout << "\nEnter the name of customer: ";
            cin >> Cname;
            cout << "\nEnter the Customer ID: ";
            cin >> CID;
            b1.AddCustomer(Cname, CID);

            break;
        }
        case '4':
        {
            char optionB;
            cout << "There are two options available for creating account.\nOne is saving account(INTEREST)\t"
                << "Press '1' for it and \nSecond one is Current Account(NO INTEREST)\tPress '2' for it\n";
            cin >> optionB;
            int AID;
            int CID;
            int AB;
            string AT;
        
            switch (optionB)
            {
            case '1':
            {
                cout << "\nEnter the Account ID: ";
                cin >> AID;
                cout << "\nEnter the Customer ID: ";
                cin >> CID;
                cout << "\nEnter the Account BALANCE: ";
                cin >> AB;
                cout << "\nEnter the Account Type: ";
                cin >> AT;

                b1.Add_Saving_Account(AID, AT, CID, AB);
                cout << "\nSuccessfully created a Saving account\tBut delete it as soon as possible\n";
                break;
            }
            case'2':
            {
                cout << "\nEnter the Account ID: ";
                cin >> AID;
                cout << "\nEnter the Customer ID: ";
                cin >> CID;
                cout << "\nEnter the Account BALANCE: ";
                cin >> AB;
                cout << "\nEnter the Account Type: ";
                cin >> AT;

                b1.Add_Current_Account(AID, AT, CID, AB);
                cout << "\nSuccessfully created a Current account\nKeep It as long as you want\n";
                break;
            }
            default:
                cout << "\nSorry Try Again!\nEnter right value only one or two\n";
                break;
            }

            break;
        }
        case '5':
        {
            int index;
            cout << "\nEnter the customer which you want to delete: ";
            cin >> index;
            b1.DeleteCustomer(index);

            break;
        }
        case '6':
        {
            char optionB;
            cout << "There are two options available for DELETING account.\nOne is saving account(INTEREST)\t"
                << "Press '1' for it and \nSecond one is Current Account(NO INTEREST)\tPress '2' for it\n";
            cin >> optionB;

            int index;

            switch (optionB)
            {
            case '1':
            {
                cout << "\nEnter the account number you want to delete\n";
                cin >> index;

                b1.Delete_Saving_Account(index);
                cout << "\nSuccessfully deleted Saving account at the given address\n";
                break;
            }
            case'2':
            {
                cout << "\nEnter the account number you want to delete\n";
                cin >> index;

                b1.Delete_Current_Account(index);
                cout << "\nSuccessfully deleted a Current account at current index\n";
                break;
            }
            default:
                cout << "\nSorry Try Again!\nEnter right value only one or two\n";
                break;
            }



            break;
        }
        case '7':
        {
            int cn;
            cout << "\nEnter the customer ID which you want to get name: ";
            cin >> cn;
            b1.GetCustomer_Name(cn);

            break;
        }
        case '8':
        {
            char optionB;
            cout << "There are two options available for getting number of account.\nOne is saving account(INTEREST)\t"
                << "Press '1' for it and \nSecond one is Current Account(NO INTEREST)\tPress '2' for it\n";
            cin >> optionB;

            switch (optionB)
            {
            case '1':
            {

                cout << "\nThe number of accounts of Saving account type are: \t" << b1.Get_Saving_NoOfAccounts() << endl;
                break;
            }
            case'2':
            {
                cout << "\nThe number of accounts of Current account type are: \t" << b1.Get_Current_NoOfAccounts() << endl;
                break;
            }
            default:
                cout << "\nSorry Try Again!\nEnter right value only one or two\n";
                break;
            }

            break;
        }
        case '9':
        {
            char optionB;
            cout << "There are two options available for getting the balance in the bank.\nOne is saving account(INTEREST)\t"
                << "Press '1' for it and \nSecond one is Current Account(NO INTEREST)\tPress '2' for it\n";
            cin >> optionB;

            switch (optionB)
            {
            case '1':
            {

                cout << "\nThe Balance of Saving account type is: \t" << endl;
                b1.getAll_saving_Balance();
                break;
            }
            case'2':
            {
                cout << "\nThe Balance of Current account type is: \t" << endl;
                b1.getAll_current_Balance();
                break;
            }
            default:
                cout << "\nSorry Try Again!\nEnter right value only one or two\n";
                break;
            }

            break;
        }
        case 'e':
        {
            cout << "The program is ending now: ";

            break;
        }
        default:
        {
            cout << "\n\nEnter right value please: \n";
        }

        }
        /// </summary>
        /// <returns></returns>

        char option;

        cout << "Enter y for opening menu again and n for exiting\t";
        cin >> option;
        while (option != 'y' && option != 'n')
        {
            cout << "Enter right value Please! only y or n: ";
            char option1;
            cin >> option1;
            if (option1 == 'y' || option1 == 'n')
            {
                break;
            }
        }
        if (option == 'y')
        {
            check = true;
        }
        else if (option == 'n')
        {
            check = false;
            cout << "The program is ending now! ";
        }
    }
}

The link to view code on github is https://github.com/Muhammad-Bilal-7896/Program-Bank-With-Inheritance-and-Polymorphism

\$\endgroup\$
7
  • \$\begingroup\$ Technically it's obvious you are using polymorphsm (your Accounts class is abstract and has pure virtual functions). Can you elaborate about your doubs please? \$\endgroup\$ – πάντα ῥεῖ Aug 10 '20 at 18:25
  • \$\begingroup\$ @πάντα Yes my doubts are that if I will remove virtual keyword from base classes than the same program works .i.e making function normal from pure virtual does not changes anything then where goes the polymorphism \$\endgroup\$ – BilalMohib Aug 10 '20 at 18:27
  • 1
    \$\begingroup\$ "I think that for using polymorphism it is necessary to use refrence or pointer " To the base class, yes. \$\endgroup\$ – πάντα ῥεῖ Aug 10 '20 at 18:37
  • 1
    \$\begingroup\$ @ πάντα ῥεῖ thank you I just wanted to learn about that your conversation was quite helpful.Its cleared now \$\endgroup\$ – BilalMohib Aug 10 '20 at 18:39
  • 3
    \$\begingroup\$ See codereview.stackexchange.com/questions/56363/… for how polymorphism works in C++. \$\endgroup\$ – Edward Aug 10 '20 at 18:56
3
\$\begingroup\$

But what do you think, did I use polymorphism in Accounts, CurrentAccount, SavingAccount and also between Person, Customer, Manager classes or not?

No, you didn't use polymorphism anywhere in your program. You only work with concrete classes of CurrentAccount, SavingAccount, Customer and Manager.

Polymorphism means, that you call the concrete classes functions via a reference or pointer to their base class. E.g.:

class Bank
{
    Customer customers;
    Accounts* accountsC;
    Accounts* accountsS;
    // ...
};

Bank::Bank()
{
    accountsC = new CurrentAccount();
    accountsS = new SavingAccount();
    cout << "\nThe program is in the bank class\n";
}
//Current Account
void Bank::Add_Current_Account(int AID, string AT, int CID, int AB)
{
    accountsC->Deposit(AID, AT, CID, AB);
}

// ...
Bank::~Bank()
{
    delete accountsC;
    delete accountsS;
    cout << "\nThe Bank class ended \n";
   
}

Also the whole program structure and class hierarchy seems a bit suspect to me:

  • Shouldn't a Bank instance have many CustomerAccounts?
  • A Customer instance could have more than one account at the same Bank, no?
  • What's the actual role of a Manager instance? Could they do something to manipulate a Customers accounts?
  • I don't fully understand what accountsS (SavingAccount) is for. Are you sure that you need it at all?
\$\endgroup\$
4
  • \$\begingroup\$ I tried to make polymorphism between them just for learning purpose for clearing the concept there was really no need for that in reality.I did this for my learning. \$\endgroup\$ – BilalMohib Aug 11 '20 at 1:25
  • \$\begingroup\$ A customer can have as many accounts in the bank for this vectors are used because they actually grow as the accounts grow dynamically. \$\endgroup\$ – BilalMohib Aug 11 '20 at 4:01
  • \$\begingroup\$ @BilalMohib You should rather have something like std::vector<Customer> and std::vector<Account*> inside the Bank class, than managing vectors of the properties inside of these classes. \$\endgroup\$ – πάντα ῥεῖ Aug 11 '20 at 5:25
  • \$\begingroup\$ yaa good suggestion \$\endgroup\$ – BilalMohib Aug 12 '20 at 9:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.