Construictors in c++ language

Construictors in c++ language

One crucial piece still is missing. How can we make sure the fields of an object have reasonable initial values before a client begins using the object? A class may define a constructor that ensures an object will begin in a well-defined sate.

A constructor definition looks similar to a method definition. The code within a constructor executes on behalf of an object when a client creates the object. For some classes, the client can provide information for the constructor to use when initializing the object. As with functions and methods, class constructors may be overloaded.

#include <iostream>

#include <iomanip>

#include <string>

class Account {
    // String representing the name of the account's owner
    std::string name;
    // The account number
    int id;
    // The current account balance
    double balance;
        // Initializes a bank account object
        Account(const std::string & customer_name, int account_number,
            double amount):
        name(customer_name), id(account_number), balance(amount) {
            if (amount < 0) {
                std::cout << "Warning: negative account balance\n";
                balance = 0.0;
    // Adds amount amt to the account's balance.
    void deposit(double amt) {
        balance += amt;
    // Deducts amount amt from the account's balance,
    // if possible.
    // Returns true if successful; otherwise, it returns false.
    // A call can fail if the withdraw would
    // cause the balance to fall below zero
    bool withdraw(double amt) {
        bool result = false; // Unsuccessful by default
        if (balance - amt >= 0) {
            balance -= amt;
            result = true; // Success
        return result;
    // Displays information about the account object
    void display() {
        std::cout << "Name: " << name << ", ID: " << id <<
            ", Balance: " << balance << '\n';
int main() {
    Account acct1("Joe", 2312, 1000.00);
    Account acct2("Moe", 2313, 500.29);
    std::cout << "---------------------" << '\n';
Name: Joe, ID: 2312, Balance: 1000
Name: Moe, ID: 2313, Balance: 500.29
Name: Joe, ID: 2312, Balance: 200
Name: Moe, ID: 2313, Balance: 522.29

• A constructor has the same name as the class.

• A constructor has no return type, not even void.

The constructor in initializes all the fields with values supplied by the client. The comma-separated list between the colon and the curly brace that begins the body of the constructor is called the constructor initialization list. An initialization list contains the name of each field with its initial value in parentheses. All of the fields that make up an object must be initialized before the body of the constructor executes. In this case the code within the constructor adjusts the balance to zero and issues a warning if a client attempts to create an account with an initial negative balance.

// Client creating two Account objects
Account acct1("Joe", 2312, 1000.00);
Account acct2("Moe", 2313, 500.29);
// Client creating two Account objects
Account acct1{"Joe", 2312, 1000.00};
Account acct2{"Moe", 2313, 500.29};
Account acct("Joe", 1033, 50.00); // New bank account object
acct.deposit(1000.00); // Add some funds
acct.withdraw(2000.00); // Method should disallow this operation

The details of depositing and withdrawing funds are the responsibility of the object itself, not the client code. The attempt to withdraw the $2,000 dollars above would not change the account’s balance, and the client can check the return value of withdraw to provide appropriate feedback to the user about the error. The program then could take steps to correct the situation.