我的 class 怎么了

Whats wrong with my class

我不知道为什么,但是当我用 class 创建一个对象并使用默认构造函数时,当我尝试将名为 cash 的变量传递给访问器 user2.setCash(cash) 时,目的是主集 cash 等于 new_cash,它给出了一个很大的值,如 1.222256e+461 或类似的东西。为什么会这样?如果我使用我的重载构造函数,它工作正常。

main.cpp

#include <iostream>
#include <string>
#include "Bank.h"

using namespace std;

int main()
{
    string name;
    int id;
    double cash;

bank user2;
cout << "\n\nPlease type your name: ";
getline(cin >> ws, name);
user2.setName(name);
cout << "Enter an id number: ";
cin >> id;
user2.setID(id);
cout << "Enter your cash: ";
cin >> cash;
cout << cash << endl;
user2.setCash(cash);
cout << "\nAlright " << user2.getName() << ", current cash: " << user2.getCash();
cout << "\nChoose how much would you like to Deposit: ";
cin >> cash;
user2.deposit(cash);
cout << "New amount is: " << user2.getCash() << " For user ID: " << user2.getID() << "\n\n";
bank::printStatic();

return 0;
}

Bank.h

#ifndef BANK_H
#define BANK_H

#include <iostream>
#include <string>

using namespace std;

class bank
{
public:
    // Default Constructor
    bank();

    // Overload Constructor
    bank(string, int, double);

    // Destructor
    ~bank();

    // Accessor Functions
    string getName() const;
    int getID() const;
    double getCash() const;

    // Mutator Functions
    void setName(string);
    void setID(int);
    void setCash(double);

    // Functions
    void withdraw(double);
    void deposit(double);
    static void printStatic();

private:
    // Member Variables
    string new_name;
    int new_id;
    double new_cash;

    // Static Member Variables
    static int num_of_accounts;
    static double total_cash;

};

#endif

Bank.cpp

#include "Bank.h"

// Static Variables
int bank::num_of_accounts = 0;
double bank::total_cash = 0.0;

// Default Constructor
bank::bank()
{
    int new_id = 0;
    double new_cash = 0.0;
    ++num_of_accounts; // New object is created e.g. a person so total accounts must be increased.
}

// Overload Constructor
bank::bank(string name, int id, double cash)
{
    new_name = name;
    new_id = id;
    new_cash = cash;
    ++num_of_accounts; // New object is created e.g. a person so total accounts must be increased.
    total_cash += new_cash; // New object is created e.g. a person so his/hers cash must be added to the total cash of the bank.
}


// Destructor
bank::~bank()
{
    --num_of_accounts; // When Destructor is called to destroy an object (e.g. a person) then the id must be dropped by 1 cause the person e.g. (left).
    total_cash -= new_cash; // And the balance he had to be removed so it is not counted in total cash avaliable in the bank cause he e.g. (left).
}

// Accessor Functions
string bank::getName() const
{
    return new_name;
}

int bank::getID() const
{
    return new_id;
}

double bank::getCash() const
{
    return new_cash;
}

// Mutator Functions
void bank::setName(string name)
{
    new_name = name;
}

void bank::setID(int id)
{
        new_id = id;
}

void bank::setCash(double cash)
{
    cout << new_cash << endl;
    total_cash -= new_cash; // We must remove his prior cash which we holded in the total so we can then use the new value suplied.
    new_cash = cash;
    total_cash += new_cash; // Here we add the new cash (balance) he/she has.
}

void bank::withdraw(double cash)
{
    new_cash -= cash;
    total_cash -= cash;
}

void bank::deposit(double cash)
{
    new_cash += cash;
    total_cash  += cash;
}

void bank::printStatic()
{
    cout << "Total users are: " << num_of_accounts << endl;
    cout << "Total cash in bank is: " << total_cash << endl;
}

您需要在构造函数中初始化所有原始类型成员。

否则你会得到不确定的值

此外,非默认构造函数有问题:

// Default Constructor
bank::bank()
{
    int new_id = 0; 
    double new_cash = 0.0;
 ....

^ 设置局部变量的值,而不是成员变量


我建议使用初始化列表:

// Default Constructor
bank::bank() : new_name(), new_id(0), new_cash(0.0)
{
    ++num_of_accounts;
}

// Overload Constructor
bank::bank(string name, int id, double cash)
    : new_name(name), new_id(id), new_cash(cash)
{
    ++num_of_accounts;
    total_cash += new_cash;
}

您也可以将两者结合起来:

bank::bank(string name = "", int id = 0, double cash = 0.0)
    : new_name(name), new_id(id), new_cash(cash)
{
    ++num_of_accounts;
    total_cash += new_cash;
}

在您的默认构造函数中,您声明了与成员变量同名的局部变量。然后你设置这些局部变量而不是分配成员。去掉类型声明,使它们成为正常的赋值。

bank::bank()
{
    new_id = 0;
    new_cash = 0.0;
    ++num_of_accounts; // New object is created e.g. a person so total accounts must be increased.
}