一.构造函数
1.构造函数是一个特殊的成员函数,名字与类名相同,创建类类型对象时由编译器自动调用,以保证每个数据成 员都有 一个合适的初始值,并且在对象整个生命周期内只调用一次
2.作用:初始化对象。
3.特性:
(1). 函数名与类名相同。
(2). 无返回值。
(3). 对象实例化时编译器自动调用对应的构造函数。
(4). 构造函数可以重载。
- #include
- using namespace std;
- class lin
- {
- public:
- // 1.无参构造函数
- lin()
- {
- cout << "lin1" << endl;
- }
- // 2.带参构造函数
- lin(int year, int month, int day)
- {
- _year = year;
- _month = month;
- _day = day;
- cout << "lin2" << endl;
- }
- private:
- int _year;
- int _month;
- int _day;
- };
- void Testlin()
- {
- lin d1; // 调用无参构造函数
- lin d2(2022, 8, 10); // 调用带参的构造函数
-
- lin d3();//如果通过无参构造函数创建对象时,对象后面不用跟括号,否则就成了函数声明
- }
- int main()
- {
- Testlin();
- return 0;
- }

4. 如果类中没有显式定义构造函数,则C++编译器会自动生成一个无参的默认构造函数,一旦用户显式定 义编译器将不再生成。
- #include
- using namespace std;
- class lin
- {
- public:
- /*
- // 如果用户显式定义了构造函数,编译器将不再生成
- lin(int year, int month, int day)
- {
- _year = year;
- _month = month;
- _day = day;
- }
- */
-
- void Print()
- {
- cout << _year << "-" << _month << "-" << _day << endl;
- }
- private:
- int _year;
- int _month;
- int _day;
- };
- int main()
- {
- // 将lin类中构造函数屏蔽后,代码可以通过编译,因为编译器生成了一个无参的默认构造函数
- // 将lin类中构造函数放开,代码编译失败,因为一旦显式定义任何构造函数,编译器将不再生成
- lin d;
- //lin d1(1);// 没有合适的默认构造函数可用
- d.Print();
- return 0;
- }
![]()
![]()
5.:C++11 中针对自定义类型初始化,内置类型成员不初始化的缺陷,又打了补丁,即:内置类型成员变量在类中声明时 可以给默认值。
6.无参的构造函数和全缺省的构造函数都称为默认构造函数,并且默认构造函数只能有一个。注意:无参 构造函数、全缺省构造函数、我们没写编译器默认生成的构造函数,都可以认为是默认构造函数。
- #include
- using namespace std;
- class lin
- {
- public:
- /*lin()
- {
- _year = 2022;
- _month = 8;
- _day = 10;
- cout << "lin1 " << _year <<"/" << month<<"/" << day << endl;
- }*/
- lin(int year = 2022, int month = 8, int day = 10)
- {
- _year = year;
- _month = month;
- _day = day;
- cout << "lin2 " << _year <<"/" << month<<"/" << day << endl;
- }
- private:
- int _year;
- int _month;
- int _day;
- };
- int main()
- {
- lin d;
- return 0;
- }
二.析构函数
1.概念:与构造函数功能相反,析构函数不是完成对对象本身的销毁,局部对象销毁工作是由编译器完成 的。而对象在销毁时会自动调用析构函数,完成对象中资源的清理工作。
2.特性
(1). 析构函数名是在类名前加上字符 ~。
(2). 无参数无返回值类型。
(3). 一个类只能有一个析构函数。若未显式定义,系统会自动生成默认的析构函数。
(4)析构函数不能重载
(5). 对象生命周期结束时,C++编译系统系统自动调用析构函数。
- #include
- using namespace std;
- typedef int DataType;
- class Stack
- {
- public:
- Stack(size_t capacity = 3)
- {
- _a = (DataType*)malloc(sizeof(DataType) * capacity);
- if (NULL == _a)
- {
- perror("malloc申请空间失败:");
- return;
- }
- _capacity = capacity;
- _size = 0;
- }
- void Push(DataType data)
- {
- _a[_size] = data;
- _size++;
- }
- ~Stack()
- {
- if (_a)
- {
- free(_a);
- _a = NULL;
- _capacity = 0;
- _size = 0;
- }
- cout << "~Stack()" << endl;
- }
- private:
- DataType* _a;
- int _capacity;
- int _size;
- };
- int main()
- {
- Stack s;
- s.Push(1);
- s.Push(2);
- }

三.拷贝构造函数
1.概念:只有单个形参,该形参是对本类类型对象的引用(一般常用const修饰),在用已存在的类类型 对象创建新对象时由编译器自动调用
2.特性:
(1). 拷贝构造函数是构造函数的一个重载形式。
(2). 拷贝构造函数的参数只有一个且必须是类类型对象的引用,使用传值方式编译器直接报错,因为会引发 无穷递归调用。
- #include
- using namespace std;
- class lin
- {
- public:
- lin(int year = 2022, int month = 8, int day = 10)
- {
- _year = year;
- _month = month;
- _day = day;
- }
- // lin(const lin d) // 错误写法:编译报错,会引发无穷递归
- lin(const lin& d) // 正确写法
- {
- _year = d._year;
- _month = d._month;
- _day = d._day;
- }
- private:
- int _year;
- int _month;
- int _day;
- };
- int main()
- {
- lin d1;
- lin d2(d1);
- return 0;
- }
3.若未显式定义,编译器会生成默认的拷贝构造函数。 默认的拷贝构造函数对象按内存存储按字节序完成拷贝,这种拷贝叫做浅拷贝,或者值拷贝,内置类型是按照字节方式直接拷贝的,而自定义类型是调 用其拷贝构造函数完成拷贝的。
4.:类中如果没有涉及资源申请时,拷贝构造函数是否写都可以;一旦涉及到资源申请时,则拷贝构 造函数是一定要写的,否则就是浅拷贝,为了提高程序效率,一般对象传参时,尽量使用引用类型,返回时根据实际场景,能用引用尽量使用引 用。
5.拷贝构造函数典型调用场景:
(1).使用已存在对象创建新对象
(2)函数参数类型为类类型对象
(3)函数返回值类型为类类型对象
- #include
- using namespace std;
- class Date
- {
- public:
- Date(int year, int minute, int day)
- {
- cout << "Date(int,int,int):" << this << endl;
- }
- Date(const Date& d)
- {
- cout << "Date(const Date& d):" << this << endl;
- }
- ~Date()
- {
- cout << "~Date():" << this << endl;
- }
- private:
- int _year;
- int _month;
- int _day;
- };
- Date Test(Date d)
- {
- Date temp(d);//调用拷贝构造函数创建temp
- return temp;//函数以值方式返回,返回时使用temp拷贝构造临时对象来返回
- }
- int main()
- {
- Date d1(2022, 8, 10);//调用构造函数创建的d1
- Test(d1);//Test以值传递的方式传参时调用拷贝构造函数创建d
- return 0;
- }

四..赋值运算符重载
1.概念:C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。 函数名字为:关键字operator后面接需要重载的运算符符号。
2.函数原型:返回值类型 operator操作符(参数列表)
3.注意:
(1) 不能通过连接其他符号来创建新的操作符:比如operator@
(2) 重载操作符必须有一个类类型参数
(3) 用于内置类型的运算符,其含义不能改变,例如:内置的整型+,不 能改变其含义
(4) 作为类成员函数重载时,其形参看起来比操作数数目少1,因为成员函数的第一个参数为隐藏的this
(5) (.*)( ::)( sizeof)( ?)( : .) 注意以上5个运算符不能重载。
4.赋值运算符重载格式:
(1)参数类型:const T&,传递引用可以提高传参效率
(2)返回值类型:T&,返回引用可以提高返回的效率,有返回值目的是为了支持连续赋值
(3)检测是否自己给自己赋值
(4)返回*this :要复合连续赋值的含义
- #include
- using namespace std;
- class Date
- {
- public:
- Date(int year = 2022, int month = 8, int day = 10)
- {
- _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;
- }
- private:
- int _year;
- int _month;
- int _day;
- };
-
- int main()
- {
- return 0;
- }
5.赋值运算符只能重载成类的成员函数不能重载成全局函数
- class Date
- {
- public:
- Date(int year = 1900, int month = 1, int day = 1)
- {
- _year = year;
- _month = month;
- _day = day;
- }
- int _year;
- int _month;
- int _day;
- };
- // 赋值运算符重载成全局函数,注意重载成全局函数时没有this指针了,需要给两个参数
- Date& operator=(Date& left, const Date& right)
- {
- if (&left != &right)
- {
- left._year = right._year;
- left._month = right._month;
- left._day = right._day;
- }
- return left;
- }
- // 编译失败:
- // error C2801: “operator =”必须是非静态成员
原因:赋值运算符如果不显式实现,编译器会生成一个默认的。此时用户再在类外自己实现一个全局的赋值运算符重载,就和编译器在类中生成的默认赋值运算符重载冲突了,故赋值运算符重载只能是类的成员函数。
6.用户没有显式实现时,编译器会生成一个默认赋值运算符重载,以值的方式逐字节拷贝。注意:内置类型成员变量是直接赋值的,而自定义类型成员变量需要调用对应类的赋值运算符重载完成赋值。
7.日期类的实现
- #include
- using namespace std;
- class Date
-
- {
-
- public:
-
- // 获取某年某月的天数
-
- int GetMonthDay(int year, int month)const
- {
- static int monthday[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
- if ((month == 2)
- && (_year % 4 == 0 || _year % 100 != 0) && (_year % 400 == 0))
- {
- return 29;
- }
- return monthday[month];
- }
-
- // 全缺省的构造函数
-
- Date(int year = 1900, int month = 1, int day = 1)
- {
- if (year >= 0
- && month >= 1 && month <= 12
- && day >= 1 && day <= GetMonthDay(year, month))
- {
- _year = year;
- _month = month;
- _day = day;
- }
- else
- {
- cout<<"日期有误"<
- }
- }
-
- // 拷贝构造函数
-
- Date(const Date& d)
- {
- this->_year = d._year;
- this->_month = d._month;
- this->_day = d._day;
- }
-
- // 赋值运算符重载
-
- // d2 = d3 -> d2.operator=(&d2, d3)
-
- Date& operator=(const Date& d)
- {
- if (this != &d)
- {
- this->_year = d._year;
- this->_month = d._month;
- this->_day = d._day;
- }
- return *this;
- }
-
- // 日期+=天数
-
- Date& operator+=(int day)
- {
- if (day < 0)
- {
- return *this -= -day;
- }
- _day += day;
- while (day > GetMonthDay(_year, _month))
- {
- _day -= GetMonthDay(_year, _month);
- ++_month;
- if (_month == 13)
- {
- _year++;
- _month = 1;
- }
- }
- return *this;
- }
-
- // 日期+天数
-
- Date operator+(int day)const
- {
- Date tmp(*this);
- tmp += day;
- return tmp;
- }
-
- // 日期-天数
-
- Date operator-(int day)const
- {
- Date tmp(*this);
- tmp -= day;
- return tmp;
- }
-
- // 日期-=天数
-
- Date& operator-=(int day)
- {
- if (day < 0)
- {
- return *this += -day;
- }
- _day -= day;
- while(_day<=0)
- {
-
- _month--;
- if (_month == 0)
- {
- _year--;
- _month = 12;
- }
- _day+= GetMonthDay(_year, _month);
- }
- return *this;
- }
-
- // 前置++
-
- Date& operator++()
- {
- return *this += 1;
- }
-
- // 后置++
-
- Date operator++(int)
- {
- Date tmp(*this);
- *this += 1;
- return tmp;
- }
-
- // 后置--
-
- Date operator--(int)
- {
- Date tmp(*this);
- *this -= 1;
- return tmp;
- }
-
- // 前置--
-
- Date& operator--()
- {
- return *this -= 1;
- }
-
- // >运算符重载
-
- bool operator>(const Date& d)const
- {
- if (_year > d._year)
- {
- return true;
- }
- else if (_year == d._year)
- {
- if (_month > d._month)
- {
- return true;
- }
- else if (_month == d._month&&_day>d._day)
- {
- return true;
- }
- }
- return false;
- }
-
- // ==运算符重载
-
- 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) || (*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 !(*this == d);
- }
-
- // 日期-日期 返回天数
-
- int operator-(const Date& d)const
- {
- //定义一个flag用来判断运算后的符号为+-
- int flag = 1;
- Date max = *this;
- Date min = d;
- if (*this < d)
- {
- max = d;
- min = *this;
- flag = -1;
- }
- //统计日期之间相差的天数
- int day = 0;
- while (min < max)
- {
- ++(min);
- ++(day);
- }
- return day * flag;
- }
- void Print()
- {
- cout << _year << "/" << _month << "/" << _day << endl;
- }
-
- private:
-
- int _year;
-
- int _month;
-
- int _day;
-
- };
- int main()
- {
- Date lin(2022, 8, 12);
- lin.Print();
-
- Date lin1 = lin + 10;
- lin1.Print();
-
- Date lin2(2022, 8, 10);
- cout << "lin == lin2"<<" " << (lin == lin2) << endl;
- cout << "lin > lin2" << " " << (lin > lin2) << endl;
- cout << "lin < lin2" << " " << (lin < lin2) << endl;
-
- return 0;
- }

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

- #include
- using namespace std;
- class Date
- {
- public:
- Date(int year, int month, int day)
- {
- _year = year;
- _month = month;
- _day = day;
- }
- void Print()
- {
- cout << "Print()" << endl;
- cout << "year:" << _year << endl;
- cout << "month:" << _month << endl;
- cout << "day:" << _day << endl << endl;
- }
- void Print() const
- {
- cout << "Print()const" << endl;
- cout << "year:" << _year << endl;
- cout << "month:" << _month << endl;
- cout << "day:" << _day << endl << endl;
- }
- private:
- int _year;
- int _month;
- int _day;
- };
- void Test()
- {
- Date d1(2022, 8, 10);
- d1.Print();
- const Date d2(2022, 8, 10);
- d2.Print();
- }
-
- int main()
- {
- Test();
- return 0;
- }

2.取地址及const取地址操作符重载
- class Date
- {
- public :
- Date* operator&()
- {
- return this ;
- }
-
- const Date* operator&()const
- {
- return this ;
- }
- private :
- int _year ; // 年
- int _month ; // 月
- int _day ; // 日
- };
这两个运算符一般不需要重载,使用编译器生成的默认取地址的重载即可,只有特殊情况,才需要重载,比 如想让别人获取到指定的内容!