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

Description

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

#include <iostream>
#include <string>
using namespace std;
class Product/*from   w w w.  ja va 2s. 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;
}



PreviousNext

Related