Elementary C++ – Classes and Objects – Implementation of const members and date classes

Article directory

  • const member
  • Implementation of a date class
    • Source code
    • parse
      • Constructor verification
      • Operator reuse
      • Prefix + + and postfix + +
      • Validation of other operators

const members

The const modified member function is called a const member function.
In a member function, the object pointed to by this pointer is hidden, and there is no way to modify it with conventional methods, so we modify it like this:

Precautions:

Implementation of a date class

Source code

date.h

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<assert.h>
using namespace std;
class Date
{<!-- -->
private:
int _year;
int _month;
int _day;
public:
Date(int year=1, int month=1, int day=1);
Date(const Date & d);

Date & operator + =(int day);
Date operator + (int day)const;

//Date - number of days
Date & operator-=(int day);
//Date - number of days
Date operator-(int day)const;

Date & operator + + ();
Date operator + + (int);

Date & operator--();
Date operator--(int);

//date-date
int operator-(const Date & d)const;
bool operator>(const Date & amp; d)const;
bool operator<(const Date & amp; d)const;
bool operator==(const Date & amp; d)const;
bool operator>=(const Date & amp; d)const;
bool operator<=(const Date & amp; d)const;
void Print()const;
int GetMonthDay()const;


};

date.cpp

#define _CRT_SECURE_NO_WARNINGS 1
#include"Date.h"

Date::Date(int year,int month,int day)
{<!-- -->
_year = year;
_month = month;
_day = day;

if (_year<1 || _month < 1 || _month>12 || _day>GetMonthDay())
{<!-- -->
Print();
cout << "Illegal date" << endl;

}
}
Date::Date(const Date & d)
{<!-- -->
_year = d._year;
_month = d._month;
_day = d._day;
}

void Date::Print()const
{<!-- -->
cout << _year << "-" << _month << "-" << _day << endl;
}
int Date::GetMonthDay()const
{<!-- -->
assert(_year >= 1 & amp; & amp; _month >= 1 & amp; & amp; _month<=12);
int day=0;
day + = _day;
int Array[] = {<!-- --> 0,31,28,31,30,31,30,31,31,30,31,30,31};
if (_month == 2 & amp; & amp; (_year % 4 == 0 & amp; & amp; _year % 100 != 0 || _year % 400 == 0))
{<!-- -->
return 29;
}
return Array[_month];
}

//d1 + =50;
Date & Date::operator + =(int day)
{<!-- -->
if (day < 0)
{<!-- -->
return *this -= (-day);
}
_day + = day;
while (_day > GetMonthDay())
{<!-- -->
_day -= GetMonthDay();
_month + + ;
if (_month > 12)
{<!-- -->
_month = 1;
_year + + ;
}
}
return (*this);
}
//d2=d1 + 100
Date Date::operator + (int day)const
{<!-- -->
Date tmp(*this);
tmp + = day;
return tmp;
}

//d1-=100
Date & Date::operator-=(int day)
{<!-- -->
if (day < 0)
{<!-- -->
return *this + = (-day);
}

_day -= day;
while (_day <= 0)
{<!-- -->
_month--;
if (_month == 0)
{<!-- -->
_month = 12;
_year--;
}
_day + = GetMonthDay();
}
return (*this);
}
//d1=d2-100
//Date - number of days
Date Date::operator-(int day)const
{<!-- -->
Date tmp(*this);
tmp -= day;
return tmp;
}

// + + d1
Date & Date::operator + + ()
{<!-- -->
*this + = 1;
return *this;
}
//d1 + +
Date Date::operator + + (int)
{<!-- -->
Date tmp(*this);
*this + = 1;
return tmp;
}

//--d1
Date & Date::operator--()
{<!-- -->
*this -= 1;
return *this;
}
//d1--
Date Date::operator--(int)
{<!-- -->
Date tmp(*this);
*this -= 1;
return tmp;
}

bool Date::operator>(const Date & d)const
{<!-- -->
if (_year < d._year)
{<!-- -->
return false;
}
if (_year == d._year & amp; & amp; _month < d._month)
{<!-- -->
return false;
}
if (_year == d._year & amp; & amp; _month == d._month & amp; & amp; _day < d._day)
{<!-- -->
return false;
}
return true;
}
bool Date::operator==(const Date & d)const
{<!-- -->
if (_year == d._year & amp; & amp; _month == d._month & amp; & amp; _day == d._day)
{<!-- -->
return true;
}
return false;
}
bool Date::operator>=(const Date & d)const
{<!-- -->
return *this > d || *this == d;
}

bool Date::operator<(const Date & d)const
{<!-- -->
return !(*this>d || *this==d);
}

bool Date::operator<=(const Date & d)const
{<!-- -->
return *this < d || *this == d;
}

int Date::operator-(const Date & d)const
{<!-- -->
int day = 0;
Date min = *this;
Date max = d;
if (*this>d)
{<!-- -->
min = d;
max = *this;
}
while (min < max)
{<!-- -->
min + + ;
day + + ;
}
return day;

}

Analysis

Multi-file programming is used here, so in date.cpp, it is used outside the Date class and needs to be added with a scope qualifier;
For class objects that do not change, that is, the objects pointed to by this pointer, try to modify them with const, which can improve efficiency to a certain extent;
In date.h, both member variables and member functions only serve the purpose of declaration in the class. The definition of the member variables of the class will be defined in the object, and the member functions of the class are all in date. defined by cpp.

Constructor verification

When we defined, we already added the default value,

Therefore, there is no need to add a default value when defining. Adding it will not cause a conflict, making the compiler unable to distinguish which default value is required, causing conflicts;

When defining, we added a judgment on whether the date is legal, but there is no forced exit, only a warning is printed;

We will give three sets of data for verification:

Reuse of operators

For repeated steps, we can use another function to complete:

There is also a certain emphasis here. For parameters and return values, a temporary variable object will be created to copy the value of the parameter, and this means that the copy constructor must be called, so when we write the program, Call as few functions as possible to improve efficiency;

In the above program, the number of function calls:

If we write it in reverse:
!](https://img-blog.csdnimg.cn/eac3af7c12554ba0b67aef2d8c27db9a.png)

Note: For + =, it will change the value of the object pointed to by this pointer, so const cannot be used for modification here;
And + will return a value to the corresponding object in the end. This object is not the object pointed to by this pointer, so it can be modified with const;


We have not specified the overload of = in date.cpp, and the class will generate a default operator overload;

Prefix + + and postfix + +

In C++, the overloaded form of prefix++ is the same as the overloaded form of post++. In order to distinguish the two overloaded forms, add in the parameter when overloading post++ A parameter of type int, but this parameter is not passed when calling the function.

The preposition + + is directly + =1; while the postposition + + will wait until the end of the statement before adding 1, so first find an intermediate value tmp to replace it, indicating that there is no + 1 yet;

Validation of other operators