简体   繁体   中英

How to initialize private variable in class in c++?

Error is: data member initializer is not allow
How to initialize private variable in class in c++?

class CreditCard 
{
public:
    CreditCard();
    CreditCard(int bc);

    double getCredLimit();
    double getBalDue();
    int getAccNum();
    double credAvailable();
    int incre_Credit();
    void trans1();
    int cdInc();
    bool addingChrg(double chrgAmt, const std::string& desc);

    ~CreditCard();

private:
    int accNo;
    bool err;
    string mssg;
    double dueAmt;

    void wtStats();
    void logFl(string qu);
    string credName, credlastName;
    double lim;
    double bala = lim;     //Here i getting error how to solve this
    double payscale;
    double chrg;
};

how to intialize private varable in class in c++; and thanks in advance :)

bala is being initialized to lim , but lim is not initialized. You need to initialize `lim' too. For example:

double lim = 0.0;
double bala = lim;

You should initialize all your members though. Currently, you don't seem to initialize any of them.

Initialize it within a constructor:

class CreditCard 
{
public:
    CreditCard() {
        lim = -1;
        bala = lim;
    }
    // rest of code
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM