Добавил:
Studfiles2
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Тестовые примеры / ch2 / const
.C // source module: const.C
#include <iostream.h>
static int year = 0;
class dates {
int year, month, day;
public:
dates() { year=month=day = 0; };
~dates() {};
void set(int y) { year = y; };
// const member functions
void print( ostream& = cerr ) const;
int sameDay(int d) const { return d==day; };
// note: this function is overloaded
void set(int y) const { ::year = y; };
};
void dates::print( ostream& os ) const {
os << year << "," << month << "," << day << '\n';
}
int main()
{
const dates foo; // const object
dates foo1; // non-const object
foo.set(1915); // ::year = 1915
foo.print(); // year=0, month=0, day=0
foo1.set(25); // foo1.year=25
foo1.print(); // year=25,month=0,day=0
return 0;
}