12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- #include <iostream>
- #include <stdio.h>
- using namespace std;
- class Date
- {
- public:
- Date() : day(1), month(1), year(2000) {counter++;}
- Date(int d, int m, int y) : day(d), month(m), year(y) {counter++;}
- ~Date() {counter--;}
- private:
- int day;
- int month;
- int year;
- static int counter; // Статические переменные инициализируются вне класса
- public:
- static int getCounter() {
- return counter;
- }
- public:
- void print() { // Константный метод не изменяет состояние объекта
- cout << day << "." << month << "." << year << endl;
- }
- int getDay() const {
- return day;
- }
- Date& setDay(int day) {
- this->day = day;
- return *this;
- }
- Date& setMonth(int month) {
- this->month = month;
- return *this;
- }
- void setDate(int d, int m, int y); // boid setDate(Date* const this, int d, int m, int y)
- };
- // Статические переменные инициализируются вне класса
- int Date::counter = 0;
- void Date::setDate(int d, int m, int y)
- {
- day = d;
- month = m;
- year = y;
- }
- int main()
- {
- //const Date date(17, 9, 2023); // Для const объектов можно вызывать только const-методы
- Date date;
- Date date_2;
- date.setDate(18, 9, 2023); // setDate(&date, 18, 9, 2023)
- date.print();
- date.setDay(3).setMonth(9);
- date.print();
- cout << "Objects number: " << Date::getCounter() << endl;
- }
|