类与对象(二)

本文详细介绍了C++中的类成员函数,包括构造函数、析构函数、拷贝构造函数和赋值运算符重载。构造函数用于初始化对象,析构函数在对象销毁时清理资源。拷贝构造函数用于复制对象,而赋值运算符重载则确保对象正确赋值。同时,文章通过日期类的实现,演示了这些概念的实际应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

如果一个类中什么也没有,这个类是空类吗?并不是,任何一个类在我们不写的情况下,会自动生成下面6个默认成员函数。
类的6个默认成员函数

2.构造函数

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

构造函数的特征:

  1. 函数名与类名相同。
  2. 无返回值。
  3. 对象实例化时编译器自动调用对应的构造函数。
  4. 构造函数可以重载
  5. 如果类中没有显式定义构造函数,则C++编译器会自动生成一个无参的默认构造函数,一旦用户显式定义编译器将不再生成
  6. 无参的构造函数和全缺省的构造函数都称为默认构造函数,并且默认构造函数只能有一个。注意:无参构造函数、全缺省构造函数、我们没写编译器默认生成的构造函数,都可以认为是默认成员函数。
    (如果构造函数没有参数,或者构造函数的所有参数都有默认值,就可以称其为缺省构造函数。一个类中,只能有一个缺省构造函数。)
class Date
{
public:
	//无参构造函数
	Date()
	{}
	//带参构造函数
	Date(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}
private:
	int _year;
	int _month;
	int _day;
};

void TestDate()
{
	Date d1;  //调用无参构造函数
	//调用无参构造函数时,后边不用跟括号,跟了括号就是函数的声明而非对象的实例化
	Date d2(2001, 12, 25);  //调用带参构造函数
}

3. 析构函数

3.1析构函数概念

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

3.2析构函数特性

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

  1. 析构函数名是在类名前加上字符 ~。
  2. 无参数无返回值
  3. 一个类有且只有一个析构函数。若未显式定义,系统会自动生成默认的析构函数。
  4. 对象生命周期结束时,C++编译系统系统自动调用析构函数。
  5. 编译器生成的默认析构函数,对会自定类型成员调用它的析构函数
  6. 析构函数不可以重载

下面简单实现了一个栈,及其相关操作,在调用结束后,通过调试监视窗口可以看出系统会自动调用析构函数

typedef int DataType;

class Stack
{
public:
	//构造函数
	Stack()
	{
		_array = (DataType*)malloc(sizeof(DataType) * 10);
		if (NULL == _array)
		{
			assert(0);
			return;
		}
		_capacity = 10;
		_size = 0;
	}
		//析构函数
	~Stack()
	{
		if (_array)
		{
			free(_array);
			_array = nullptr;
			_capacity = 0;
			_size = 0;
		}
	}
	
	//尾插
	void Push(const DataType& data)
	{
		//...容量检查以及扩容等操作
		_array[_size] = data;
		_size++;
	}
	//判空
	bool Empty()
	{
		return NULL == _size;
	}
	//出栈
	void Pop()
	{
		if (Empty())
			return;
		_size--;
	}
	//获取栈顶元素
	DataType Top()
	{
		return _array[_size - 1];
	}
	size_t Size()
	{
		return _size;
	}

private:
	int _capacity;
	DataType* _array;
	int _size;
};

void TestStack()
{
	Stack s1;
	s1.Push(1);
	s1.Push(2);
	s1.Push(3);
	s1.Push(4);
	s1.Push(5);

	cout << s1.Size() << endl;
	cout << s1.Top() << endl;

	s1.Pop();
	s1.Pop();

	cout << s1.Size() << endl;
	cout << s1.Top() << endl;
	//当对对象的操作完成之后,操作系统自动调用析构函数
}

int main()
{
	TestStack();
	_CrtDumpMemoryLeaks();  //检测是否存在内存泄漏的函数
	return 0;
}

4.拷贝构造函数

4.1拷贝构造函数概念

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

4.2拷贝构造函数特征
  1. 拷贝构造函数是构造函数的一个重载形式
  2. 拷贝构造函数的参数只有一个且必须使用引用传参,使用传值方式会引发无穷递归调用
  3. 若未显示定义,系统生成默认的拷贝构造函数。 默认的拷贝构造函数对象按内存存储按字节序完成拷贝,这种拷贝我们叫做浅拷贝,或者值拷贝。

5.赋值运算符重载

为什么要有运算符重载?因为这样就可以告诉编译器,自定义的对象遇到该运算符时应该如何操作。

  • 函数名字为:关键字operator后面接需要重载的运算符符号。
  • 函数原型:返回值类型 operator操作符(参数列表)
  • 注意:
  • 不能通过连接其他符号来创建新的操作符:比如operator@
  • 重载操作符必须有一个类类型或者枚举类型的操作数
  • 用于内置类型的操作符,其含义不能改变,例如:内置的整型+,不能改变其含义
  • 作为类成员的重载函数时,其形参看起来比操作数数目少1成员函数的
  • 操作符有一个默认的形参this,限定为第一个形参
  • . 、:: 、sizeof 、?: 、.* 注意以上5个运算符不能重载。

6.日期类的实现

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};
	int day = days[month];
	if (month == 2 &&((year % 4 == 0 && year % 100 != 0) || (year%400 == 0)))
{
	day += 1;
}
	return day;
}
// 全缺省的构造函数
Date(int year = 1900, int month = 1, int day = 1)
{
	if(year < 1900|| month < 1 || month > 12|| day < 1 || day > GetMonthDay(year ,month) )
{
	cout<<"非法日期"<<endl;
}
_year = year;
_month = month;
_day = day;
}
// 拷贝构造函数
// d2(d1)
Date(const Date& d)
{
	this->_year = d._year;
	_month = d._month;
	_day = d._day;
}
// 赋值运算符重载
// d2 = d3 -> d2.operator=(&d2, d3)
Date& operator=(const Date& d)
{
	if (this != &d)
{
	this->_year = d._year;
	this->_month = d._month;
	this->_day = d._day;
}
	return *this;
}
// 析构函数
~Date()
{
	// 清理工作
}
void Print()
{
	cout<<_year<<"-"<<_month<<"-"<<_day<<endl;
}
// 日期+=天数
// d1 += 10
// d1 += -10
Date& operator+=(int day)
{
	if (day < 0)
	{
		return *this -= -day;
	}
	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		_month++;
	if (_month == 13)
	{
		_year++;
		_month = 1;
	}
	}
	return *this;
}
// 日期+天数
// d + 10
Date operator+(int day)
	{
		Date ret(*this);
		ret += day;
		return ret;
	}
// 日期-天数
Date operator-(int day)
	{
	Date ret(*this);
	ret -= day;
	return ret;
	}
// 日期-=天数
// d -= 100
// d -= -100
Date& operator-=(int day)
	{
	if (day < 0)
	{
	return *this += -day;
	}
_day -= day;
while (_day <= 0)
	{
	--_month;
	if (_month == 0)
	{
	--_year;
	_month = 12;
	}
	_day += GetMonthDay(_year, _month);
	}
	return *this;
	}
// 前置++
// ++d -> d.operator++(&d)
Date& operator++() 
	{
	*this += 1;
	return *this;
	}
// 后置++
// d++ -> d.operator++(&d, 0)
Date operator++(int) 
	{
	Date ret(*this);
	*this += 1;
	return ret;
	}
// // 后置--
Date operator--(int)
{
	Date ret(*this);
	*this -= 1;
	return ret;
}
// 前置--
Date& operator--()
	{
	*this -= 1;
	return *this;
	}
// d1 > d2
// >运算符重载
bool operator>(const Date& d)
	{
	if (_year > d._year)
	{
	return true;
	}
	else if (_year == d._year)
	{
	if (_month > d._month)
	{
	return true;
	}
	else if (_month == d._month)
{
	if (_day > d._day)
{
	return true;
}
}
}
	return false;
}
// ==运算符重载
bool operator==(const Date& d)
{
	return _year == d._year
	&& _month == d._month
	&& _day == d._day;
}
// 下面复用上面两个的实现
// >=运算符重载
inline bool operator >= (const Date& d)
{
	return *this > d || *this == d;
}
// <运算符重载
bool operator < (const Date& d)
{
	return !(*this >= d);
}
// <=运算符重载
bool operator <= (const Date& d)
{
	return !(*this > d);
}
// !=运算符重载
bool operator != (const Date& d)
{
	return !(*this == d);
}
// d1 - d2
// 日期-日期 返回天数
int operator-(const Date& d)
{
	int flag = 1;
	Date max = *this;
	Date min = d;
if (*this < d)
{
	max = d;
	min = *this;
	flag = -1;
}
int day = 0;
while (min < max)
{
	++(min);
	++day;
}
	return day*flag;
}
private:
	int _year;
	int _month;
	int _day;
};
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值