mr g
mr g

Reputation: 39

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 :)

Upvotes: 1

Views: 3483

Answers (2)

Nikos C.
Nikos C.

Reputation: 51832

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.

Upvotes: 4

CaptainTrunky
CaptainTrunky

Reputation: 1707

Initialize it within a constructor:

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

Upvotes: -1

Related Questions