A short example

#include <iostream> #include "DayOfYear.h" using namespace std; // a function that inputs a DayOfYear // from the user void InputDayOfYear( DayOfYear& doy) { cout << "Please input the day of year: \n"; doy.Input( ) } // a function that prints a DayOfYear // to the standard output void OutputDayOfYear( const DayOfYear& doy) { cout << "The day of year is \n"; doy.Output( ); } int main ( ) { // a const object // must be initialized; cannot be changed const DayOfYear jan1st( 1, 1 ); // an object that is not constant // aka a "non-const" object DayOfYear today; // since non-const objects are changeable // they can be passed as const or non-const params // with no problems InputDayOfYear ( today ); OutputDayOfYear( today ); // similarly, all class methods may be called for // a non-const object today.GetMonthNumber ( ); // const method today.GetDay( ) // const method today.Set( 10, 5); // non-const method today.Set( 12 ); // non-const method today.Input( ); // non-const method today.Output() // const method // since const objects are not changeable // they may not be passed as non-const parameters InputDayOfYear ( jan1st ); // **** compiler error *** // but may be passed as const parameter OutPutDayOfYear ( jan1st ); // no problem // similarly, only const methods can be called // for a const object, since non-const methods // will try to change the object jan1st.GetMonthNumber ( ); // const method -- no problem jan1st.GetDay( ) // const method -- no problem jan1st.Set( 10, 5); // non-const method *** compiler error jan1st.Set( 12 ); // non-const method *** compiler error jan1st.Input( ); // non-const method *** compiler error jan1st.Output() // const method -- no problem return 0; }