Add setter for the private member variable - C++ Class

C++ examples for Class:Member Field

Description

Add setter for the private member variable

class Pool
{
private:
  double length;
  double width;
  double height;

public:
  // Constructors
  Pool(double lv = 1.0, double wv = 1.0, double hv = 1.0);

  double volume();                                  // Function to calculate the volume of a pool

  // Functions to provide access to the values of data members
  double getLength() {return length;}
  double getWidth()  {return width;}
  double getHeight() {return height;}

  // Functions to set data member values
  void setLength(double lv) { if(lv > 0) length = lv;}
  void setWidth(double wv)  { if(wv > 0) width = wv;}
  void setHeight(double hv) { if(hv > 0) height = hv; }
};

Related Tutorials