3_V1-类和对象 -- 默认成员函数

定义一个日期类

#include <iostream>
#include <assert.h>
using namespace std;

class Date
{
public:
    void Display();
private:
    int _year;
    int _month;
    int _day;
};

注意:

在定义一个类的时候往往会将其成员变量定义为私有,成员函数定义为公有.这是为了达到软件工程上的高内聚低耦合的要求

this指针

每一个函数都有自己的形式指针,它的名字是固定的,称为this,同时this指针是隐式的.
编译器会对成员函数进行优化,在对象调用成员函数的时候,此时会将该对象的地址传递给成员函数的第一个参数
同时this指针是成员函数隐含指针参数,我们程序员不能自己将其加到成员函数的形式参数列表中,也不能在调用的时候显示的将对象的地址传给this指针
这里写图片描述

注意:

this指针是形式参数,既然是形式参数那么它就会在栈上,但是在Linux下,this指针是被存放在寄存器中.同时谁来调用this成员函数那么this指针就指向谁

构造函数

构造函数可以达到原子性,即对象被定义的时候就会被初始化.在定义类的时候我们呢看到通常会将成员函数定义为私有但是私有成员变量在类外是不能被访问的.为了对成员函数进行初始化,此时就需要构造函数来对私有成员变量进行初始化.同时构造函数只在对象被定义的时候仅仅被执行一次.下面来说一下构造函数的特点
1.函数名和类名相同
2.无返回值
3.对象构造时系统自己调用对应的构造函数
4.构造函数可以重载(函数名相同, 参数不同)
5.构造函数可以在类里面定义也可以在类外面定义
6.如果类中没有定义一个构造函数,系统会默认生成一个缺省的构造函数,但是当我们定义了构造函数,此时系统就不会生成默认的缺省构造函数,而会定义我们自己定义的构造函数,编译器在对其进行初始化的时候,内置的类型会被初始化,但是自定义的不会进行初始化
7.无参的构造函数和全缺的构造函数都认为是缺省构造函数.并且缺省的构造函数只能有一个

几种构造函数

class Date
{
public:
    // 1.无参的构造函数
    // Date()
    // {
    // }
    // 2.缺省构造函数
    Date(int year = 1900, int month = 1, int day = 1)
    {
        if(year < 1900 || month < 0 || month > 12 || day < 0 || day > 31)
        {
            assert(0);
        }
        _year = year;
        _month = month;
        _day = day;
    }

    //3. 带参的构造函数
    // Date (int year, int month, int day)
    // {
    //     _year = year;
    //     _month = month;
    //     _day = day;
    // }
    // 4. 拷贝构造函数,创建一个对象
    Date(Date& d)
    {
        _year = d._year;
        _month = d._month;
        _day = d._day;
    }

    void Display();
private:
    int _year;
    int _month;
    int _day;
};

注意:

拷贝构造其实就可以理解为一种特殊的构造函数,它是构造函数的重载,同时注意拷贝构造是对象的创建,而我们后面将所说的赋值是两个对象都已经存在
同时拷贝构造必须传引用,因为如果传值的话那么就会出现无穷递归(拷贝构造就要传参,传参就要拷贝构造)
3.如果自己没有显示进行定义,那么系统就会自己生成默认缺省构造函数.缺省的时候拷贝构造函数就会依次拷贝类成员进行初始化

析构函数

1.和类的名字前面加上一个~
2.无返回值
3.一个类有且只有一个析构函数(因为没有参数,不能构成重载),如果我们自己不写编译器会自动生成
4.对象生命周期结束的时候,系统调用析构函数
5.析构函数不能删除对象,只是对对象进行清理

注意:

如果需要清理成员变量(动态开辟空间),则自己写析构函数,对象在定义的时候一定调用了构造,生命周期结束的时候就一定调用了析构函数

运算符重载

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

    // d2 == d3
    // d2.operator(*this, d3)
    bool operator == (const Date& d) 
    {
        if(this -> _year == d._year && this -> _month == d._month && this -> _day == _day)
        {
            return true;
        }
        return false;
    }

    bool operator != (const Date d)
    {
        if(!(*this == d))
        {
            return true;
        }
        return false;
    }
    //d1 > d2
    //d1.operator > (this, d2)
    bool operator > (const Date& d) 
    {
        if(this -> _year > d._year)
        {
            return true;
        }
        if(this -> _year == d._year)
        {
            if(this -> _month > d._month)
            {
                return true;
            }
        }
        if(this -> _month == d._month)
        {
            if(this -> _day > d._day)
            {
                return true;
            }
        }
        return false;
    }
    //d2 < d3
    //d2.operator(this, d3)
    bool operator < (const Date& d) 
    {
        if(*this == d || *this > d)
        {
            return false;
        }
        return true;
    }
    // d2 >= d3
    bool operator >= (const Date& d)
    {
        if(*this > d || *this == d)
        {
            return true;
        }
        return false;
    }

    //d2 <= d3
    bool operator <= (const Date& d)
    {
        if(*this < d || *this == d)
        {
            return true;
        }
        return false;
    }

    //d2++
    Date& operator ++ () // 前置 
    {
        ++( this -> _day );
        if(this -> _day > GetMonthDay(this -> _year, this -> _month))
        {
            this -> _day = 1;
            ++(this -> _month);
            if((this -> _month) > 12)
            {
                ++(this -> _year);
                this -> _month = 1;
            }
        }
        return *this;
    }

    //d2++
    Date operator ++ (int) // 后置 
    {
        Date ret = (*this);
        ++( *this );
        return ret;
    }

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

    Date operator -- (int)//后置
    {
        Date ret = *this;
        --(*this);
        return ret;
    }

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

    //d1 + 30
    //d1.operator(this, day)
    Date operator + (int day) 
    {
        Date ret = *this;
        ret += day;
        return ret;
    }

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

    Date operator - (int day)
    {
        Date ret = *this;
        ret -= day;
        return ret;
    }

    // 2018-1-31  -  2018-1-1
    // d1.operator(this, d2)
    int operator - (const Date& d)
    {
        int count = 0;//记录天数
        while(*this > d)
        {
            --(*this);
            ( *this ).Display();
            ++count;
        }
        return count;
    }

    void Display()//展示日期类
    {
        cout<<_year<<"-"<<_month<<"-"<<_day<<endl;
    }
    int GetMonthDay(int year, int month)
    {
        int day[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        if(( year %4 == 0 && year % 100 != 0 ) || ( year % 400 == 0 ))
        {
            day[month] = day[2] + 1;
        }
        return day[month];
    }

输入探索构造函数

类的成员函数有两种初始化方式
1.初始化列表
以一个冒号开始, 接着一个逗号分隔数据列表, 每个数据成员都在括号里进行初始化. 尽量使用初始化列表, 因为初始化列表更加高效.
2.构造函数体内进行赋值
这里写图片描述
初始化列表为什么更加高效
1.初始化列表不写, 编译器会自动走一次(自定义成员变量)
2.初始化列表可以认为是成员变量定义的地方
3.构造函数体内赋值会产生临时变量, 但是初始化列表不会产生临时变量

那些成员变量必须放在初始化列表中

1.常量成员变量(常量创建时必须初始化)
2.引用类型成员变量(引用成员变量创建时必须初始化)
3.没有缺省构造函数的自定义类型
4.成员变量初始化的时候按照声明顺序依次初始化,而非初始化列表出现的顺序

猜你喜欢

转载自blog.csdn.net/qq_41027326/article/details/80947988