C++类和对象(中篇)

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

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

class Date{};

2.构造函数 

2.1 概念

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

以日期类为例

class Date
{
public:
	//构造函数,在对象构造时调用的函数,这个函数完成初始化工作
	//函数名跟类名相同,不用返回值
	/*Date(int year, int month, int day) //带参构造函数
	{
		_year = year;
		_month = month;
		_day = day;
	}
	Date()  //不带参构造函数
	{
		_year = 0;
		_month = 1;
		_day = 1;
	}*/
	//上述代码合并,更好的方法->全缺省
	Date(int year = 0, 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/char 不会处理
	//自定义类型 调用它的构造函数初始化
};

2.2 特性 

1. 函数名与类名相同。

2. 无返回值。

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

4. 构造函数可以重载。

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

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

3.析构函数

3.1 概念

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

3.2 特性

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

2. 无参数无返回值。

3. 一个类有且只有一个析构函数。若未显式定义,系统会自动生成默认的析构函数。

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

下面以栈类为例

class Stack
{
public:
	//对象创建时自动调用完成初始化工作
	Stack(int n = 10)
	{
		_a = (int*)malloc(sizeof(int) * n);
		_size = 0;
		_capacity = n;
	}
	//对象生命周期到了以后,完成清理工作
	~Stack()
	{
		free(_a);
		_size = _capacity = 0;
	}
private:
	int* _a;
	int _size;
	int _capacity;
};

4.拷贝构造函数

class Date
{
public:
	Date(int year = 0, 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;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date d1(2024, 8, 26);
	//这两个写法都是调用拷贝构造函数
	Date d2(d1);
	Date d3 = d1;

    return 0;
}

拷贝构造函数的参数只有一个且必须使用引用传参,使用传值方式会引发无穷递归调用。

5.赋值运算符重载

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

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

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

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

·重载操作符必须有一个类类型或者枚举类型的操作数 

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

·作为类成员的重载函数时,其形参看起来比操作数数目少1成员函数的 操作符有一个默认的形参this,限定为第一个形参

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

6.日期类的实现 

class Date
{
public:
	int GetmonthDay(int year, int month)
	{
		static int monthDays[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
		//月份是2月,且是闰年返回29
		if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
		{
			return 29;
		}
		return monthDays[month];
	}
	//全缺省构造函数
	Date(int year = 0, int month = 1, int day = 1)
	{
		if (year >= 0
			&& month >= 1 && month <= 12
			&& day >= 1 && day <= GetmonthDay(year, month))
		{
			_year = year;
			_day = day;
			_month = month;
		}
		else
			cout << "非法日期" << endl;
	}
	//拷贝构造函数
	Date(const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	bool operator<(const Date& d)
	{
		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;
		return false;
	}
	bool operator==(const Date& d)
	{
		return _year == d._year && _month == d._month && _day == d._day;
	}
	bool operator != (const Date& d)
	{
		return !(*this == d);
	}
	bool operator<=(const Date& d)
	{
		return *this < d || *this == d; //复用上面的来实现
	}
	Date operator+(int day)
	{
		Date ret(*this);  //用d1拷贝构造一个ret
		ret._day += day;
		while (ret._day > GetmonthDay(ret._year, ret._month))
		{
			ret._day -= GetmonthDay(ret._year, ret._month);
			ret._month++;
			if (ret._month == 13)
			{
				ret._year++;
				ret._month = 1;
			}
		}
		return ret;
	}
	Date& operator+=(int day)
	{
		_day += day;
		while (_day > GetmonthDay(_year, _month))
		{
			_day -= GetmonthDay(_year, _day);
			_month++;
			if (_month == 13)
			{
				_year++;
				_month = 1;
			}
		}
		return *this;
	}
	//d1 -= 10->d1.operator-=(&d1, 10)
	Date& operator-=(int day)
	{
		_day -= day;
		while (_day <= 0)
		{
			--_month;
			if (_month == 0)
			{
				_year--;
				_month = 12;
			}
			_day += GetmonthDay(_year, _month);
		}
		return *this;
	}
	//d1 - 10  -> d1.operator-(&d1,10)
	Date operator-(int day)
	{
		Date ret(*this); //拷贝构造一个ret
		ret._day -= day;
		while (ret._day <= 0)
		{
			--ret._month;
			if (ret._month == 0)
			{
				ret._year--;
				ret._month = 12;
			}
			ret._day += GetmonthDay(ret._year, ret._month);
		}
		return ret;
	}
	Date& operator=(const Date& d)
	{
		if (this != &d) //针对自己给自己赋值的检查判断
		{
			_year = d._year;
			_month = d._month;
			_day = d._day;
		}
		return *this;
	}
	//++d1
	Date& operator++()
	{
		*this += 1;
		return *this; //返回加之后的值
	}
	//d1++
	Date operator++(int)  //为了和上面函数构成重载,编译器默认加一个int
	{
		Date tmp(*this);
		*this += 1;
		return tmp;//返回加之前的值
	}
	//d1--
	Date& operator--()
	{
		*this -= 1;
		return *this;
	}
	//--d1
	Date operator--(int)
	{
		Date tmp(*this);
		*this -= 1;
		return tmp;
	}
	//d1 - d2
	int operator-(const Date& d)
	{
		int flag = 1;
		Date max = *this;  //拷贝构造
		Date min = d;
		if (max < min)
		{
			max = d;
			min = *this;
			flag = -1;
		}
		int n = 0;
		while (min != max)
		{
			++min;
			++n;
		}
		return n * flag;
	}
	void print()
	{
		cout << _year << "." << _month << "." << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};

7.const 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值