user3247785
user3247785

Reputation: 13

Difference between constructor and default constructor

I'm writing the following program.

Write a class called CAccount which contains two private data elements, an integer accountNumber and a floating point accountBalance, and three member functions:

  1. A constructor that allows the user to set initial values for accountNumber and accountBalance and a default constructor that prompts for the input of the values for the above data members.

  2. A function called inputTransaction, which reads a character value for transactionType ('D' for deposit and 'W' for withdrawal), and a floating point value for transactionAmount, which updates accountBalance.

  3. A function called printBalance, which prints on the screen the accountNumber and accountBalance.

--

#include <iostream>

using namespace std;

class CAccount{
    public:
        CAccount(){
            setValues(2, 5);
            printBalance();
            inputTransaction();
            printBalance();
        }
        void setValues(int aN, int aB);
        void inputTransaction();
        void printBalance();
    private:
        int accountNumber;
        float accountBalance;
};

void CAccount::setValues(int aN, int aB){
    accountNumber = aN;
    accountBalance = aB;
}

void CAccount::inputTransaction(){
    char transactionType;
    float transactionAmount;
    cout << "Type of transaction? D - Deposit, W - Withdrawal" << endl;
    cin >> transactionType;
    cout << "Input the amount you want to deposit/withdraw" << endl;
    cin >> transactionAmount;
    if(transactionType == 'D'){
        accountBalance += transactionAmount;
    }
    else if(transactionType == 'W'){
        accountBalance -= transactionAmount;
    }
}

void CAccount::printBalance(){
    cout << "Account number : " << accountNumber << endl << "Account balance : " << accountBalance << endl;
}

int main ()
{
    CAccount client;
}

I don't understand this part :

 1. A constructor that allows the user to set 
    initial values for accountNumber and 
    accountBalance and a default constructor 
    that prompts for the input of the values for 
    the above data members.

What exactly is the difference between a constructor and default constructor, I'm kinda confused on this step.

Other than that, I would like to ask people with more experience to tell me any tips I should follow when coding with classes and which mistakes to avoid (this is my first class I ever wrote in C++).

Upvotes: 1

Views: 13144

Answers (5)

qstebom
qstebom

Reputation: 739

The default constructor is a compiler generated parameter-less constructor. You can explicitly define a constructor taking zero arguments but not everybody would call it a default constructor.

Your program should define a constructor with parameters for accountNumber and accountBalance, and a parameter-less one that prompts the user. Like this:

#include <iostream>
using namespace std;

class CAccount {
    public:

        /**
         * Constructor prompting the user for accountNumber and accountBalance
         */
        CAccount()
        {
            inputTransaction();
            printBalance();
        }
        /**
         * Constructor initializing accountNumber and accountBalance with parameters
         */
        CAccount(int accountNumber, float accountBalance)
            : accountNumber(accountNumber),
              accountBalance(accountBalance)
        {
            printBalance();
        }

        void inputTransaction() { /* Implement me. */ }
        void printBalance() { /* Implement me. */ }

    private:
        int accountNumber;
        float accountBalance;
};

As you can see, I used initializers for accountNumber and accountBalance in the constructor taking those arguments. That is what you should always do.

Upvotes: 0

Monirul Islam
Monirul Islam

Reputation: 935

If you don't define a constructor for a class, a default parameterless constructor is automatically created by the compiler. Default constructor is created only if there are no constructors. If you define any constructor for your class, no default constructor is automatically created.

Upvotes: 3

Narendra
Narendra

Reputation: 3117

Default constructor is one type of constructor.

Where as we have other conctructors namely:

  • Parameterised constructor
  • Copy constructor

If we don't define any constructor then a default constructor is provided. But if we define any constructor then no default constructor is provided.

Default constructor doesn't take any parameter. Where as other constructors need parameter.

For your 2nd question: If you define any constructor (parameterised or copy constructor) then you should define a default constructor also. Otherwise code like

ClassName obj = new ClassName();

will give build error. But this again depends upon your requirement and usecase.

And constructor is generally used for initialization. But you have called some functions inside constructor which is generally not done.

Upvotes: 0

Kit Fisto
Kit Fisto

Reputation: 4515

A Default constructor is defined to have no arguments at all as opposed to a constructor in general which can have as many arguments as you wish.

Your second question is far too general to be answered here. Please turn to the many many sources in the net. stackoverflow is for specific questions not for tutorials.

Upvotes: 3

Tony Delroy
Tony Delroy

Reputation: 106106

A default constructor is one that doesn't need to be given arguments, either because it doesn't have any, or the ones it has have default values. Such constructors are special in the sense that say Type var; or Type var[10]; or new Type(); invoke and require them.

See how you've written void CAccount::setValues(int aN, int aB)? If you change setValues to CAccount it becomes another constructor with 2 int arguments... that's a user-defined non-default constructor and satisfies your requirements.

As is, because you only have one constructor that doesn't take any arguments but reads inputs from stdin, you're forcing users to use that constructor, read inputs, and if you call setValues you'd be overwriting those values with the setValue arguments....

Upvotes: 0

Related Questions