类和对象(中)

一:类的6个默认成员函数

二:构造函数

1.概念

观察下述代码:

每创建一个新的 Date 类的对象都需要调用 DateInfo 函数,在创建对象个数少的时候没有什么问题,但当需要创建很多个对象的时候会很麻烦,所以C++规定我们可以在类里面写一个构造成员函数 ,不需要每次调用都初始化。

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

2.特性

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

构造函数特征如下:
<1>. 函数名与类名相同。
<2>. 无返回值。
<3>. 对象实例化时编译器自动调用对应的构造函数。
<4>. 构造函数可以重载。

注意:如果通过无参构造函数创建对象时,对象后面不用跟括号,否则就成了函数声明

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

 类中有显式定义构造函数:

<6>.C++把类型分成内置类型(基本类型)和自定义类型。内置类型就是语言提供的数据类型,如:int/char/指针类型...,自定义类型就是我们使用class/struct/union等自己定义的类型,看看下面的程序,就会发现编译器生成默认的构造函数会对自定类型成员_t调用的它的默认构造函数。

#include<iostream>
using namespace std;
class Time
{
public:
	Time()
	{
		cout << "Time()" << endl;
		_hour = 0;
		_minute = 0;
		_second = 0;
	}
private:
	int _hour;
	int _minute;
	int _second;
};
class Date
{
public:
	//void Print()
	//{
	//	cout << _year <<" " << _month << " " << _day << endl;
	//}
private:
	//内置类型
	int _year;
	int _month;
	int _day;

	// 自定义类型
	Time _t;
};
int main()
{
	Date d;
	//d.Print();

	return 0;
}

程序运行结果为:

 C++11,声明给缺省值,默认使用缺省值:

总结:

构造函数,也是默认成员函数,我们不写,编译器会自动生成。

编译器生成的默认构造函数的特点:

<1>: 我们不写才会生成,我们写了就不会生成(任何一个)

<2>: 内置类型的成员不会处理(有些编译器会处理,但很少)

<3>: 自定义类型的成员才会处理,回去调用这个成员的(默认)构造函数

一般情况下,都需要我们自己写构造函数,决定初始化的方式。

特殊情况:成员变量全是自定义类型,可以考虑不写构造函数。

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

三:析构函数

1.概念

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

2.特性



析构函数特征如下:
<1>. 析构函数名是在类名前加上字符 ~。
<2>. 无参数无返回值类型。
<3>. 一个类只能有一个析构函数。若未显式定义,系统会自动生成默认的析构函数。注意:析构
函数不能重载
<4>. 对象生命周期结束时,C++编译系统系统自动调用析构函数
下面是一个用C++实现的栈:
#include<iostream>
#include<assert.h>
using namespace std;
typedef int DateType;
class Stack
{
public:
	Stack(size_t capacity = 4)//栈初始化
	{
		if (capacity == 0)
		{
			_a = nullptr;
			_size = _capacity = 0;
		}
		else
		{
			_a = (DateType*)malloc(sizeof(DateType) * capacity);
			if (_a == nullptr)
			{
				perror(" malloc fail ");
				return;
			}
			_capacity = capacity;
			_size = 0;
		}
	}
	void Push(DateType x)//入栈
	{
		if (_size == _capacity)//扩容
		{
			size_t newcapacity = _capacity == 0 ? 4 : _capacity * 2;
			DateType* tmp = (DateType*)realloc(_a, sizeof(DateType) * newcapacity);
			if (tmp == nullptr)
			{
				perror(" realloc fail ");
				return;
			}
			if (tmp == _a)
			{
				cout << _capacity << "原地扩容" << endl;
			}
			else
			{
				cout << _capacity << "异地扩容" << endl;
			}
			_a = tmp;
			_capacity = newcapacity;
		}
		_a[_size] = x;
		_size++;
	}
	int Top()//栈顶元素
	{
		return _a[_size - 1];
	}
	void Pop()//出栈
	{
		assert(_size > 0);
		_size--;
	}
	bool StackEmpty()//判断栈内是否为空
	{
		return _size == 0;
	}
	~Stack()//析构
	{
		cout << " ~Stack() " << endl;
		if (_a)
		{
			free(_a);
			_a = nullptr;
			_size = _capacity = 0;
		}
	}

private:
	DateType* _a;
	int _size;
	int _capacity;
};

测试用例及其运行结果:

 我们发现,程序自动调用了析构函数。

<5>.编译器生成的默认析构函数,对自定类型成员调用它的析构函数。

在main方法中根本没有直接创建Time类的对象,为什么最后会调用Time类的析构函数?
原因:在main函数中创建了Date对象d,而d中包含4个成员变量,其中_year, _month, _day三个是
内置类型成员,销毁时不需要资源清理,最后系统直接将其内存回收即可;而_t是Time类对象,所以在d销毁时,要将其内部包含的Time类的_t对象销毁,所以要调用Time类的析构函数。但是main函数中不能直接调用Time类的析构函数,实际要释放的是Date类对象,所以编译器会调用Date类的析构函数,而Date没有显式提供,则编译器会给Date类生成一个默认的析构函数,目的是在其内部调用Time类的析构函数,即当Date对象销毁时,要保证其内部每个自定义对象都可以正确销毁main函数中并没有直接调用Time类析构函数,而是显式调用编译器为Date类生成的默认析构函数。
注意:创建哪个类的对象则调用该类的析构函数,销毁那个类的对象则调用该类的析构函数
 

内置类型的成员,不做处理

自定义类型的成员,会去调用它的析构

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

四: 拷贝构造函数

1.概念

在现实生活中存在长得一摸一样的双胞胎,那没在我们创建对象时,可否创建一个与已存在对象一某一样的新对象呢?

解决方法:

引用,别名改变会影响原序列改变。

拷贝构造函数概念:

只有单个形参,该形参是对本类类型对象的引用(一般常用const修饰),在用已存

在的类类型对象创建新对象时由编译器自动调用。
  
一个对象不会析构两次。(后续进行解释原因)

2.特性


<1>. 拷贝构造函数是构造函数的一个重载形式。
<2>. 拷贝构造函数的参数只有一个且必须是类类型对象的引用,使用传值方式编译器直接报错,
因为会引发无穷递归调用。
#include<iostream>
using namespace std;
class Date
{
public:
	Date(int year = 2023, int month = 8, int day = 16)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	void Print()
	{
		cout << _year <<" " << _month << " " << _day << endl;
	}
	Date(const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
private:
	//内置类型
	int _year = 2023;
	int _month = 8;
	int _day = 16;
};
int main()
{
	Date d;
	d.Print();
	Date d1(d);
	d1.Print();

	return 0;
}

<3>. 若未显式定义,编译器会生成默认的拷贝构造函数。
默认的拷贝构造函数对象按内存存储按字节序完成拷贝,这种拷贝叫做浅拷贝,或者值拷贝。
class Time
{
public:
	Time(int hour = 0, int minute = 0,int second = 0)//构造函数 --- 初始化对象
	{
		_hour = hour;
		_minute = minute;
		_second = second;
	}
	Time(const Time& t)
	{
		cout << " Time(const Time& t) " << endl;
		_hour = t._hour;
		_minute = t._minute;
		_second = t._second;
	}
	~Time()
	{
		cout << "~Time()" << endl;
	}
private:
	int _hour;
	int _minute;
	int _second;
};

 我们不写,编译器默认生成拷贝构造函数跟之前的构造函数特性不一样:

*1.内置类型,值拷贝

*2.自定义类型,调用它的拷贝构造函数

Date 不需要我们实现拷贝构造,默认生成的就可以使用

Stack 需要我们自己实现深拷贝的拷贝构造,默认生成的会出问题。

Stack示例

*调用默认生成的构造函数:

 我们发现程序运行出错,接下来我们对程序进行调试,逐步观察:

创建对象 s ,入栈,都没有问题。接下来使用系统的默认的拷贝构造函数,将 s 拷贝给 s1 : 

我们可以看到,拷贝过程没有问题,接下来,调用在程序结束前,默认调用的析构函数(在此处相当栈的销毁 --- Destory 函数): 

 第一次调用析构函数

 没有问题,对象s1成功销毁,空间被释放。  第二次调用析构函数

     我们发现,在释放 _a 空间的时候,程序报错 --- 原因:s 和 s1 指向同一空间,在调用 s1 的析构函数时,已经将 _a 的空间释放掉了,而在调用 s 的析构函数的时候,再一次的对 _a 空间进行了释放。一块内存空间进行多次释放,必然造成程序的崩溃。

 *调用自己写的拷贝构造函数:

#include<iostream>
#include<assert.h>
#include<stdlib.h>
using namespace std;
typedef int DateType;
class Stack
{
public:
	Stack(size_t capacity = 4)//栈初始化
	{
		if (capacity == 0)
		{
			_a = nullptr;
			_size = _capacity = 0;
		}
		else
		{
			_a = (DateType*)malloc(sizeof(DateType) * capacity);
			if (_a == nullptr)
			{
				perror(" malloc fail ");
				return;
			}
			_capacity = capacity;
			_size = 0;
		}
	}
	void Push(DateType x)//入栈
	{
		if (_size == _capacity)//扩容
		{
			size_t newcapacity = _capacity == 0 ? 4 : _capacity * 2;
			DateType* tmp = (DateType*)realloc(_a, sizeof(DateType) * newcapacity);
			if (tmp == nullptr)
			{
				perror(" realloc fail ");
				return;
			}
			if (tmp == _a)
			{
				cout << _capacity << "原地扩容" << endl;
			}
			else
			{
				cout << _capacity << "异地扩容" << endl;
			}
			_a = tmp;
			_capacity = newcapacity;
		}
		_a[_size] = x;
		_size++;
	}
	int Top()//栈顶元素
	{
		return _a[_size - 1];
	}
	void Pop()//出栈
	{
		assert(_size > 0);
		_size--;
	}
	bool StackEmpty()//判断栈内是否为空
	{
		return _size == 0;
	}
	~Stack()//析构
	{
		cout << " ~Stack() " << endl;
		if (_a)
		{
			free(_a);
			_a = nullptr;
			_size = _capacity = 0;
		}
	}
	Stack(const Stack& s)
	{
		cout << " Stack(const Stack& s) " << endl;
		_a = (DateType*)malloc(sizeof(DateType) * s._capacity);
		if (_a == nullptr)
		{
			perror("Stack malloc fail");
			return;
		}
		memcpy(_a, s._a, sizeof(DateType) * s._size);
		_size = s._size;
		_capacity = s._capacity;
	}
private:
	DateType* _a;
	int _size;
	int _capacity;
};
int main()
{
	Stack s;
	Stack s1(s);
	return 0;
}

 注意:类中如果没有涉及资源申请时,拷贝构造函数是否写都可以;一旦涉及到资源申请

时,则拷贝构造函数是一定要写的,否则就是浅拷贝。
<4>. 拷贝构造函数典型调用场景:
        *1.使用已存在对象创建新对象
        *2.函数参数类型为类类型对象
        *3.函数返回值类型为类类型对象

五:赋值运算符重载

1.运算符重载

C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。
函数名字为:关键字operator后面接需要重载的运算符符号。
函数原型:返回值类型 operator操作符(参数列表)
  
  注意:
不能通过连接其他符号来创建新的操作符:比如operator@
重载操作符必须有一个类类型参数
用于内置类型的运算符,其含义不能改变,例如:内置的整型+,不 能改变其含义
作为类成员函数重载时,其形参看起来比操作数数目少1,因为成员函数的第一个参数为隐
藏的this
.* :: sizeof ?: . 注意以上5个运算符不能重载。 
不能改变操作数的操作数个数,一个操作符十几个操作数,那么重载的时候九有几个参数

示例:日期类比较大小

在此处为了便于观察,我们将其分成 .h .c 文件


判断一个日期是否小于另一个日期 ,即重载运算符 < 

// <运算符重载
bool Date::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;
	}
	else
	{
		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 || *this == d;
}


 将其写在同一个文件中为(可以修改测试用例):

#include<iostream>
using namespace std;
class Date
{
public:
	// 获取某年某月的天数
	int GetMonthDay(int year, int month);
	// 全缺省的构造函数
	Date(int year = 1900, int month = 1, int day = 1);
	// <运算符重载
	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();
private:
	int _year;
	int _month;
	int _day;
};
//获取某年某月有多少天
int Date::GetMonthDay(int year, int month)
{
	int arr[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	//              0  1  2  3  4  5  6  7  8  9 10 11 12 
	if ((month == 2) && ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)))
	{
		arr[2] = 29;
	}
	return arr[month];
}

// 全缺省的构造函数
// ??? 把后面的参数去掉了?
Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
	//检查日期的合法性
	if (month < 1 || month > 12 || day < 0 || day > GetMonthDay(year, month))
	{
		cout << "日期不合法" << endl;
	}
}
//析构函数
//对象在销毁时会自动调用的函数析构函数,完成对象中资源的清理工作。
Date::~Date()
{
	cout << "Date::~Date()" << endl;
}
// <运算符重载
bool Date::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;
	}
	else
	{
		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 || *this == d;
}
// !=运算符重载
bool Date::operator != (const Date& d)
{
	return !(*this == d);
}
void test1()
{
	Date d1(2023, 10, 1);
	Date d2(2023, 8, 16);

	cout << (d1 < d2) << endl;
}
int main()
{
	test1();
	return 0;
}

2.赋值运算符重载

<1>. 赋值运算符重载格式

参数类型:const T&,传递引用可以提高传参效率
返回值类型:T&,返回引用可以提高返回的效率,有返回值目的是为了支持连续赋值
检测是否自己给自己赋值
返回*this :要复合连续赋值的含义
#include<iostream>
using namespace std;
class Date
{
public:
	Date(int year = 2023, int month = 8, int day = 16)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	void Print()
	{
		cout << _year <<" " << _month << " " << _day << endl;
	}
	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 = 2023;
	int _month = 8;
	int _day = 16;
};

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

我们将赋值运算符从类中拿到全局里面: 

 原因:赋值运算符如果不显式实现,编译器会生成一个默认的。此时用户再在类外自己实现

一个全局的赋值运算符重载,就和编译器在类中生成的默认赋值运算符重载冲突了,故赋值
运算符重载只能是类的成员函数。

<3>. 用户没有显式实现时,编译器会生成一个默认赋值运算符重载,以值的方式逐字节拷贝。

       内置类型成员变量是直接赋值的,而自定义类型成员变量需要调用对应类的赋值运算符
重载完成赋值。
#include<iostream>
using namespace std;
class Time
{
public:
	Time(int hour = 0, int minute = 0, int second = 0)
	{
		_hour = hour;
		_minute = minute;
		_second = second;
	}
	Time& operator=(const Time& t)
	{
		cout << " Time& operator=(const Time& t) " << endl;
		if (this != &t)
		{
			_hour = t._hour;
			_minute = t._minute;
			_second = t._second;
		}
		return *this;
	}
private:
	int _hour;
	int _minute;
	int _second;
};
class Date
{
private:
	//内置类型
	int _year = 1970;
	int _month = 1;
	int _day = 1;
	// 自定义类型
	Time _t;
};

既然编译器生成的默认赋值运算符重载函数已经可以完成字节序的值拷贝了,还需要自己实

现吗?例如Stack 类?(使用拷贝构造中的 Stack 示例)

 

总结:如果类中未涉及到资源管理,赋值运算符是否实现都可以;一旦涉及到资源管理则必
须要实现。

 

 <4>.前(后)置++,前(后)置-- 重载

日期类:在写前(后)置++ 或前(后)置-- 时,我们需要得出 日期加天数 和 日期减天数

而日期加天数又分为原来的数据不变,和原来的数据改变两种状态,我们可以将其看作 加法 和 加等两种状态。


 在此处,我们可以选择优先实现 += ,在实现日期加天数的时候,复用 +=

实现思路,日期加天数可能产生进位,此时我们需要知道每年每月对应 的天数,在此处我们可以写一个  GetMonthDay 的函数 --- 用于获取对应的天数。

	// 获取某年某月的天数
	int GetMonthDay(int year, int month)
	{
		int arr[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
		//              0  1  2  3  4  5  6  7  8  9 10 11 12 
		if ((month == 2) && ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)))
		{
			arr[2] = 29;
		}
		return arr[month];
	}

 然后计算 日期+天数 为多少天,判断其是否大于对应年月的天数,如果小于可以直接返回日期(此时对应的天已经修改完成);如果大于,则用 加好后的天数 - 对应年月的天数 ,然后需要将月份 +1,判断月份是否等于13,若月份等于13,则需要将年份 +1 。具体代码如下:

	// 日期+=天数
	Date& operator+=(int day)
		// 日期+=天数
        //自己本身改变
	{
		//考虑加上的值为负数
		if (day < 0)
		{
			return *this -= (-day);
		}
		_day += day;
		/*int tmp = _day + day;*/ // 错误的 _day 的数值没有改变
		while (_day > GetMonthDay(_year, _month))
		{
			_day -= GetMonthDay(_year, _month);
			_month++;
			if (_month == 13)
			{
				_year++;
				//需要将月份重置
				_month = 1;
			}
		}
		return *this;
	}

在此处,我们可以先屏蔽掉 加上一个负数部分的代码,因为我们还没有写 -=。

输入测试用例:

 检验此处结果是否正确:

我们发现结果无误。 


日期 + 天数

在此种情况下,原来的日期并没有改变,我们可以复用 +=

原来的日期没有改变,需要提前记录下 *this ,修改记录的值,不影响 *this 的值。

代码为:

	// 日期+天数
    //自己本身没有改变,需要提前记录该值
	Date operator+(int day)
	{
		Date tmp(*this);
		tmp += day;// * 本身为*this ,改变tmp的值,*this的值不改变
		return tmp;
	}

输入测试用例:


-= 和 - 在此处不做详细介绍,与上述类似,需要注意的是:在计算 -= 的时候,可能存在需要向前借位的情况,此时需要先计算好年月。
代码如下(可作为参考) :

 前(后)置++,前(后)置-- 重载

*前置++和后置++都是一元运算符,为了让前置++与后置++形成能正确重载。
*C++规定:后置++重载时多增加一个int类型的参数,但调用函数时该参数不用传递,编译器自动传递 。​​​​​​​
*注意:后置++是先使用后+1,因此需要返回+1之前的旧值,故需在实现时需要先将this保存
一份,然后给this+1
    // 前置++
	Date& operator++()
	{
		*this += 1;
		return *this;//出作用域,this 还在 &
	}
	// 后置++
	Date operator++(int)
	{
		Date tmp(*this);
		*this += 1;
		return tmp;//出作用域,tmp 不在 无需&
	}
	// 后置--
	Date operator--(int)
	{
		Date tmp(*this);
		*this -= 1;
		return tmp;
	}
	// 前置--
	Date& operator--()
	{
		*this -= 1;
		return *this;
	}

输入测试用例:


六:const 成员 

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

 权限可以缩小,可以平移,但不能放大!!!

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

非const成员函数内可以调用其它的const成员函数

​​​​​​​反之,不可以。

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

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

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值