Basic Banking program

there ae some error about current and private class function calls, plz help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include<iostream>
   #include<cstdlib>
    using namespace std;
    class Account
    {
    protected: // protected accessor modifire is somewhere between the public and private
    int Account_No;
    int Current_Balance;
    char *Bank_name;
    int Branch_code;
    int Interest_rate;
   
    public:
    Account(int balance , int number , char *name , int code , int rate)// constructor of Account class
    {
    Account_No = number;
    Current_Balance = balance;
    Bank_name = name;
    Branch_code = code;
    Interest_rate = rate;
    }
    void setBlance(int bln)
    {
    Current_Balance = bln;
    }

    int getBalance()
    {
    return Current_Balance;
    }
    int getInterRate()
    {
    return Interest_rate;
    }
    virtual void calcProfit() = 0; // virtual function 

    };

    class currentAccount : public Account
    {
    public:
    currentAccount(int balance , int number , char *name , int code , int rate=0):Account(balance,number,name,code,rate)
    {

    }
    void calcProfit()
    {
    int pro = (Current_Balance*Interest_rate)/100;
    int Balance = Current_Balance + pro;
    cout<<"Profit: 0 Balance: "<<Current_Balance<<endl;
    }
    };

    class savingAccount : public Account
    {
    public :
    savingAccount(int balance , int number , char *name , int code , int rate):Account(balance,number,name,code,rate)
    {

    }
    void calcProfit()
    {
    int pro = (Current_Balance*Interest_rate)/100;
    int Balance = Current_Balance + pro;
    cout<<"Profit: "<<Current_Balance<<" Balance before profit "<<Current_Balance<<" Balance after profit "<<Balance<<endl;
    }
    } ;
    main ()
    {

    currentAccount(10000, 01, "UBL", 123);
    currentAccount(15000, 01, "HBL", 111);
    savingAccount(15000, 02, "HBL", 111, 10);
    savingAccount(10000, 02, "UBL", 123, 15);
    currentAccount(25000, 03, "HBL", 111);


    Account *obj[5];

    obj[0] = &current1 123;
    obj[1] = &current 111;
    obj[2] = &saving 111,10;
    obj[3] = &saving 123, 15;
    obj[4] = &current 111;
    for(int i = 0 ; i< 5; i++)
    {
    obj[i]->calcProfit();
    cout<<endl;
    }

cout<<endl;
    system("pause");
    }

  Put the code you need help with here.
What are lines 80-84 supposed to be doing?

I think you meant for main to look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main ()
{
    Account *obj[5];

    obj[0] = new currentAccount(10000, 01, "UBL", 123);
    obj[1] = new currentAccount(15000, 01, "HBL", 111);
    obj[2] = new savingAccount(15000, 02, "HBL", 111, 10);;
    obj[3] = new savingAccount(10000, 02, "UBL", 123, 15);
    obj[4] = new currentAccount(25000, 03, "HBL", 111);

    for(int i = 0 ; i< 5; i++)
    {
        obj[i]->calcProfit();
        cout<<endl;
    }

    cout<<endl;
    system("pause");
}
Last edited on
if i delete those lines the .exe file crashes
If you leave them in you won't even get an .exe because the program will not compile.

It looks like what you want to is create an array of pointers to Accounts. You do that on line 78. However, all line 78 does is set aside space to hold 5 pointers.

The pointers are not initialized to anything yet. If you attempt to dereference them, your program will probably crash.

To initialize the pointers, we use the keyword new to invoke a constructor of our choosing. Since we said that the pointers in the array have the type Account, we must use a constructor for Account or one of its subclasses. Since Account has a pure virtual method, we can't make an Account object. So we'll have to use a constructor for one of its subclasses. In this case, that means we can make currentAccounts and savingAccounts.

In your code, you are invoking the constructors for these objects on lines 71-75, but you aren't saving the new object in any way. In the sample I provided, I am invoking the constructor using new, and saving the returned pointer to one of my allocated spots in the array.

What I did not show was deleting each of the pointers before I left main. For any use of new there should be a corresponding delete to prevent memory leaks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main ()
{
    Account *obj[5];

    obj[0] = new currentAccount(10000, 01, "UBL", 123);
    obj[1] = new currentAccount(15000, 01, "HBL", 111);
    obj[2] = new savingAccount(15000, 02, "HBL", 111, 10);;
    obj[3] = new savingAccount(10000, 02, "UBL", 123, 15);
    obj[4] = new currentAccount(25000, 03, "HBL", 111);

    for(int i = 0 ; i< 5; i++)
    {
        obj[i]->calcProfit();
        cout<<endl;
    }

    for(int i = 0; i < 5; i++)
    {
        delete obj[i];
    }

    cout<<endl;
    system("pause");
}
Last edited on
Topic archived. No new replies allowed.