c++类和对象2

1.类的六个默认成员函数

在c++中,一个空类中什么都不存在吗?
编译器会自动在类中生成六个下面的默认成员函数
在这里插入图片描述

默认成员函数:用户没有显示实现,编译器会生成的成员函数称为默认成员函数

1.1构造函数(构造函数是特殊的成员函数


构造函数的特征:
1.没有返回值
2.函数名与类名相同
3.对象实例化的时候编译器自动调用对应的构造函数
4.构造函数可以重载
实例:

在这里插入图片描述

构造函数重载

在这里插入图片描述

5.如果用户不显示定义构造函数,编译器会自动生成一个无参的默认构造函数,如果用户定义了,编译器就不再生成
默认生成的构造函数对内置类型(int/char/double…)不做处理,对自定义类型(class/struct)调用他的默认构造函数
默认构造函数:(编译器自动调用的,不需要传参)
我们不写,编译器自动生成的
全缺省的
无参的
在c++11的时候c++打了一个补丁,在定义变量的时候,我们可以给一个缺省值

在这里插入图片描述

(构造函数实际上完成了成员变量的初始化,不如叫他初始化函数更合适)

1.2析构函数

析构函数与构造函数的功能相反,对象在销毁时会调用析构函数,完成对资源的清理工作。
比如:日期类就不需要我们显示的写析构函数,因为当函数结束时栈会自动销毁,日期类定义的变量都会被销毁,但是栈类就需要我们写析构函数,因为栈中有动态开辟的内存空间需要手动释放
1.析构函数是类名前面加上~
2.无参数无返回值
3.一个类只能有一个析构函数,如果未显示定义编译器会自动生成一个默认析构函数。析构函数不能重载
4.对象生命周期结束时,c++编译器自动调用析构函数

~Stack()
    {
        free(_array);                                
    }

一个栈类的析构函数在这里插入代码片
5.默认生成的析构函数:
内置类型不处理,自定义类型调用他的默认析构函数

1.3拷贝构造函数(特殊的成员函数)

1.拷贝构造函数是构造函数的一个重载
2.拷贝构造只有一个参数必须是类类型对象的引用,传值得话编译器会直接报错,因为会引发无穷递归

Date(const Date& d)
    {
        _year = d._year;
        _month = d._month;
        _day = d._day;
    }

拷贝构造

在这里插入图片描述

3.当我们不显示定义拷贝构造函数的时候,编译器会默认生成一个拷贝构造函数
默认生成的拷贝构造,对内置类型按照字节直接拷贝,对自定义类型调用他的拷贝构造函数
在这里插入图片描述

把日期类的拷贝构造屏蔽

在这里插入图片描述

可以看到编译器自动生成的拷贝构造函数完成了日期类的拷贝,所以对日期类来说不需要写拷贝构造。

2.运算符重载

关键字:operator

在这里插入图片描述

完整日期类代码

#include<iostream>


using namespace std;


class Date


{
public:
    // 获取某年某月的天数
    int GetMonthDay(int year, int month)
    {
        static int days[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
        if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
        {
            days[2] = 29;
        }
        else
        {
            days[2] = 28;
        }
        return days[month];
    }


    // 全缺省的构造函数


    Date(int year = 1900, int month = 1, int day = 1)
    {
        _year = year;
        _month = month;
        _day = day;
        cout << "Date" << endl;
    }


    // 拷贝构造函数


  // d2(d1)


    Date(const Date& d)
    {
        _year = d._year;
        _month = d._month;
        _day = d._day;
    }


    // 赋值运算符重载


  // d2 = d3 -> d2.operator=(&d2, d3)


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


    // 析构函数


    ~Date()
    {
        cout << "~Date()" << endl;
    }


    // 日期+=天数


    Date& operator+=(int day)
    {
        _day += day;
        while(_day > GetMonthDay(_year, _month))
        {
            _month++;
            if (_month == 13)
            {
                _month = 1;
                _year++;
            }
            _day -= GetMonthDay(_year, _month);
        }
        return *this;
    }



    // 日期+天数


    Date operator+(int day)
    {
        Date day3(*this);
        day3._day += day;
        while (day3._day > GetMonthDay(day3._year, day3._month))
        {
            day3._month++;
            if (day3._month == 13)
            {
                day3._month = 1;
                day3._year++;
            }
            day3._day -= GetMonthDay(day3._year, day3._month);
        }
        return day3;
    }



    // 日期-天数


    Date operator-(int day)
    {
        Date day3(*this);
        while (day >= day3._day)
        {
            day -= day3._day;
            day3._month--;
            day3._day = GetMonthDay(day3._year, day3._month);
            if (day3._month == 0)
            {
                day3._month = 12;
                day3._year--;
            }
        }
        day3._day -= day;
        return day3;
    }


    // 日期-=天数


    Date& operator-=(int day)
    {
        while (day >= _day)
        {
            day -= _day;
            _month--;
            _day = GetMonthDay(_year, _month);
            if (_month == 0)
            {
                _month = 12;
                _year--;
            }
        }
        _day -= day;
        return *this;
    }


    // 前置++


    Date& operator++()
    {
        _day++;
        if (_day > GetMonthDay(_year, _month))
        {
            _day -= GetMonthDay(_year, _month);
            _month++;
            if (_month == 13)
            {
                _month = 1;
                _year++;
            }
        }
        return *this;
    }



    // 后置++


    Date operator++(int)
    {
        Date day3(*this);
        _day++;
        if (_day > GetMonthDay(_year, _month))
        {
            _day -= GetMonthDay(_year, _month);
            _month++;
            if (_month == 13)
            {
                _month = 1;
                _year++;
            }
        }
        return day3;
    }



    // 后置--


    Date operator--(int)
    {
        Date day3(*this);
        _day--;
        if (_day == 0)
        {
            _month--;
            _day = GetMonthDay(_year, _month);
            if (_month == 0)
            {
                _year--;
            }
        }
        return day3;
    }


    // 前置--


    Date& operator--()
    {
        _day--;
        if (_day == 0)
        {
            _month--;
            _day = GetMonthDay(_year, _month);
            if (_month == 0)
            {
                _year--;
            }
        }
    }


    // >运算符重载


    bool operator>(const Date& d)
    {
        if ((*this) - d > 0)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }


    // ==运算符重载


    bool operator==(const Date& d)
    {
        if ((*this) - d == 0)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }


    // >=运算符重载


    bool operator >= (const Date& d)
    {
        if ((*this) - d >= 0)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }


    // <运算符重载


    bool operator < (const Date& d)
    {
        if ((*this) - d < 0)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }


    // <=运算符重载


    bool operator <= (const Date& d)
    {
        if ((*this) - d <= 0)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }


    // !=运算符重载


    bool operator != (const Date& d)
    {
        if ((*this) - d != 0)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }


    // 日期-日期 返回天数

    int operator-(const Date& d)
    {
        int sum = 0;
        if (_year == d._year)
        {
            if (_month == d._month)
            {
                if (_day == d._day)
                {
                    return sum;
                }
                else
                {
                    sum += abs(_day - d._day);
                    return sum;
                }
            }
            else
            {
                int min_month = _month > d._month ? d._month:_month;
                int max_month = _month > d._month ? _month : d._month;
                int min_day = 0;
                int max_day = _day;
                if (min_month == _month)
                {
                    min_day = _day;
                }
                else
                {
                    min_day = d._day;
                }
                if (max_day == min_day)
                {
                    max_day = d._day;
                }
                sum += GetMonthDay(_year, min_month) - min_day;
                int i = min_month + 1;
                for (; i < max_month; i++)
                {
                    sum += GetMonthDay(_year, i);
                }
                sum += GetMonthDay(_year, max_month);
                return sum;
            }
        }
        else
        {
            Date max_date;
            Date min_date;
            if (_year > d._year)
            {
                Date max_date(*this);
                Date min_date(d);
            }
            else
            {
                Date min_date(*this);
                Date max_date(d);
            }
            sum += GetMonthDay(min_date._year, min_date._month)-min_date._day;
            int i = min_date._year;
            int j = min_date._month+1;
            while (i <= max_date._year)
            {
                sum += GetMonthDay(i, j);
                j++;
                if (i == max_date._year)
                {
                    if (j == max_date._month+1)
                    {
                        return sum;
                    }
                }
                if (j == 13)
                {
                    j = 0;
                    i++;
                }
            }


        }
    }


private:


    int _year;


    int _month;


    int _day;


};


int main()
{
    Date day1(2022,10,26);
    Date day2(2022,1,26);
    cout << day1 - day2 << endl;
    cout << (day1 >= day2) << endl;
    day1 = day2;
    cout << (day1 <= day2) << endl;
    cout << (day1 < day2) << endl;
    cout << (day1++ > day2) << endl;
    cout << (day1 <= ++day2) << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值