C++【面试题】:类实现万年历(日期计算器),(含构造函数、拷贝构造、运算符重载、析构函数)

原创 2016年05月30日 17:16:26
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<stdlib.h>
using namespace std;

class Date
{
public:
    Date(int year=0, int month=0, int day=0)
    :_year(year)
    , _month(month)
    , _day(day)
    {
        cout << "构造函数" << endl;
    }

    Date( const Date& d)
    {
        cout << "拷贝构造函数" << endl;
        _year = d._year;
        _month = d._month;
        _day = d._day;
    }

    /*Date& operater = (const Date& d)
    {
        cout << "运算符重载" <<endl;

    }*/
    bool operator == (const Date&d)
    {
        return this->_year == d._year
            && this->_month == d._month
            &&this->_day = d._month;
    }

     //大于
    bool operator > (const Date& d)
    {
        if (this->_year > d._year)
        {
            return true;
        }
        else
        {
            if (this->_year == d._year)
            {
                if (this->_month > d._month)
                {
                    return true;
                }
                else
                {
                    if (this->_month == d._month)
                    {
                        if (this->_day > d._day)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            else
            {
                return false;
            }
        }
    }

    //小于
    bool operator < (const Date& d)
    {
        if (this->_year < d._year)
        {
            return true;
        }
        else
        {
            if (this->_year == d._year)
            {
                if (this->_month < d._month)
                {
                    return true;
                }
                else
                {
                    if (this->_month == d._month)
                    {
                        if (this->_day < d._day)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            else
            {
                return false;
            }
        }
    }

     //大于等于
    bool operator >= (const Date& d)
    {
        if (this->_year >= d._year)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    //小于等于
    bool operator <= (const Date& d)
    {
        if (this->_year <= d._year)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    //Date operator+ (int day);
    Date operator+ (int day)
    {
        
    }
    void Display()
    {
        cout << _year << "-" << _month << "-" << _day<<endl;
    }
    private:
        //判断平年闰年
        bool IsLeapYear(int year)
        {
            if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        //判断天数
        int GetMonthDay(int year, int month)
        {
            if (year && month)
            {
                int monthArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
                int day = monthArray[month];

                if (month == 2 && IsLeapYear(year))
                {
                    day += 1;
                }
                return day;
            }
        }

        //加天数,Date operator+ (int day)
        Date operator+ (int day)
        {
            int rest = 0;
            if (day >= 0)
            {
                this->_day += day;
                while (this->_day > GetMonthDay(2016, 5))
                {
                    rest = this->_day - GetMonthDay(2016, 2);
                    if (this->_month != 12)
                    {
                        this->_month++;
                    }
                    else
                    {
                        this->_year++;
                        this->_month = 1;
                    }
                    this->_day = rest;
                }
            }
            else
            {
                Date operator - 50;
            }
            return *this;
        }


    //加天数,Date operator+= (int day);
    Date operator+= (int day )
    {
        Date tmp(*this);
        tmp._day += day;
        int rest = 0;
        if (day > 0)
        {
            while (tmp._day > GetMonthDay(2016, 5))
            {
                rest = tmp._day - GetMonthDay(2016, 2);
                if (tmp._month != 12)
                {
                    tmp._month++;
                }
                else
                {
                    tmp._year++;
                    tmp._month = 1;
                }
                tmp._day = rest;
            }
        }
        else
        {
            Date operator-= 50;
        }
        return tmp;

    }

    //减天数,Date operator- (int day);
    Date operator - (int day)
    {
        int rest = 0;
        if (day <= 0)
        {
            this->_day -= day;
            while (this->_day > GetMonthDay(2016, 5))
            {
                rest = this->_day - GetMonthDay(2016, 2);
                if (this->_month != 1)
                {
                    this->_month--;
                }
                else
                {
                    this->_year--;
                    this->_month = 12;
                }
                this->_day = rest;
            }
        }
        else
        {
            Date operator + 50;
        }
        return *this;
    }

    //减天数,Date operator-= (int day);
    Date operator-= (int day)
    {
        int rest = 0;
        Date tmp(*this);
        if (day <= 0)
        {
            tmp._day -= day;
            while (tmp._day > GetMonthDay(2016, 5))
            {
                rest = tmp._day - GetMonthDay(2016, 2);
                if (tmp._month != 1)
                {
                    tmp._month--;
                }
                else
                {
                    tmp._year--;
                    tmp._month = 12;
                }
                tmp._day = rest;
            }
        }
        else
        {
            Date operator + 50;
        }
        return tmp;
    }

    //前置++
    Date operator++()
    {
        if (day > GetMonthDay(int year, int month))
        {
            if (month != 12)
            {
                ++this->day;
            }
            else
            {
                ++year;
                month = 1;
                day = 1;
            }
        }
        else
        {
            ++day;
        }
        return *this;
    }

    //后置++
    Date operator++(int)
    {
        Date tmp(*this);
        tmp._day;
        if (day > GetMonthDay(int year, int month))
        {
            if (month != 12)
            {
                ++tmp.day;
            }
            else
            {
                ++tmp.year;
                tmp.month = 1;
                tmp.day = 1;
            }
        }
        else
        {
            ++tmp.day;
        }
        return tmp;
    }

    //前置--
    Date operator++()
    {
        if (day > GetMonthDay(int year, int month))
        {
            if (month != 1)
            {
                ++this->day;
            }
            else
            {
                --year;
                month = 12;
                day = 1;
            }
        }
        else
        {
            --day;
        }
        return *this;
    }

    //后置--
    Date operator--(int)
    {
        Date tmp(*this);
        tmp._day;
        if (day > GetMonthDay(int year, int month))
        {
            if (month != 1)
            {
                --tmp.day;
            }
            else
            {
                --tmp.year;
                tmp.month = 12;
                tmp.day = 1;
            }
        }
        else
        {
            --tmp.day;
        }
        return tmp;
    }
    ~Date()
    {
        cout << "析构函数" << endl;
    }

private:
    int _year;
    int _month;
    int _day;
};

void Test1()
{
    Date d;
    d.Display();
    Date ret = operator + (50);
}
int main()
{
    Test1();
    system("pause");
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

总结c++类的构造函数 拷贝构造函数 析构函数 赋值运算符重载的特点以及函数调用顺序

对 c++类成员函数中的构造函数 拷贝构造函数 析构函数 赋值运算符重载的一些特点以及函数调用顺序,并附上例子,希望对大家有帮助,有错误大家可以指出来。

c++构造函数、拷贝构造函数、析构函数、赋值运算符重载

构造函数 构造函数的特点: 1.是一个特殊的成员函数; 2.名字与类名相同; 3.有初始化列表; 4.创建类类型对象时,由编译器自动调用,在对象的声明周期内只调用一次; 5.构造函数可以重载,实参决定...

C++ 构造函数 析构函数 拷贝构造函数 运算符重载解析。

C++ 构造函数, 析构函数, 构造函数重载, 重载运算符

c/c++ 构造函数、拷贝构造函数、析构函数和赋值运算符重载

构造函数 #include "stdafx.h" #include using namespace std; class String { public: String() {} 类的实例化...

c++实现日期类(class Date) 构造函数 拷贝构造 操作符重载(输入输出 比较操作 算数运算 自增自减)

c++实现日期类(class Date) 构造函数 拷贝构造 操作符重载(输入输出 比较操作 算数运算 自增自减)

编写String类的构造函数、析构函数以及赋值函数(C++面试题)

这是一道C++面试题:用C++编写一个String类,实现其构造函数、析构函数以及赋值函数 #ifndef STRING_H #define STRING_H #include class ...

继承的时候,子类的拷贝构造函数和重载运算符的实现

#include using namespace std; class Parent { public: Parent(){} Parent(int a):m_a(a){} Pa...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)