Declaring a function with the key word const ,it is specifies that the function is Read Only method,that dones not modify that object for which it is called.
Class Date
{
int month;
public:
Date (int month,int date,int year);
int getMonth(int month);
int setMonth(int month) const;//const method
};
int Date::setMonth(int mon)
{
month=mon;
}
int main()
{
Date currentDate(1,1,2011);
const Date birthDate(21,1,1987);
currentDate.setMonth(2);//right
birthDate,setMonth(2);//Error
return 1;
}
Comments
Post a Comment