类与对象(中)

类的6个默认成员函数

我们知道如果一个类中什么成员都没有,简称为空类。那么空类中真的什么都没有吗?并不是,任何类在什么都不写时,编译器会自动生成以下6个默认成员函数
默认成员函数:用户没有显式实现,编译器生成成员函数称为默认成员函数。

注意:只有需要用到这些函数且用户没有定义的时候,编译器才会去定义它们。

构造函数

概念: 

有如下一个Date类:

class Date
{
public:
	void Init(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date d1;
	d1.Init(1949, 10, 1);
	d1.Print();
	Date d2;
	d2.Init(2000, 1, 1);
	d2.Print();
	return 0;
}

对于上述Date类,可以通过 Init() 函数给对象初始化设置日期,但如果每次创建对象时都调用该函数初始化设置信息,未免太过麻烦,能否在对象创建时,就将信息初始化设置进去呢?

构造函数是一个特殊的成员函数名字与类名相同,创建类对象时由编译器自动调用,以保证每个数据成员都有一个合适的初始值,并且在对象整个生命周期内只调用一次

特性:

构造函数是特殊的成员函数,需要注意的是,构造函数虽然名称叫构造,但是构造函数的主要任 务并不是开空间创建对象,而是初始化对象

其特征如下:

1. 函数名与类名相同。

2. 无返回值(也不用写void,直接使用类名即可)。

3. 创建对象时编译器自动调用

4. 构造函数可以重载

class Date
{
public:
	//无参数构造函数
	Date()
	{

	}
	//含参数构造函数
	Date(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	
	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};

int main()
{
	Date d1;
	Date d2(2000, 1, 1);

	//Date d3();错误的使用方法,无法与函数声明区分
	return 0;
}

注意:

  • 含参数的构造函数也可以是缺省参数(全缺省、半缺省都可以)。
  • 一个类中不能多个构造函数同时存在
  • 如果通过无参构造函数(或全缺省构造函数)创建对象时,对象后面不用跟括号,否则就成了函数声明。

5. 如果类中用户没有显式定义构造函数,则C++编译器会自动生成一个无参的默认构造函数一旦用户显式定义编译器将不再生成。

有如下代码:

class Date
{
public:

	/*Date(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}*/
	
	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};

int main()
{
	Date d1;
	d1.Print();

	return 0;
}

可以正常编译运行:

而把注释内容放开后在编译运行就会报错:

6.看了上边调用默认构造函数的运行结果很多人会有疑惑:不实现构造函数的情况下,编译器会 生成默认的构造函数。但是看起来默认构造函数又没什么用,d1对象调用了编译器生成的默认构造函数,但是d1对象的_year、_month、_day,依旧是随机值。也就说在这里编译器生成的默认构造函数什么也没干。那么编译器生成的默认构造函数真的是什么也没干吗?

答案是否定的。首先我们要知道C++把类型分成内置类型(基本类型)自定义类型内置类型就是语言提供的数据类型,如:int/char...,自定义类型就是我们使用class/struct/union等自己定义的类型。编译器生成的默认构造函数不会处理内置类型,它的作用是调用类成员中其他自定义类型成员的默认成员函数

class Time
{
public:
	Time()
	{
		cout << "Time()" << endl;
		_hour = 0;
		_minute = 0;
		_second = 0;
	}
private:
	int _hour;
	int _minute;
	int _second;
};
class Date
{
private:
	// 基本类型(内置类型)
	int _year;
	int _month;
	int _day;
	// 自定义类型
	Time _t;
};
int main()
{
	Date d;
	return 0;
}

运行结果:

7. 无参的构造函数全缺省的构造函数都称为默认构造函数,并且默认构造函数只能有一个

注意:无参的构造函数、全缺省的构造函数、我们没写编译器默认生成的构造函数,都可以认为是默认构造函数

看下边这段代码:

class Date
{
public:
	Date()
	{
		_year = 1000;
		_month = 1;
		_day = 1;
	}

	Date(int year = 1949, int month = 10, int day = 1)
	{
		_year = 2000;
		_month = 2;
		_day = 2;
	}

	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};

int main()
{
	Date d;

	return 0;
}

编译会报错:

 虽然全缺省函数和无参函数构成重载,但当无参调用时编译器无法进行区分,就无法通过编译。因此默认构造函数只能存在一个。

再看另一段代码:

class Date
{
public:
	Date()
	{
		_year = 1000;
		_month = 1;
		_day = 1;
	}

	Date(int year, int month = 10, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}

	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};

int main()
{
	Date d1;
	d1.Print();

	Date d2(1949);
	d2.Print();

	return 0;
}

这段代码就可以正常运行:

可以发现上述代码中存在一个无参的默认构造函数和一个有参的半缺省构造函数,代码能够正常编译运行就说明构造函数可以重载、可以有多个,因此上边描述时一直强调的是默认构造函数只能存在一个

同时默认构造函数只能存在一个也是为了避免在无参调用时出现无法区分的情况

因此在定义构造函数时,最好只定义一个,避免出现问题。

析构函数

概念:

与构造函数功能相反,析构函数不是完成对对象本身的销毁,局部对象销毁工作是由编译器完成的。而对象在销毁时会自动调用析构函数完成对象中资源(malloc等开辟的空间)的清理工作

特性:

析构函数是特殊的成员函数,其特征如下:

1. 析构函数名是在类名前加上字符  ~  

2. 无参数无返回值类型

3. 一个类只能有一个析构函数若未显式定义,系统会自动生成默认的析构函数。注意:析构函数不能重载。

4. 对象生命周期结束时,C++编译系统自动调用析构函数。

有如下代码:

class Stack
{
public:
	Stack(int capacity = 3)
	{
		cout << "Stack(int capacity = 3)" << endl;

		_array = (int*)malloc(sizeof(int) * capacity);
		if (nullptr == _array)
		{
			perror("malloc fail");
			exit(-1);
		}
		_capacity = capacity;
		_size = 0;
	}

	void Push(int data)
	{
		_array[_size] = data;
		_size++;
	}


	~Stack()
	{
		cout << "~Stack()" << endl;

		if (_array)
		{
			free(_array);
			_array = NULL;
			_capacity = 0;
			_size = 0;
		}
	}
private:
	int* _array;
	int _capacity;
	int _size;
};
void TestStack()
{
	Stack s;
	s.Push(1);
	s.Push(2);
}

int main()
{
	TestStack();
	return 0;
}

运行结果:

5.对于编译器自动生成的析构函数,它无法完成对资源的清理,其主要功能是调用类成员中其他自定义类型成员的析构函数

有如下代码:

class Date
{
public:
	~Date()
	{
		cout << "~Date()" << endl;
	}
private:
	int _year = 1970;
	int _month = 1;
	int _day = 1;
};

class a
{
public:
	a(int n = 3)
	{
		cout << "a(int capacity = 3)" << endl;

		_a = (int*)malloc(sizeof(int) * n);
		if (nullptr == _a)
		{
			perror("malloc fail");
			exit(-1);
		}
		_n = n;
		_i = 0;

		for (; _i < _n; _i++)
		{
			_a[_i] = 1;
		}
	}

	~a()
	{
		cout << "~a()" << endl;

		if (_a)
		{
			free(_a);
			_a = NULL;
			_n = 0;
			_i = 0;
		}
	}
private:
	int* _a;
	int _n;
	int _i;

	Date d;
};

class b
{
public:
	b(int n = 3)
	{
		cout << "b(int capacity = 3)" << endl;

		_b = (int*)malloc(sizeof(int) * n);
		if (nullptr == _b)
		{
			perror("malloc fail");
			exit(-1);
		}
		_n = n;
		_i = 0;

		for (; _i < _n; _i++)
		{
			_b[_i] = 2;
		}
	}

private:
	int* _b;
	int _n;
	int _i;

	Date d;
};

int main()
{
	a aa;
	b bb;

	return 0;
}

上述代码中a类和b类的区别只有是否显式定义了析构函数,a类有显式定义的析构函数,b类没有显式定义的析构函数。

调试代码,监视aa、bb两个类对象中指针_a、_b指向空间数据的变化:

可以发现aa对象中的资源已经被清理了,而bb对象中的资源并没有被清理

再看代码运行结果:

可以发现Date类的析构函数被调用了两次,说明无论是显示定义的析构函数还是编译器默认生成的析构函数都可以调用类成员中其他自定义类型成员的析构函数。

6. 如果类中没有申请资源时,析构函数可以不写,直接使用编译器生成的默认析构函数,比如 Date类;有资源申请时,一定要写,否则会造成内存泄漏,比如Stack类。

拷贝构造函数

概念:

只有单个形参该形参是对类类型对象的引用(一般常用const修饰),在已存在的类类型对象创建新的同类型对象时由编译器自动调用

特性:

拷贝构造函数也是特殊的成员函数,其特征如下:

1. 拷贝构造函数是构造函数的一个重载形式

2. 拷贝构造函数的参数只有一个且必须是对类类型对象的引用,使用传值方式编译器直接报错, 因为会引发无穷递归调用。

如下代码:

class Date
{
public:
     Date(int year = 1900, int month = 1, int day = 1)
     {
         _year = year;
         _month = month;
         _day = day;
     }
     //Date(const Date d) // 错误写法:编译报错,会引发无穷递归
     Date(const Date & d) // 正确写法 
     {
         _year = d._year;
         _month = d._month;
         _day = d._day;
     }

     void Print()
     {
         cout << _year << "/" << _month << "/" << _day << endl;
     }
private:
     int _year;
     int _month;
     int _day;
};
int main()
{
    Date d1;
    Date d2(d1);

    d1.Print();
    d2.Print();
    return 0;
}

运行结果:

3. 若未显式定义编译器会生成默认的拷贝构造函数。 默认的拷贝构造函数对对象按内存存储字节序完成拷贝,这种拷贝叫做浅拷贝,或者值拷贝。

有如下代码: 

class Date
{
public:
     Date(int year = 1900, int month = 1, int day = 1)
     {
         _year = year;
         _month = month;
         _day = day;
     }
   
     void Print()
     {
         cout << _year << "/" << _month << "/" << _day << endl;
     }
private:
     int _year;
     int _month;
     int _day;
};
int main()
{
    Date d1;
    Date d2(d1);

    d1.Print();
    d2.Print();
    return 0;
}

上述代码,Date类中没有显式定义的拷贝构造函数,运行结果:

注意:在编译器生成的默认拷贝构造函数中,内置类型是直接按内存存储字节序完成拷贝的,而自定义类型是调用其拷贝构造函数完成拷贝的

有如下代码: 

class Time
{
public:
    Time()
    {
        _hour = 1;
        _minute = 1;
        _second = 1;
    }
    Time(const Time& t)
    {
        cout << "Time(const Time&)" << endl;

        _hour = t._hour;
        _minute = t._minute;
        _second = t._second;
    }
private:
    int _hour = 1;
    int _minute = 2;
    int _second = 3;
};
class Date
{
    int _year = 1970;
    int _month = 1;
    int _day = 1;

    Time _t;
};
int main()
{
    Date d1;

    Date d2(d1);
    return 0;
}

运行结果:

代码中并没有显式调用 _t 对象的拷贝构造函数,但是在程序运行过程中还是调用了,说明在调用d2对象的默认拷贝构造函数时,会调用 _t 对象的拷贝构造函数。

4. 编译器生成的默认拷贝构造函数已经可以完成字节序的值拷贝了,还需要自己显式实现吗?

下来看看下边这段代码:

class a
{
public:
	a(int n = 3)
	{
		cout << "a(int capacity = 3)" << endl;

		_a = (int*)malloc(sizeof(int) * n);
		if (nullptr == _a)
		{
			perror("malloc fail");
			exit(-1);
		}
		_n = n;
		_i = 0;

		for (; _i < _n; _i++)
		{
			_a[_i] = 1;
		}
	}

	~a()
	{
		cout << "~a()" << endl;

		if (_a)
		{
			free(_a);
			_a = NULL;
			_n = 0;
			_i = 0;
		}
	}
private:
	int* _a;
	int _n;
	int _i;
};

int main()
{
	a a1;
	
	a a2(a1);
	return 0;
}

上述代码没有显式定义的拷贝构造函数,调试代码,并监视a1、a2两个对象的_a指针成员:

可以发现,编译器生成的默认拷贝构造函数对于指针类型也是直接进行值拷贝,也就是说调用拷贝构造函数后,a1、a2两个对象的 _a 指针会指向同一块空间,当要销毁a1、a2两个对象时会调用两次析构函数,就会对 _a 指针指向的空间进行重复的释放操作,就会造成程序崩溃

注意:类中如果没有涉及资源申请时,拷贝构造函数是否写都可以;一旦涉及到资源申请时,则拷贝构造函数是一定要写的,否则就会进行浅拷贝。

运算符重载

概念:

大家都知道对于内置类型可以通过“+、-、*、/、= 、>、<”等符号进行变量的各种运算、赋值、比较的操作,那么对于C++的自定义类型想要实现类对象的加减乘除等操作,该怎么办呢?

为了解决这个问题引入了运算符重载的概念,运算符重载是具有特殊函数名的函数,也具有其返回值类型,函数名字以及参数列表,其返回值类型和参数列表与普通的函数类似

函数名字为:关键字operator后面接需要重载的运算符符号。

函数原型:返回值类型  operator操作符(参数列表)

注意:

1、不能通过连接其他符号来创建新的操作符:比如operator@。

2、重载操作符必须有一个类类型参数用于内置类型的运算符,其含义不能改变,例如:内置的整型+,不能改变其含义。

3、.* 、:: sizeof ?:.   注意以上5个运算符不能重载。

4、运算符重载和函数重载没有关系。

5、运算符能否重载取决于其是否有意义。

class Date
{
public:
    Date(int year = 1900, int month = 1, int day = 1)
    {
        _year = year;
        _month = month;
        _day = day;
    }
//private:
    int _year;
    int _month;
    int _day;
};

// 全局的operator==
bool operator==(const Date& d1, const Date& d2)
{
    return d1._year == d2._year
        && d1._month == d2._month
        && d1._day == d2._day;
}
void Test()
{
    Date d1(2018, 9, 26);
    Date d2(2018, 9, 27);
    cout << (d1 == d2) << endl;
}

上述代码中重载了一个全局的‘ == ’操作符,但是此时又涉及到了访问权限的问题,只能把类中的变量定义成公有的,或者使用友元解决,但是这样做又会破坏类的封装性,因此直接重载成成员函数更好一点。

class Date
{
public:
    Date(int year = 1900, int month = 1, int day = 1)
    {
        _year = year;
        _month = month;
        _day = day;
    }

    //bool operator==(Date* this, const Date& d2)
    bool operator==(const Date& d2)
    {
        return _year == d2._year
            && _month == d2._month
            && _day == d2._day;
    }
private:
    int _year;
    int _month;
    int _day;
};

void Test()
{
    Date d1(2018, 9, 26);
    Date d2(2018, 9, 27);
    cout << (d1 == d2) << endl;
}

注意:重载为类成员函数后形参看起来比操作数数目少1因为成员函数的第一个参数为隐藏的this指针。

赋值运算符重载:

1. 赋值运算符重载格式:

参数类型:const  类对象&,传引用可以提高传参效率。

返回值类型:类对象&,返回引用可以提高返回的效率,有返回值目的是为了支持连续赋值检测是否自己给自己赋值。

返回*this :要符合连续赋值的含义。


class Date
{
public:
    Date(int year = 1900, int month = 1, int day = 1)
    {
        _year = year;
        _month = month;
        _day = day;
    }

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

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

        return *this;
    }
private:
    int _year;
    int _month;
    int _day;
};

2. 赋值运算符只能重载成类的成员函数不能重载成全局函数。

因为赋值运算符如果不显式实现,编译器会生成一个默认的。此时再在类外自己实现一个全局的赋值运算符重载,就和编译器在类中生成的默认赋值运算符重载冲突了,故赋值运算符重载只能是类的成员函数。

看下边这段代码:

class Date
{
public:
    Date(int year = 1900, int month = 1, int day = 1)
    {
        _year = year;
        _month = month;
        _day = day;
    }

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


    int _year;
    int _month;
    int _day;
};

Date& operator=(Date& d1, const Date& d2)
{
    if (&d1 != &d2)
    {
        d1._year = d2._year;
        d1._month = d2._month;
        d1._day = d2._day;
    }

    return d2;
}
void Test()
{
    Date d1(2018, 9, 26);
    Date d2(2018, 9, 27);
    d2 = d2;
}

编译结果:

3. 用户没有显式实现时,编译器会生成一个默认赋值运算符重载以值的方式逐字节拷贝。此时就会出现和默认拷贝构造函数类似的问题。

注意:内置类型成员变量是直接赋值的,而自定义类型成员变量需要调用对应类的赋值运算符重载完成赋值

有如下代码:

class A
{
public:
	A(int a=1, int n=5)
	{
		_a = a;
		_p = new int[5]{ 0 };
	}
	
	~A()
	{
		delete[] _p;
		_p = nullptr;
	}
private:
	int _a;
	int* _p;
};

int main()
{
	A a1(1,2);
	A a2(3,4);

	a1 = a2;

	return 0;
}

调试代码监视a1、a2对象的 _p 指针的变化:

可以发现赋值操作之后a1、a2对象的 _p 指针指向了同一块空间,在销毁a1、a2时会调用两次delete 就会对同一块空间进行重复的释放操作,进而导致程序崩溃

自增自减运算符重载:

前置++后置++都是一元运算符,再使用常规的运算符重载无法让前置++与后置++形成正确的重载,C++为了让前置++和后置++能形成正确重载,规定:后置++重载时多增加一个int类型的参数,但调用函数时该参数不用传递编译器自动传递

class Date
{
public:
    Date(int year = 1900, int month = 1, int day = 1)
    {
        _year = year;
        _month = month;
        _day = day;
    }

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

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

        return *this;
    }

    //前置++(先+1,再使用)
    Date& operator++()
    {
        _day += 1;
        return *this;
    }

    //后置++(先使用,再+1)
    Date operator++(int)
    {
        Date temp(*this);
        _day += 1;
        return temp;
    }

    void Print()
    {
        cout << _year << "-" << _month << "-" << _day << endl;
    }
private:
    int _year;
    int _month;
    int _day;
};
void Test()
{
    Date d1(2000,2,2);
    Date d2(1000,1,1);
    d1.Print();
    d2.Print();

    Date d3, d4;

    d3 = d1++;
    d4 = ++d2;

    d3.Print();
    d4.Print();
}
int main()
{
    Test();
    return 0;
}

前置--和后置--的实现与++类似,就不多演示。

实现一个日期类:

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

class Date

{
public:
		
	// 获取某年某月的天数
	int GetMonthDay(int year, int month);
	// 输出数据
	void Print();
	
	//默认成员函数
	// 全缺省的构造函数
	Date(int year = 1949, int month = 10, int day = 1);

	// 拷贝构造函数
	Date(const Date& d);

	//析构函数
	~Date();

	// 赋值运算符重载
    // d2 = d3 -> d2.operator=(&d2, d3)
	Date& operator=(const Date& d);

	// 日期+=天数
	Date& operator+=(int day);

	// 日期+天数
	Date operator+(int day);

	// 日期-天数
	Date operator-(int day);

	// 日期-=天数
	Date& operator-=(int day);

	// 前置++
	Date& operator++();

	// 后置++
	Date operator++(int);

	// 后置--
	Date operator--(int);

	// 前置--
	Date& operator--();

	// >运算符重载
	bool operator>(const Date& d);

	// ==运算符重载
	bool operator==(const Date& d);

	// >=运算符重载
	bool operator >= (const Date& d);

	// <运算符重载
	bool operator < (const Date& d);

	// <=运算符重载
	bool operator <= (const Date& d);

	// !=运算符重载
	bool operator != (const Date& d);

	// 日期-日期 返回天数
	int operator-(const Date& d);

private:

	int _year;

	int _month;

	int _day;

};

// 获取某年某月的天数
int Date::GetMonthDay(int year, int month)
{
	assert(year >= 1 && month >= 1 && month <= 12);

	int a[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	if (month == 2 && (year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
	{
		a[2] = 29;
	}
	return a[month];
}
// 输出数据
void Date::Print()
{
	cout << _year << "-" << _month << "-" << _day << endl;
}


//默认成员函数
// 全缺省的构造函,声明与定义不能同时出现缺省值
Date::Date(int year, int month, int day)
{
	//cout << this <<"构造" << endl;

	_year = year;
	_month = month;
	_day = day;

	if (_year <= 0 ||
		(_month <= 0 || _month > 12) ||
		(_day <= 0 || _day > GetMonthDay(_year, _month)))
	{
		Print();
		cout << "日期非法" << endl;
	}
}

// 拷贝构造函数
Date::Date(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}

//析构函数
Date::~Date()
{
	//cout << this << "析构" << endl;

	_year = 0;
	_month = 0;
	_day = 0;
}



// 赋值运算符重载
Date& Date::operator=(const Date& d)
{
	if (this == &d)
	{
		return *this;
	}

	_year = d._year;
	_month = d._month;
	_day = d._day;

	return *this;

}

// 日期+=天数
Date& Date::operator+=(int day)
{
	if (day >= 0)
	{
		_day += day;
		while (_day > GetMonthDay(_year, _month))
		{
			_day -= GetMonthDay(_year, _month);

			_month++;
			if (_month == 13)
			{
				_month = 1;
				_year++;
			}
		}
	}
	else
	{
		*this -= (-day);
	}

	return *this;
}

// 日期+天数
Date Date::operator+(int day)
{
	Date tem(*this);

	tem += day;

	return tem;
}

// 日期-=天数
Date& Date::operator-=(int day)
{
	if (day >= 0)
	{
		_day -= day;
		while (_day <= 0)
		{
			_month--;
			if (_month == 0)
			{
				_month = 12;
				_year--;
			}

			_day += GetMonthDay(_year, _month);
		}
	}
	else
	{
		*this += (-day);
	}

	return *this;
}

// 日期-天数
Date Date::operator-(int day)
{
	Date tem(*this);

	tem -= day;

	return tem;
}

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

	* this += 1;

	return *this;
}

// 后置++
Date Date::operator++(int)
{
	Date tem(*this);

	//直接实现
	/*_day++;
	if (_day > GetMonthDay(_year, _month))
	{
		_day = 1;
		_month++;
		if (_month > 12)
		{
			_month = 1;
			_year++;
		}
	}*/

	//函数复用
	* this += 1;

	return tem;
}


// 后置--
Date Date::operator--(int)
{
	Date tem(*this);

	//直接实现
	/*_day--;
	if (_day == 0)
	{
		_month--;
		if (_month <= 0)
		{
			_month = 12;
			_year--;
		}
		_day = GetMonthDay(_year, _month);
	}*/

	//函数复用
	* this -= 1;

	return tem;
}

// 前置--
Date& Date::operator--()
{
	//直接实现
	/*_day--;
	if (_day == 0)
	{
		_month--;
		if (_month <= 0)
		{
			_month = 12;
			_year--;
		}
		_day = GetMonthDay(_year, _month);
	}*/

	//函数复用
	* this -= 1;

	return *this;
}

// >运算符重载
bool Date::operator>(const Date& d)
{
	if (this == &d)//同一个对象
	{
		return false;
	}

	if (_year > d._year)
	{
		return true;
	}
	if (_year == d._year && _month > d._month)
	{
		return true;
	}
	if (_year == d._year && _month == d._month && _day > d._day)
	{
		return true;
	}

	return false;
}

// ==运算符重载
bool Date::operator==(const Date& d)
{
	return _year == d._year && 
		   _month == d._month && 
		   _day == d._day;
}

// >=运算符重载
bool Date::operator >= (const Date& d)
{
	return *this > d || *this == d;
}

// <运算符重载
bool Date::operator < (const Date& d)
{
	return !(*this >= d);
}

// <=运算符重载
bool Date::operator <= (const Date& d)
{
	return !(*this > d);
}

// !=运算符重载
bool Date::operator != (const Date& d)
{
	return !(*this == d);
}

// 日期-日期 返回天数
int Date::operator-(const Date& d)
{
	if (this == &d)//同一个对象
	{
		return 0;
	}

	int day = 0;
	int flag = 1;
	Date max(*this);//默认第一个日期大
	Date min(d);

	if (*this < d)
	{
		max = d;
		min = *this;
		flag = -1;
	}

	while (max > min)
	{
		min++;
		day++;
	}

	return day * flag;
}

const成员

const修饰的“成员函数”称之为const成员函数,const修饰类成员函数,实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改

注意:

1、同名的const成员函数和非const成员函数是构成函数重载的。

2、this指针不可改变指向,是一个隐含的const修饰的指针变量。

来看下边一段代码:

class A
{
public:
	void func1()
	{}

	void func2() const
	{}

	void func3()
	{
		func2();
	}

	void func4()const
	{
		func1();
	}

private:
	int _a = 1;
};

int main()
{
	A a1;
	const A a2;

	a1.func1();
	a1.func2();

	a2.func1();
	a2.func2();
}

编译结果:

 

总结: 

     const对象可以调用const成员函数
     const对象不能调用非const成员函数
      非const对象可以调用const成员函数
      非const对象可以调用非const成员函数
      const成员函数不能调用非const成员函数
      非const成员函数可以调用非const成员函数

取地址及const取地址操作符重载

class Date
{
public:
	Date* operator&()
	{
		return this;
	}
	const Date* operator&()const
	{
		return this;
	}
private:
	int _year; 
	int _month;
	int _day; 
};

这两个默认成员函数一般不用重新定义 ,编译器默认会生成,使用编译器生成的默认取地址的重载即可,只有特殊情况,才需要重载,比如想让别人获取到指定的内容

评论 36
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值