C++类和对象(中篇)

在C++类和对象上篇中介绍了类的定义、类的访问限定符及封装、类的作用域和实例化、类对象大小计算和this指针,在本章中,将会详细介绍类的默认成员函数,并实现一个完整的日期类。

目录

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

                2.构造函数

                2.1.构造函数特性 

                3.析构函数

                3.1.析构函数特性

                4.拷贝构造函数

                4.1.拷贝构造函数特性

                5.赋值运算符重载

                5.1.赋值运算符重载特性

                5.2.前置++和后置++重载

                6.日期类的实现

                6.1.日期类头文件部分(声明)

                6.2.日期类源文件部分(定义)


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

如果一个类中什么成员都没有,简称为空类。

空类中真的什么都没有吗?并不是,任何类在什么都不写时,编译器会自动生成以下6个默认成员 函数

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

C++类和对象(中篇)_第1张图片

2.构造函数

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

构造函数的主要任务并不是开空间创建对象,而是初始化对象

// 全缺省的构造函数
Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
}

2.1.构造函数特性 

 1. 函数名与类名相同。

 2. 无返回值。

 3. 对象实例化时编译器自动调用对应的构造函数。

 4. 构造函数可以重载。

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

 6. C++把类型分成内置类型(基本类型)和自定义类型。内置类型就是语言提供的数据类型,如:int/char...,自定义类型就是我们使用class/struct/union等自己定义的类型,默认生成的构造函数对内置类型是不做处理的,对于自定义类型会去调用它的默认构造函数

 7. 无参的构造函数和全缺省的构造函数都称为默认构造函数,并且默认构造函数只能有一个。 注意:无参构造函数、全缺省构造函数、我们没写编译器默认生成的构造函数,都可以认为是默认构造函数。

3.析构函数

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

// 析构函数
Date::~Date()
{
	_year = 0;
	_month = 0;
	_day = 0;
}

3.1.析构函数特性

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

 2. 无参数无返回值类型。

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

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

 5. 编译器生成的默认析构函数,对内置类型不处理,对自定类型成员调用它的析构函数。

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

4.拷贝构造函数

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

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

4.1.拷贝构造函数特性

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

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

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

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

 5. 拷贝构造函数典型调用场景:

        使用已存在对象创建新对象

        函数参数类型为类类型对象

        函数返回值类型为类类型对象

5.赋值运算符重载

C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。

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

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

注意:

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

        重载操作符必须有一个类类型参数

        用于内置类型的运算符,其含义不能改变,例如:内置的整型+,不 能改变其含义

        作为类成员函数重载时,其形参看起来比操作数数目少1,因为成员函数的第一个参数为隐藏的this

        .* :: sizeof ?: . 注意以上5个运算符不能重载。这个经常在笔试选择题中出现。 

// 赋值运算符重载  d2 = d3 -> d2.operator=(&d2, d3)
Date& Date::operator=(const Date& d) 
{
	if (this != &d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}

	return *this;
}

5.1.赋值运算符重载特性

 1. 赋值运算符重载格式

        参数类型:const T&,传递引用可以提高传参效率

        返回值类型:T&,返回引用可以提高返回的效率,有返回值目的是为了支持连续赋值

        检测是否自己给自己赋值

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

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

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

 3. 用户没有显式实现时,编译器会生成一个默认赋值运算符重载,以值的方式逐字节拷贝。注 意:内置类型成员变量是直接赋值的,而自定义类型成员变量需要调用对应类的赋值运算符重载完成赋值

5.2.前置++和后置++重载

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

// 前置++
Date& Date::operator++()
{
	*this += 1;
	return *this;
}
// 后置++
Date Date::operator++(int)
{
	Date tmp(*this);
	*this += 1;
	return tmp;
}

以上介绍的是4个最为常用的默认成员函数,还有两个默认成员函数分别是const成员函数和取地址及const取地址操作符重载,这两个默认成员函数用处非常少,因此本章中不进行具体介绍!

6.日期类的实现

6.1.日期类头文件部分(声明)

#pragma once
#include
#include
#include
using namespace std;

class Date
{
public:
	// 获取某年某月的天数
	int GetMonthDay(int year, int month);

	// 全缺省的构造函数
	Date(int year = 1900, int month = 1, int day = 1);
	// 拷贝构造函数  d2(d1)
	Date(const Date& d);
	// 析构函数
	~Date();

	// 赋值运算符重载  d2 = d3 -> d2.operator=(&d2, d3)
	Date& 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);
	// !=运算符重载
	bool operator != (const Date& d);

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

    // 前置++
	Date& operator++();
	// 后置++
	Date operator++(int);
	// 后置--
	Date operator--(int);
	// 前置--
	Date& operator--();

	void Print();
private:
	int _year = 1;
	int _month = 1;
	int _day = 1;
};

6.2.日期类源文件部分(定义)

#define _CRT_SECURE_NO_WARNINGS 1
#include"Date.h"

// 全缺省的构造函数
Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
}
// 拷贝构造函数  d2(d1)
Date::Date(const Date& d)
{
	_year = d._year;
	_month = d._month;
	_day = d._day;
}
// 析构函数
Date::~Date()
{
	_year = 0;
	_month = 0;
	_day = 0;
}
// 获取某年某月的天数
int Date::GetMonthDay(int year, int month)
{
	assert(month > 0 && month < 13);
	int MonthDay[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)))
	{
		return 29;
	}
	return MonthDay[month];
}
// 赋值运算符重载  d2 = d3 -> d2.operator=(&d2, d3)
Date& Date::operator=(const Date& d) 
{
	if (this != &d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}

	return *this;
}
// >运算符重载
bool Date::operator>(const Date& d)
{
	//写法1 - 好理解
	//if (_year > d._year)
	//{
	//	return true;
	//}
	//else if (_year == d._year && _month > d._month)
	//{
	//	return true;
	//}
	//else if (_year == d._year && _month == d._month && _day > d._day)
	//{
	//	return true;
	//}
	//else
	//{
	//	return false;
	//}
	//写法2 - 便捷
	return (_year > d._year) ||
		(_year == d._year && _month > d._month) ||
		(_year == d._year && _month == d._month && _day > d._day);
}
// ==运算符重载
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);
}

// 日期+=天数
Date& Date::operator+=(int day)
{
	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		++_month;
		if (_month == 13)
		{
			++_year;
			_month = 1;
		}
	}
	return *this;
}
// 日期+天数
Date Date::operator+(int day)
{
	Date tmp = *this;
	tmp._day += day;
	while (tmp._day > GetMonthDay(tmp._year, tmp._month))
	{
		tmp._day -= GetMonthDay(tmp._year, tmp._month);
		++tmp._month;
		if (tmp._month == 13)
		{
			++tmp._year;
			tmp._month = 1;
		}
	}
	return tmp;
}
// 日期-=天数
Date& Date::operator-=(int day)
{
	if (day < 0)
	{
		*this += -day;
		return *this;
	}

	_day += day;
	while (_day <= 0)
	{
		--_month;
		if (_month < 1)
		{
			--_year;
			_month = 12;
		}
		_day += GetMonthDay(_year, _month);
	}
	return *this;
}
// 日期-天数
Date Date::operator-(int day)
{
	Date tmp(*this);
	tmp -= day;
	return tmp;
}
// 日期-日期 返回天数
int Date::operator-(const Date& d)
{
	Date max = *this;
	Date min = d;
	int flag = 1;
	if (*this < d)
	{
		max = d;
		min = *this;
		flag = -1;
	}

	int n = 0;
	while (min != max)
	{
		++n;
		++min;
	}
	return n * flag;
}

// 前置++
Date& Date::operator++()
{
	*this += 1;
	return *this;
}
// 后置++
Date Date::operator++(int)
{
	Date tmp(*this);
	*this += 1;
	return tmp;
}
// 后置--
Date Date::operator--(int)
{
	Date tmp(*this);
	*this -= 1;
	return tmp;
}
// 前置--
Date& Date::operator--()
{
	*this -= 1;
	return *this;
}

void Date::Print()
	{
		cout << _year << "年" << _month << "月" << _day << "日" << endl;
	}

你可能感兴趣的:(c++,开发语言)