Change value on const object value with setter - C++ Class

C++ examples for Class:Member Function

Description

Change value on const object value with setter

Demo Code

#include <iostream>
#include <string>
using namespace std;
class Product//from   www .  j av a2s.  c o m
{
   private:
       int size;                     //related to constness
       mutable string owner;         //not relevent to constness
   public:
       Product(int sz, string own) : size(sz), owner(own)
       {  }
       void setSize(int sz)             //changes size
       { size = sz; }
       void setOwner(string own) const  //changes owner
       { owner = own; }
       int getSize() const              //returns size
       { return size; }
       string getOwner() const          //returns owner
       { return owner; }
};
int main()
{
   const Product sbar(60, "Window1");
   // sbar.setSize(100);             //can't do this to const obj
   sbar.setOwner("Window2");      //this is OK
   //these are OK too
   cout << sbar.getSize() << ", " << sbar.getOwner() << endl;
   return 0;
}

Result


Related Tutorials