Set default value in constructor - C++ Class

C++ examples for Class:Constructor

Description

Set default value in constructor

Demo Code

#include <iostream>

using namespace std;

class MyClass//from w  w  w .  j  a v  a 2s . co  m
{
private:
    int secret;
public:
    MyClass() { secret = 150; }
    int &GetValue() { return secret; }
    void Write() { cout << secret << endl; }
};

int main()
{
    MyClass inst;
    inst.Write();
    int &pry = inst.GetValue();
    pry = 30;
    inst.Write();
    return 0;
}

Result


Related Tutorials