0

I'm a beginner in c++. Though the code is still incomplete, I would like to know why I'm not able to create an array to store my objects from the class. I have to store 5 bank accounts in an array and I was trying to do so by soring the objects but it keeps showing error.

#include<iostream>
#include<stdio.h>
#include<string>


using namespace std;


class Bank
{
        string depositor;
        int accno;
        char type;
        float balance;

    public:
        Bank(string depositor, int accno, char type, float balance); //to assign initial values
        void deposit(); //to deposit amount
        float withdraw(); //to withdraw amount
        void show(); //to show name and balance
        Bank(string depositor, int accno); //constructor function for name and account no.
        Bank(float balance, int accno); //constructor function for balance and account no.
        Bank(char type, int accno); //constructor function for type and account no.
        Bank(const Bank&); //copy constructor
        //getter and setter functions for all data members
        void setname(string depositor);
        void setacc(int accno);
        void settype(char type);
        void setbal(float balance);
        string getname();
        int getacc();
        char gettype();
        float getbal();

};

Bank::Bank(string depos, int acno, char typ, float bal)
{
    depositor=depos;
    accno = acno;
    type = typ;
    balance = bal ? bal : 0;
}

void Bank::deposit()
{
    float damt1;
    cout << "Enter deposit amount: ";
    cin >> damt1;
    if (damt1 < 0.0) {
        cout << "Can't deposit negative amount." << endl;
        damt1 = 0.0;
    }
    balance += damt1;
}

float Bank::withdraw()
{
    int amount;
    cout << "Enter withdrawal amount: ";
    cin >> amount;
    if (amount < 0.0) {
        cout << "Negative amount can't be withdrawn" << endl;
        amount = 0;
    }
    if (amount > balance - 1000.0) {
        cout << "Not enough balance.";
    }
    balance -= amount;
    return amount;
}

Bank::Bank(string name, int no)
{
    depositor = name;
    accno = no;

}

Bank::Bank(float bal, int no)
{
    balance = bal;
    accno = no;

}

Bank::Bank(char ty, int no)
{
    type = ty;
    accno = no;

}

Bank::Bank(const Bank& p)
{
    balance = p.balance;
    accno = p.accno;
}

void Bank::setname(string name)
{
    depositor = name;
}
void Bank::setacc(int n)
{
    accno = n;
}
void Bank::settype(char ty)
{
    type = ty;
}

void Bank::setbal(float bal)
{
    balance = bal?bal:0;
}
string Bank::getname()
{
    return depositor;
}
int Bank::getacc()
{
    return accno;
}
char Bank::gettype()
{
    return type;
}
float Bank::getbal()
{
    return balance;
}
void Bank::show()
{
    cout << "Name: " << depositor<<endl;
    cout << "Account number: " << accno<<endl;
    cout << "Type: " << type<<endl;
    cout << "Balance: " << balance<<endl;
}

int main()
{
    Bank acct[5];//This is the line with error.I am unable to complete the code bcoz of this
    int acno,i;
    char ty;
    string name;
    float bal;
    for (i=0;i<5;i++){
        cout << "Enter details: \n";
        cout << "name: ";
        cin >> name;
        cout << "\nEnter accno: ";
        cin >> acno;
        cout << "\nEnter type: ";
        cin >> ty;
        cout << "\nEnter balance: ";
        cin >> bal;
        Bank b1(name, acno, ty, bal);
        
    }
    

    
    
    return 0;
    


}

Can someone help me with what corrections I should make?

  • 1
    `Bank acct[5]` default constructs 5 `Bank` objects, as you have no default constructor that doesn't work. Add a default constructor or initialise the elements or use an empty `std::vector` which doesn't initialise unused elements – Alan Birtles Feb 21 '22 at 08:05
  • All of the constructors that you have defined take arguments. You haven't defined a default constructor that runs if you don't provide any arguments. Creating a constructor `Bank() = default` or `Bank(){}` will at least remove the error. Another solution is to work with pointers and initialise the array as `Bank * acct[5]`. Here, every index of the array is a pointer to a Bank object. – Harsh Motwani Feb 21 '22 at 08:07
  • Thank you so much. It worked. But how do I add values to my array when I was already using a constructor to add the values? – Nayasha 012 Feb 21 '22 at 08:11
  • Oh it all worked out now. Thank you so much – Nayasha 012 Feb 21 '22 at 08:15

0 Answers0