C++ Constructor Setting default value

Description

C++ Constructor Setting default value

#include <iostream>

using namespace std;

class MyClass//from   www . jav a2  s  . 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;
}



PreviousNext

Related