jweinst1
8/23/2017 - 12:53 AM

c++ example to show how references can be used to modify the internal values of a class.

c++ example to show how references can be used to modify the internal values of a class.

#include <iostream>

//example that tests how internal, private values
//can be set up to be modified outside the class

class IntHold
{
private:
  int value;
public:
  void setValue(int& val)
  {
    value = val;
  }
  
  void setValue(int val)
  {
    value = val;
  }
  int& getValue()
  {
    return value;
  }
  
};



int main() {
  IntHold cont;
  cont.setValue(8);
  int& a = cont.getValue();
  a += 6;
  std::cout << "The class value is: " << cont.getValue() << "\n";
  //The class value is: 14
}