类和对象(中)

类和对象(中)

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

任何一个类在我们不写的情况下,都会自动生成下面六个默认成员函数
在这里插入图片描述

2.构造函数

  • 构造函数是一个特殊的成员函数,名字与类名相同,创建类类型对象时由编译器自动调用,保证每个数据成员
    都有 一个合适的初始值,并且在对象的生命周期内只调用一次。
  • 构造函数是特殊的成员函数,需要注意的是,构造函数的虽然名称叫构造,但是需要注意的是构造函数的主
    要任务并不是开空间创建对象,而是初始化对象。
  • 特征如下:
    1.函数名与类名相同
    2.无返回值
    3.对象实例化时编译器自动调用对应的构造函数
    4.构造函数可以重载。
    5.在对象整个生命周期中只调用一次
  1. 代码测试构造函数:
class Date{
public:
	//1.无参构造函数
	Date(){
	
	}
	//2.带参构造函数
	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(2022,3,24);//调用带参构造函数

	Date d3();//带括号是函数声明,并不是调用

}

int main(){
	TestDate();
	return 0;
}

在这里插入图片描述

编译器用构造函数来初始化对象

2.使用编译器默认生成的构造函数

class Time{
public:
	Time(){
		cout << "Time()" << endl;
		_hour = 0;
		_minute = 0;
		_second = 0;
	}
private:
	int _hour;
	int _minute;
	int _second;
};
class Date{	
private:
	int _year;
	int _month;
	int _day;

	Time _t;
};



int main(){
	Date d;
	return 0;
}

在这里插入图片描述

可见编译器默认生成的构造函数会给对象赋一个随机值

3.创建一个全缺省的构造函数,使得既可以调用有参数的对象,也可以调用无参数的

class Date{
public:

	//2.带参构造函数
	Date(int year=2008, int month=2, int day=27){
		_year = year;
		_month = month;
		_day = day;
	}
private:
	int _year;
	int _month;
	int _day;
};


void TestDate(){
	Date d1;//调用无参构造函数
	Date d2(2022, 3, 24);//调用带参构造函数

	//Date d3();//带括号是函数声明,并不是调用

}

在这里插入图片描述

3.析构函数

概念:

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

特性:

  • 析构函数名是在类名前加上字符“~”
  • 无参数无返回值
  • 一个类只有一个析构函数,若未定义,系统会自动生成默认的析构函数
  • 对象生命周期结束时,系统自动调用析构函数
  • 编译器生成的默认析构函数,会对自定义类型成员调用它的析构函数

代码测试:

class String{
public:
	String(const char*str="jack"){
		_str = (char*)malloc(strlen(str)+1);
		strcpy(_str,str);
	}
	
	~String(){
		cout << "~String"<< endl;
		free(_str);
	}
private:
	char* _str;
};

class Person{
private:
	String _name;
	int _age;
};

int main(){
	Person p;
	return 0;
}

对象在栈上的栈帧开辟的空间会随着函数调用结束而释放掉,但是如果涉及到了资源的申请,比如堆上申请资源,那么就要求程序员自己实现析构函数释放掉。以下就是一个在堆上申请资源的案例:

代码测试:

typedef int DataType;
class SeqList{
public:
	SeqList(int capacity=10){
		_pData = (DataType*)malloc(capacity*sizeof(DataType));
		assert(_pData);
		_size = 0;
		_capacity = capacity;
	}

	~SeqList(){
		if (_pData){
			free(_pData);//释放堆上的空间
			_pData = NULL;//将指针置空
			_capacity = 0;
			_size = 0;
		}
	}
private:
	int* _pData;
	size_t _size;
	size_t _capacity;
};
int main(){
	SeqList seq;
	return 0;
}

在这里插入图片描述
当函数执行完之后,return 0以后才执行析构函数释放堆上的空间
在这里插入图片描述

4.拷贝构造函数

概念;

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

特征:

  • 拷贝构造函数是构造函数的一个重载形式
  • 拷贝构造函数的参数只有一个并且必须使用引用传参,使用传值方式会引发无穷递归调用。
  • 若未显示定义,系统生成默认的拷贝构造函数。 默认的拷贝构造函数对象按内存存储按字节序完成拷贝,这种拷贝我们叫做浅拷贝,或者值拷贝
  • 那么编译器生成的默认拷贝构造函数已经可以完成字节序的值拷贝了,我们还需要自己实现吗?需要程序员实现深拷贝。,原因如下:
class String
{
public:
	String(const char* str = "jack")
	{
		_str = (char*)malloc(strlen(str) + 1);
		strcpy(_str, str);
	}
	~String()
	{
		cout << "~String()" << endl;
		free(_str);
	}
private:
	char* _str;
};
int main()
{
	String s1("hello");
	String s2(s1);
}

在这里插入图片描述

  • 这里会发现下面的程序会崩溃掉,需要深拷贝解决(后续会更新);

5.赋值操作符重载

C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。
函数名字为:关键字operator后面接需要重载的运算符符号。
函数原型:返回值类型 operator操作符(参数列表)

注意:

  • 用于内置类型的操作符,其含义不能改变,例如:内置的整型+,不 能改变其含义
  • 作为类成员的重载函数时,其形参看起来比操作数数目少1,成员函数的操作符有一个默认的形参this,限定为第一个形参
  • . 、:: 、sizeof 、?: 、. 注意以上5个运算符不能重载*。

赋值运算符主要有四点

  • 参数类型:返回值类型 operator=(参数){}
  • 返回值:返回*this
  • 检测是否自己给自己赋值
  • . 一个类如果没有显式定义赋值运算符重载,编译器也会生成一个,完成对象按字节序的值拷贝。但是当使用realloc/malloc等在堆上申请资源时,需要程序员自己编写,否则程序会崩溃掉

代码:

class Date{
public:
	//构造函数
	Date(int year=1900,int month=1,int day=1){
		_year = year;
		_month = month;
		_day= day;
	}

	//bool operator==(Data* this,const Date& d2)
	//左操作数是this指向的调用函数的对象,隐藏
	bool operator==(const Date& d2){
		return _year ==d2._year
		&& _month == d2._month
			&& _day == d2._day;

	}
private:
	int _year;
	int _month;
	int _day;
};

void Test()
{
	Date d1(2018, 9, 26);
	Date d2(2018, 9, 27);
	cout << (d1 == d2) << endl;
}

int main(){
	Test();
	return 0;
}

6.实现一个日期类

直接上代码:

#pragma once

#include <iostream>
#include<windows.h>
using namespace std;

class Date
{
public:
	explicit Date(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;

		// 如果日期非法,将该日期调整成默认的日期
		if (!(year > 0 &&
			(month >= 1 && month < 13) &&
			(day >= 1 && day <= GetDayOfMonth(year, month))))
		{
			_year = 1900;
			_month = 1;
			_day = 1;
		}
	}
	//拷贝构造函数
	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;
	}
	//析构函数
	~Date()
	{}
	//打印
	void Print()const
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}

	///
	// 一般情况下:对于类中私有的成员变量  如果想要在类外访问 都是提供setter和getter的公有方法
	void SetYear(int year)
	{
		_year = year;
	}

	int GetYear()const
	{
		return _year;
	}

	void SetMonth(int month)
	{
		_month = month;
	}

	int GetMonth()const
	{
		return _month;
	}

	void SetDay(int day)
	{
		_day = day;
	}

	int GetDay()const
	{
		return _day;
	}
	//判断闰年
	bool IsLeapYear(int year)const
	{
		if ((0 == year % 4 && 0 != year % 100) ||
			(0 == year % 400))
		{
			return true;
		}

		return false;
	}

	// 100天之后是哪天:给日期 + 具体天数
	Date operator+(int days)const
	{
		if (days < 0)
		{
			return *this - (0 - days);
		}

		Date temp(*this);
		temp._day += days;

		int daysOfMonth = 0;
		while (temp._day >(daysOfMonth = GetDayOfMonth(temp._year, temp._month)))
		{
			temp._day -= daysOfMonth;
			temp._month += 1;

			if (temp._month == 13)
			{
				temp._year += 1;
				temp._month = 1;
			}
		}

		return temp;
	}
	//日期+=天数
	Date& operator+=(int days)
	{
		*this = *this + days;
		return *this;
	}

	// 100天之前是哪天:给日期 + 具体天数
	Date operator-(int days)const
	{
		if (days < 0)
		{
			return *this + (0 - days);
		}

		Date temp(*this);
		temp._day -= days;

		while (temp._day <= 0)
		{
			temp._month -= 1;
			if (temp._month == 0)
			{
				temp._year -= 1;
				temp._month = 12;
			}
			temp._day += GetDayOfMonth(temp._year, temp._month);
		}

		return temp;
	}

	// 日期-=天数
	Date& operator-=(int days)
	{
		//*this = *this - days;
		return *this;
	}

	// 还要到少天放五一假
	size_t operator-(const Date& d)
	{
		Date minDate(*this);
		Date maxDate(d);
		if (maxDate < minDate)
		{
			minDate = d;
			maxDate = *this;
		}

		size_t gap = 0;
		while (minDate != maxDate)
		{
			++minDate;
			gap++;
		}

		return gap;
	}
	//前置++
	Date& operator++()
	{
		*this = *this + 1;
		return *this;
	}
	//后置++
	Date operator++(int)
	{
		Date temp(*this);
		*this = *this + 1;
		return temp;
	}
	//前置--
	Date& operator--()
	{
		*this = *this - 1;
		return *this;
	}
	//后置--
	Date operator--(int)
	{
		Date temp(*this);
		*this = *this - 1;
		return temp;
	}
	// ==运算符重载
	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);
	}
	// <运算符重载
	bool operator<(const Date& d)const
	{
		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._month)
			return true;
		else
			return false;
	}
private:
	int GetDayOfMonth(int year, int month)const
	{
		int days[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

		// 要考虑闰年
		if (IsLeapYear(year))
			days[2] += 1;

		return days[month];
	}
private:
	int _year;
	int _month;
	int _day;
};


int main()
{
	int a = 10;
	int b = 20;
	int c = a + b;
	a += b;

	Date d1(2022, 3, 20);
	// d1._day = 22;
	d1.SetDay(20);
	d1.Print();

	// cout << d1._year << endl;
	cout << d1.GetYear() << endl;

	Date d2(2022, 3, -10);
	d2.Print();

	d1.Print();
	d2 = d1 + 5;
	d1.Print();
	d2.Print();

	d2 = d1 + 100;
	d2.Print();

	Date d3(2022, 12, 20);
	d2 = d3 + 100;
	d2.Print();

	d2 = d3 + -100;
	d2.Print();

	d2 = d1 - 100;
	d2.Print();

	Date d4(2022, 5, 1);
	cout << d4 - d1 << endl;
	system("pause");
	return 0;
}

7.const成员

  • 将const修饰的类成员函数称之为const成员函数,const修饰类成员函数,实际修饰该成员函数隐含的this
    指针,表明在该成员函数中不能对类的任何成员进行修改。
    在这里插入图片描述
  • 26
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 19
    评论
评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值