const Time noon( 12, 0, 0 );
Once a constant class object has been initialized via constructor, any attempt to modify the member variables of the object is disallowed, as it would violate the constness of the object. This includes both changing member variables directly (if they are public), or calling member functions that sets the value of member variables.
For Example:
class IntData { public: int value; IntData() { value= 0; } void SetValue(int v) { value = v; } int GetValue() { return value ; } }; int main() { const IntData cIntData; // calls default constructor cIntData.value = 5; // violates const cIntData.SetValue(5); // violates const return 0; }
cout << cIntData.GetValue();
This will cause a compile error. This is because constant class objects can only call constant member functions, then now a constant member function is a member function that guarantees it will not change any class variables or call any non-constant member functions.
To define a constant member function, we have to add const keyword in its prototype and definition:
Prototype: ReturnType FunctionName(param1,param2…) const; Definition: ReturnType FunctionName(param1,param2…) const { …}So we can redefine GetValue() to be a constant member function, then we can call it for a constant objects:
int GetValue() { return value ; }
// Declaration of the class Time. class Time { public: // Constructor function to initialize private data. // Default values are 0 (see class definition) Time( int hr, int min, int sec ) // default constructor { setTime( hr, min, sec ); } // set functions // Set the values of hour, minute, and second. void setTime( int h, int m, int s ) // set time { setHour( h ); setMinute( m ); setSecond( s ); } // Set the hour value void setHour( int h ) // set hour { hour = ( h >= 0 && h < 24 ) ? h : 0; } // Set the minute value void setMinute( int m ) // set minute { minute = ( m >= 0 && m < 60 ) ? m : 0; } // Set the second value void setSecond( int s ) // set second { second = ( s >= 0 && s < 60 ) ? s : 0; } // get functions (normally declared const) // Get the hour value int getHour() const // return hour { return hour; } // Get the minute value int getMinute() const // return minute { return minute; } // Get the second value int getSecond() const // return second { return second; } // print functions (normally declared const) // Display military format time: HH:MM void printMilitary() const // print military time { cout << ( hour < 10 ? "0" : "" ) << hour << ":" << ( minute < 10 ? "0" : "" ) << minute; } // Display standard format time: HH:MM:SS AM (or PM) void printStandard() // should be const { cout << ( ( hour == 12 ) ? 12 : hour % 12 ) << ":" << ( minute < 10 ? "0" : "" ) << minute << ":" << ( second < 10 ? "0" : "" ) << second << ( hour < 12 ? " AM" : " PM" ); } private: int hour; // 0 - 23 int minute; // 0 - 59 int second; // 0 - 59 }; int main() { Time wakeUp( 6, 45, 0 ); // non-constant object const Time noon( 12, 0, 0 ); // constant object // MEMBER FUNCTION OBJECT wakeUp.setHour( 18 ); // non-const non-const noon.setHour( 12 ); // non-const const wakeUp.getHour(); // const non-const noon.getMinute(); // const const noon.printMilitary(); // const const noon.printStandard(); // non-const const return 0; }
Compiling... Fig07_01.cpp d:fig07_01.cpp(14) : error C2662: 'setHour': cannot convert 'this' pointer from 'const class Time' to 'class Time &' Conversion loses qualifiers d:\fig07_01.cpp(20) : error C2662: 'printStandard' : cannot convert 'this' pointer from 'const class Time' to 'class Time &' Conversion loses qualifiers Time5.cpp Error executing cl.exe. test.exe -2error(s), 0 warning(s)Note: You can find the full source code of this example in code.zip file.