Using function overloading with constructors..

There was a "project" at my uni where i had to make a program that uses two constructors, a default one that sets some default values to the general parameters and another one where the user has the chance to put some values himself.

Now my problem is (note that i am new to c++ and I need some guidance) that i do not know how to validate these parameters, for example the balance must be > 0 and i do not know how to validate it. Also there's an update() function that at the end of each year, a certain amount of money is added to the account depending on the rate of the account.

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
  #include <iostream>
using namespace std;

class BankAccount{
    public:
        BankAccount(){          //Default constructor
            Balance = 0;
            Rate = 0;
            Account_id = 111111111111;
        }
        BankAccount(double balance, double rate, unsigned long long int account_id){ //Retrieve parameters.
            Balance = balance;
            Rate = rate;
            Account_id = account_id;
            do{                                                          //Validate the Account ID the user enters
                cout << "Type a twelve-digit Account ID. \n";
                cin >> account_id;
            }while(account_id <= 111111111111 && account_id >= 99999999999);

            do{                                                         //Validate the Balance value the user enters
                cout << "Type a valid Balance number. *Balance > 0 \n";
                cin >> balance;
            }while(balance < 0);

            do{                                                          //Validate the Rate value the user enters
                cout << "Type a valid rate number. ** 0 < rate < 1 ** \n";
                cin >> rate;
                cout <<endl;
            }while(0 < rate < 1);

            cout << "Bank Account ID: "<< account_id << "." << endl;
            cout << "Current Account Balance: " << balance << " euro(s)." << endl;
            cout << "Account Rate: " << rate << " ." << "\n"<< endl;
        }

        void setBalance(long double balance){ Balance = balance;} //Function that sets th balance value
        double getBalance(){return Balance;}    //Function that gets the balance value

        void setRate(long double rate){Rate = rate;} //Function that sets the rate value
        double getRate(){return Rate;}  //Function that gets the rate value

        void setAccount_id(unsigned long long int account_id){Account_id = account_id;} //Function that sets the Account ID
        unsigned long long int getAccount_id(){return Account_id;}  //Function that gets the Account ID

        void update(){
            int apantisi;
            double rate, balace;
            cout << "Did a year pass? If yes type 1, else 0. \n";
            cin >> apantisi;
            cout << "\n";
            if(apantisi == 1){
                double afterbalance, balance;
                afterbalance = 0;
                afterbalance = afterbalance + (balance*rate) /100;
                balance = balance + afterbalance;
                cout << "Balance has been increased to " << balance << " euro(s)." << "\n" << endl;
            }
            if(apantisi == 0){
                cout << "Balance has not been increased" << endl;
            }
        };

    private:
        double Balance, Rate;
        unsigned long long int Account_id;
};


int main()
{
    BankAccount object();
    object.update();
    return 0;
}


So when a user puts some "invalid" values in the object, I do not know how to actually prompt him to re-put some values, the do-while way works but only if i use only 1 constructor.

The deadline is over and the code i sent was wrong, hopefully i will get some points for the effort, but i still want to learn how to do it.

Thank you for the effort in reading this and sorry if I gave you a headache with this code.

Regards,
Marios.
First why are you trying to get all the data inside the constructor? That constructor has parameters that you should be using to pass the required values into the constructor.

Next you called the constructor that didn't have arguments so your variables will all have the default values.

The first constructor will put the default values in that data, what do you mean by "all data".
Second, if i remove the do-while on the other constructor, i dont know how to validate the data a user will put, for example if a user gives negative balance..
Second, if i remove the do-while on the other constructor, i dont know how to validate the data a user will put, for example if a user gives negative balance..

That's why I recommend not getting the user input in that constructor.

The first constructor will put the default values in that data, what do you mean by "all data".

You do realize that only one of your constructors will ever be called, right? In the code you provided the only constructor that is being called is the constructor with no arguments. The constructor with multiple arguments is never being called.

Your default constructor should just call the full-argument constructor:
1
2
3
 public BankAccount() {
   this( 0., 0., 111111111111ULL );
 }



And the full-argument constructor should just test the input values. For example:
1
2
3
   if ( balance < 0. ) {
      // do whatever needs to be done if balance is negative
   }
Actually I disagree about calling the multiple argument constructor from the no-argument constructor. The no argument constructor should not need to check the values because the programmer insured that the default arguments were valid when he wrote the program. You should only need to validate the values when the user is providing them.



That's what i want to do, but i don't know how.
Topic archived. No new replies allowed.