jeudi 23 juin 2016

Overloaded operator issue C++

I am having an issue with an overloaded operator that does not seem to be working right. The following is my code.

ACCOUNT.H

#ifndef SICT_ACCOUNT_H__
#define SICT_ACCOUNT_H__

#include <iostream>

#define MAX_NAME 40

namespace sict
{
    class Account
    {
        char name_[MAX_NAME+1];
        double balance_;
    public:
        Account();
        Account(double);
        Account(const char*, double = 0.0);
        void display(bool = true) const;
        Account& operator=(const Account&);
        Account& operator+=(const Account&);
        friend Account operator+(Account&, const Account&);
    };
    std::ostream& operator<<(std::ostream& os, const Account& a);
};

#endif

ACCOUNT.CPP

 #include <cstring>
 #include <iomanip>
 #include "Account.h"

namespace sict
{
    Account::Account()
    {
        name_[0] = '\0';
        balance_ = 0;
    }

    Account::Account(double balance)
    {
        name_[0] = '\0';
        balance_ = balance;
    }

    Account::Account(const char name[], double balance)
    {
        name_[MAX_NAME] = '\0';
        strncpy(name_, name, MAX_NAME);
        balance_ = balance;
    }

    void Account::display(bool gotoNewline) const
    {
        std::cout << (name_[0] ? name_: "No Name") << ": $" << std::setprecision(2) << std::fixed << balance_;
        if(gotoNewline)
        {
          std::cout << std::endl;
        }
    }

    std::ostream& operator<<(std::ostream& os, const Account& a)
    {
      a.display();
      return os;
    }

    Account& Account::operator=(const Account& c)
    {
      balance_ = c.balance_;
      name_[MAX_NAME] = '\0';
      strncpy(name_, c.name_, MAX_NAME);

      return *this;
    }

    Account& Account::operator+=(const Account& b)
    {
      balance_ += b.balance_;
      return *this;
    }

    Account operator+(Account& a, const Account& b)
    {
      return a.balance_ + b.balance_;
    }


}

MAIN

#include <iostream>
#include "Account.h"

using namespace sict;

void displayABC(const Account& a, const Account& b, const Account& c)
{
    std::cout << "A: " << a << std::endl << "B: " << b << std::endl
    << "C: " << c << std::endl << "--------" << std::endl;
}

int main()
{
    Account a;
    Account b("Saving", 10000.99);
    Account c("Checking", 100.99);
    displayABC(a, b, c);
    a = b + c;
    displayABC(a, b, c);
    a = "Joint";
    displayABC(a, b, c);
    a = b += c;
    displayABC(a, b, c);
    a = b += c += 100.01;
    displayABC(a, b, c);

    return 0;
}

the output that I am supposed to get is

A: No Name: $0.00
B: Saving: $10000.99
C: Checking: $100.99
--------
A: No Name: $10101.98
B: Saving: $10000.99
C: Checking: $100.99
--------
A: Joint: $10101.98
B: Saving: $10000.99
C: Checking: $100.99
--------
A: Saving: $10101.98
B: Saving: $10101.98
C: Checking: $100.99
--------
A: Saving: $10302.98
B: Saving: $10302.98
C: Checking: $201.00

this is the out that I am getting now

A: No Name: $0.00
B: Saving: $10000.99
C: Checking: $100.99
--------
A: No Name: $10101.98
B: Saving: $10000.99
C: Checking: $100.99
--------
A: Joint: $0.00
B: Saving: $10000.99
C: Checking: $100.99
--------
A: Saving: $10101.98
B: Saving: $10101.98
C: Checking: $100.99
--------
A: Saving: $10302.98
B: Saving: $10302.98
C: Checking: $201.00
--------

As you can see the integer value is 0 for the display :

A: Joint: $0.00.

I cannot seem to figure this out. any help is appreciated!

Aucun commentaire:

Enregistrer un commentaire