C++类和对象默认成员函数总结

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

    

2.构造函数

    什么是构造函数?

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

    构造函数的特性

        1、函数名和类名相同

        2、无返回值

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

        4、构造函数可以重载

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

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

        7、编译器生成默认的构造函数会对自定义类型成员调用它的默认成员函数

   默认的构造函数:

        1、没有参数的构造函数:

        

        2、全缺省的构造函数:

        

        3、系统默认的构造函数

3.析构函数

    什么是析构函数?

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

    特性:

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

        2、无参数返回值

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

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

    自定义析构函数:

        

4.拷贝构造函数

    拷贝构造函数:只有单个形参,该形参是对本类类型对象的引用,再用已存在的类类型对象创建新对象时由编译器自动调用

    特性:

        1、拷贝构造函数时构造函的一个重载形式

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

        3、若未显示定义,系统生成默认的拷贝构造函数。默认的拷贝构造函数对象按内存存储按字节序完成拷贝,这种拷贝叫做浅拷贝,或者值拷贝

             浅拷贝指:只是将被拷贝对象的内部资源的拷贝,而没有拷贝析构函数要清理的空间

    自定义的拷贝构造函数:

        

5.赋值运算符重载

    为什么C++引入运算符重载?

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

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

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

    注意:

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

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

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

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

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

    赋值运算符主要有四点:

        1、参数类型

        2、返回值

        3、检测是否自己给自己赋指

        4、返回this指针

        5、一个类如果没有显示定义赋值运算符重载,编译器也会生成一个,完成对象按字节序的值拷贝

    为什么编译器已经给我们完成了值拷贝,为什么还要有赋值运算符重载呢?

        因为默认的赋值运算重载函数只是简单按字节序拷贝,不拷贝新开辟的资源,如果遇到一个析构函数是释放开辟的空间free(),那么在释放第二次的时候拷贝出来的指针所指的内容为空,这就是未定义行为

    自定义的赋值运算符重载函数:

        

6.const成员

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

    注意:const对象不可以调用非const成员函数,非const对象可以调用const成员函数,const成员函数内不可以调用其他非const成员函数,非const成员函数内可以调用其他const成员函数

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

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

8.实现一个Date类

#include <iostream>
#include <stdlib.h>

using namespace std;

class Date
{
public:
	//给变量赋值
	Date(int year = 2019, int month = 3, int day = 15)
	{
		if (year <= 0 || month <= 0 || month > 12
			|| day <= 0 || day > getDay(year, month)){
			cout << "非法日期,已设默认值为:2019-3-15" << endl;
			_year = 2019;
			_month = 3;
			_day = 15;
		}
		_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;
	}

	//获取一年中一月的天数
	int getDay(int year, int month)
	{
		int days[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		int day = days[month - 1];
		if (month == 2){
			//判断该年是否是闰年
			if ((year % 4 == 0 && year % 100 != 0)
				|| (year % 400 == 0))
				day++;
		}
		return day;
	}

	//+=的运算符重载
	Date& operator+=(int day)
	{
		//day小于0,直接用-=运算符重载函数
		if (day < 0)
			return *this -= -day;
		_day += day;
		while (_day > getDay(_year, _month)){
			//减去当月天数
			_day -= getDay(_year, _month);
			++_month;
			if (_month == 13){
				_month = 1;
				++_year;
			}
		}
		return *this;
	}

	//-=运算符重载
	Date& operator-=(int day) {
		if (day > 0)
			return *this += day;
		_day -= day;
		while (_day < 0){
			//要获取这一月的上一月的天数,所以先减减_month
			--_month;
			if (_month == 0){
				--_year;
				_month = 12;
			}
			//加上上一月的天数,再进行判断
			_day += getDay(_year, _month);
		}
		return *this;
	}

	//+运算符重载
	Date operator+(int day) {
		Date ret(*this);
		ret += day;
		return ret;
	}

	//-运算符重载
	Date operator-(int day) {
		Date ret(*this);
		ret -= day;
		return *this;
	}

	//两个日期相减
	int operator-(const Date& d) {
		Date d1(*this);
		Date d2(d);
		int day = 0;
		if (d1 > d2){
			while (d1 > d2){
				--d1;
				++day;
			}
		}else{
			while (d1 < d2){
				++d1;
				--day;
			}
		}
		return day;
	}

	//++date
	Date& operator++(){
		return *this += 1;
	}

	//date--
	Date& operator++(int){
		Date ret(*this);
		*this += 1;
		return ret;
	}

	//--date
	Date& operator--() {
		return *this -= 1;
	}

	//date--
	Date& operator--(int) {
		Date ret(*this);
		*this -= 1;
		return ret;
	}

	//判断两个日期大小
	bool operator>(const Date& d)const{
		return (_year > d._year) ||
			((_year == d._year) && (_month > d._month)) ||
			((_month == d._month) && (_day > d._day));
	}

	bool operator>=(const Date& d)const {
		return (*this > d) || (*this == d);
	}

	bool operator<(const Date& d)const {
		return !(*this >= d);
	}
	bool operator<=(const Date& d)const {
		return !(*this > d);
	}

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

	bool operator!=(const Date& d)const {
		return !(*this == d);
	}

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

int main()
{
	Date d1(2019, 3, 15);
	Date d2(2019, 12, 12);
	d1 += 60;
	d1.display();
	d2 -= 364;
	d2.display();
	cout << (d1 - d2) << endl;
	cout << (d1 > d2) << endl;
	system("pause");
	return 0;
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值