-1
#include <iostream>
#include <string>
using namespace std;

class account {


    public:


        double credit(account account, double add);

        double debit(account account, double withdraw);

        double getBalance(account account);

        double addAccountBalance(account account1, account account2);



        account()  {


            balance = 0;

        }
        account::account (){


            if (balance <= 0){
                cout << "Initial balance was invalid!" ;
                balance = 0;
            }
        }


    private:

    double balance;

};


    int main(){

so the project i have is to create two bank accounts. the problem am having is how do I create class account objects and initialize one with the default constructor (account) and one with the other one (account::account). for the default constructor am not talking about the implicit constructor the compiler uses. but about the one i have down there. I want to be able to use these variables down in main. the first constructor. the default. sets the balance to 0 and the second one sets it to zero if the balance is a negative number;

basically create one account with the default and another with the non-default constructor.

Byte_Spike
  • 19
  • 6
  • What is the complexity you are facing in creating objects of class with parameter less constructor and with one parameter? To understand which constructor was called use breakpoints or write it to the console. – Rohit Vipin Mathews Sep 25 '15 at 03:38

2 Answers2

0

You need to have a non-default constructor like this that takes a parameter. As you have it both constructors take no parameters and therefore both are candidates for the default constructor. So I don't think you program will even compile.

account (double _balance){
        if (_balance <= 0){
            cout << "Initial balance was invalid!" ;
            balance = 0;
        } else {
            balance = _balance;
        }

}
ubi
  • 4,041
  • 3
  • 33
  • 50
  • i dont have enough reputation. but anyway. how do I initialize the first variable then? since i have no arguments and balance is private? do i have to use a setter function here? but i was told to initialize it can i do that along with the definition? – Byte_Spike Sep 25 '15 at 03:39
  • wait the constructor initializes it with 0. thats stupid. – Byte_Spike Sep 25 '15 at 03:40
0

The account::account () you implemented inside account, is not a constructor.

You need to overload constructors, to achieve that.

You could have the constructors:

class account {
    account();
    account(double initialBalance);
}

So if you instantiate account with no parameters, the first constructor will be called. If you provide an initialBalance, the second will be used.

Community
  • 1
  • 1
tstark81
  • 478
  • 3
  • 13
  • can I do that from main? – Byte_Spike Sep 25 '15 at 03:36
  • Instantiate? yes. `account myaccount(1.1);` Will create an object called "myaccount" that will be initialized using the second constructor. `account otheraccount;` will create an object called "otheraccount" and use the first constructor. – tstark81 Sep 25 '15 at 03:39