意为用户没有显示实现,但是编译器会自动生成的成员函数被称为默认成员函数。一个类,我们不写的情况下编译器会默认生成以下6个默认成员函数。 6个默认成员函数:初始化和清理(构造函数完成初始化工作,析构函数完成清理工作);拷贝复制(拷贝构造是使用同类对象初始化创建对象,赋值重载主要是把一个对象赋值给另一个对象);取地址重载(主要是普通对象和const对象取地址,这两个很少自己实现)
构造函数的主要内容不是开空间创建对象,而是对象实例化时初始化对象。构造函数的本质是要替代Stack和Date类中的Init函数的功能。构造函数自动调用的特点就完美替代了Init.
C++类型分为内置类型(基本类型)和自定义类型。内置类型就是原生数据类型,如:int/char/double/指针等,自定义类型就是我们使用class/struct等关键字自己定义的类型。
#include<iostream>
using namespace std;
class Date
{
public:
//1.无参构造函数
//Date()
//{
// _year = 1;
// _month = 1;
// _day = 1;
//}
////2.带参构造函数
//Date(int year, int month, int day)
//{
// _year = year;
// _month = month;
// _day = day;
//}
//3.全缺省构造参数
Date(int year = 1,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 main()
{
Date d1;//对象实例化的时候系统会自动调用对应的构造函数
d1.Print();1/1/1
Date d2(2025,9,20);//2025/9/20
d2.Print();
return 0;
}
#include<iostream>
using namespace std;
typedef int STDataType;
class Stack
{
public:
Stack(int n = 4)
{
_a = (STDataType*)malloc(sizeof(STDataType) * n);
if (nullptr==_a)
{
perror("malloc fail");
return;
}
_capacity = n;
_top = 0;
}
private:
STDataType* _a;
size_t _capacity;
size_t _top;
};
//两个Stack实现队列
class MyQueue
{
public:
//编译器默认生成的MyQueue的构造函数调用实现了Stack的构造,完成了两个成员的初始化
private:
Stack pushst;
Stack popst;
};
int main()
{
MyQueue mq;
return 0;
}
C++规定对象在销毁时会自动调用析构函数,完成对象中资源的清理释放工作。析构函数的功能类比我们之前的Stack实现的Destroy功能,而像Date没有Destroy,其实就是没有资源需要释放,所以严格说Date不需要析构函数。
#include<iostream>
using namespace std;
typedef int STDataType;
class Stack
{
public:
Stack(int n = 4)
{
_a = (STDataType*)malloc(sizeof(STDataType) * n);
if (nullptr==_a)
{
perror("malloc fail");
return;
}
_capacity = n;
_top = 0;
}
~Stack()//这一段不可省略,否则会造成内存泄漏
{
cout << "~Stack()" << endl;//cout是C++向标准输出设备(默认是控制台)输出内容的工具,endl用于换行并强制刷新输出缓冲区
free(_a);
_a = nullptr;
_top = _capacity=0;
}
private:
STDataType* _a;
size_t _capacity;
size_t _top;
};
//两个Stack实现队列
class MyQueue
{
public:
//编译器默认生成的MyQueue的析构函数调用实现了Stack的析构,释放的Stack内部的资源
private:
Stack pushst;//
Stack popst;//当main函数执行完毕,mq对象被销毁,内部的pushst,popst会被销毁,从而触发他们的析构函数,
//执行cout语句,在对象销毁中被执行,因此会打印出两个~Stcak()
};
int main()
{
MyQueue mq;
return 0;
}
一个构造函数的第一个参数是自身类类型的引用,且任何额外的参数都有默认值,则此构造函数也叫做拷贝构造函数,也就是说拷贝构造是一个特殊的构造函数。
#include<iostream>
using namespace std;
class Date
{
public:
Date(int year = 1, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
//Date(Date d)//这样写不对,“Date”: 非法的复制构造函数: 第一个参数不应是“Date”
//这是一种拷贝传参方式:第一种
/*Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}*/
//这是一种拷贝传参方式:第二种
Date(Date* d)
{
_year = d->_year;
_month = d->_month;
_day = d->_day;
}
void Print()
{
cout << _year << "/" << _month << "/" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
void Func1(Date d)
{
cout << &d << endl;//这里是取地址//2025/9/21
d.Print();
}
int main()
{
Date d1(2025, 9, 21);
//d1.Print();
Func1(d1);
cout << &d1 << endl;
//这样写就是拷贝构造,通过同类型的对象初始化构造,而不是指针
Date d2(d1);//拷贝构造一个对象来初始化
d2.Print();//2025/9/21
//也可以这样写,也是拷贝构造
Date d4 = d1;
d1.Print();//2025/9/21
return 0;
}
注意:析构函数和构造函数对内置类型不做处理;值拷贝是值是多少就拷贝多少,一个字节一个字节的拷贝,也就是浅拷贝)深拷贝:不仅拷贝值,还拷贝资源。
往期回顾: C++篇(1) 万能工具怎么用?