既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
_day = day;
}
private:
int _year;
int _month;
int _day;
};
// 以下测试函数能通过编译吗?
void Test()
{
Date d1;
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/a45a030c4e874ed89fa221ec69d06334.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA6Zuq6IqZ6Iqx,size_17,color_FFFFFF,t_70,g_se,x_16)
>
> 编译器不能明确知道该调用哪一个,所以报错了
>
>
>
## 3.析构函数
* 概念
>
> 析构函数不是完成对象的销毁,局部对象销毁工作是由编译器完成的,而对象在销毁时会自动调用析构函数,完成类的一些资源清理工作(释放动态开辟的空间)
>
>
>
* 特性:
1. 析构函数名是在类名前加上字符 ~
2. 无参数无返回值
3. 一个类有且只有一个析构函数,若未显式定义,系统会自动生成默认的析构函数(不能重载)
4. 对象生命周期结束时,C++编译系统系统自动调用析构函数 (在C语言上的优化)
* 例:
typedef int DataType;
class SeqList
{
public:
SeqList(int capacity = 10)//构造函数
{
_pData = (DataType*)malloc(capacity * sizeof(DataType));
assert(_pData);
_size = 0;
_capacity = capacity;
}
~SeqList()//析构函数
{
cout << “~SeqList()” << endl;
if (_pData)
{
free(_pData); // 释放堆上的空间
_pData = NULL; // 将指针置为空
_capacity = 0;
_size = 0;
}
}
private:
int* _pData;
size_t _size;
size_t _capacity;
};
int main()
{
SeqList sl;
return 0;
}
## 4.拷贝构造函数
* 概念:
>
> 构造函数:只有单个形参,该形参是对本类类型对象的引用(一般常用const修饰),在用已存在的类类型对象
> 创建新对象时由编译器自动调用
>
>
>
* 特征
1. 拷贝构造函数是构造函数的一个重载形式(参数不同)
2. 拷贝构造函数的参数只有一个且必须使用引用传参,使用传值方式会引发无穷递归调用(传值也是一种拷贝,会继续调用拷贝函数)
* 例:
class Date
{
public:
Date(int year = 1900, 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;//调用构造函数
Date d2(d1);//调用拷贝构造函数
return 0;
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/6ab5038a662a478cb369a35eb52d9d48.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA6Zuq6IqZ6Iqx,size_20,color_FFFFFF,t_70,g_se,x_16)
3. 若未显示定义,系统生成默认的拷贝构造函数。 默认的拷贝构造函数对象按内存存储按字节序完成拷贝,这种拷贝我们叫做浅拷贝,或者值拷贝
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1;
// 这里d2调用的默认拷贝构造完成拷贝,d2和d1的值也是一样的。
Date d2(d1);
return 0;
}
4. 编译器生成的默认拷贝构造函数已经可以完成字节序的值拷贝了,对于像日期类这样的类(只有指针类型的类)是没必要的,编译器生成的足够使用了,而对于存在指针类型的成员函数则会存现问题
class String
{
public:
String(const char* str = “xxxx”)
{
_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);
}
>
> s1和s2中的 str 指向的是同一份空间 ,析构时会被 释放 两次 ,所以会报错
>
>
>
## 5.赋值运算符重载
### 运算符重载
>
> C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。
>
>
>
* 使用方法:
1. 函数名字为:关键字operator后面接需要重载的运算符符号。
2. 函数原型:返回值类型 operator操作符(参数列表)
* 注意:
1. 不能通过连接其他符号来创建新的操作符,如@等
2. 重载操作符必须有一个类类型或者枚举类型的操作数
3. .\* 、:: 、sizeof 、?: 、. 注意以上5个运算符不能重载
4. 如果重载函数在类外,想要访问类里的成员变量,有两种方式
* 例:
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
// bool operator==(Date* 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;
}
### 赋值运算符重载
* 例:
class Date
{
public:
Date(int year = 1900, 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;
}
Date& operator=(const Date& d)
{
if (this != &d)//避免对象赋值自己
{
_year = d._year;
_month = d._month;
_day = d._day;
}
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1(2022,2,1);//调用构造函数
Date d2;
d2=d1;//调用赋值操作符重载
return 0;
}
* 赋值运算符主要有四点:
1. 参数类型
2. 返回值
3. 检测是否自己给自己赋值
4. 返回\*this
5. 一个类如果没有显式定义赋值运算符重载,编译器也会生成一个,完成对象按字节序的值拷贝
## 6.日期类的实现
>
> 通过上面的知识,我们可以写出日期类的实现了
>
>
>
* 接口的展示
class Date
{
public:
// 获取某年某月的天数
int GetMonthDay(int year, int month);
// 全缺省的构造函数
Date(int year = 1900, int month = 1, int day = 1);
// 拷贝构造函数
// d2(d1)
Date(const Date& d);
// 赋值运算符重载
// d2 = d3 -> d2.operator=(&d2, d3)
Date& operator=(const Date& d);
// 析构函数
~Date();
// 日期+=天数
Date& operator+=(int day);
// 日期+天数
Date operator+(int day);
// 日期-天数
Date operator-(int day);
// 日期-=天数
Date & operator-=(int day);
// 前置++
Date& operator++();
// 后置++
Date operator++(int);
// 后置–
Date operator–(int);
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
rator++();
// 后置++
Date operator++(int);
// 后置–
Date operator–(int);
[外链图片转存中…(img-YyinoS7h-1715622249069)]
[外链图片转存中…(img-gesH3P3K-1715622249069)]
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!