前言
我们在学习C++类的时候需要知道它的默认函数,主要包括构造函数(相当于C语言中的初始化函数)、析构函数(相当于销毁函数)、拷贝构造函数、赋值重载函数。接下来依次讲解该4个函数。
1、构造函数
1、构造函数主要是给具体对象实例时初始化的。替代了C语言中的Init()初始化函数。在C++中构造函数是自动调用的,提高了效率。
2、构造函数的特点:
①函数名和类名相同。
②无返回值。(包括void类型也没有);
③对象在实例化的时候,系统会自动调用构造函数。
④构造函数是可以重载的。
⑤若用户没写构造函数,系统会自动生成一个无参的默认构造函数。
3、构造函数分类:
①无参构造函数。
②带参构造函数。
③全缺省构造函数。
注意:三个种类的函数有且只有一个存在,不能同时存在。无参构造函数和全缺省构造函数在调用时会发生歧义,因为全缺省构造函数在调用的时候也可以忽略参数,因此分不清。
对于默认构造,此处的无参和全缺省构造函数都是默认构造,只要是调用不传实参的都是默认构造。
- 代码演示:
//构造函数
//日期类Date
class Date
{
public:
//无参构造函数
Date()
{
_year = 1;
_month = 1;
_day = 1;
}
//带参构造函数
Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
}
//全缺省构造函数
Date(int year = 2000, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
//成员函数
void print()
{
cout << _year << "/" << _month << "/" << _day << "\n";
}
private:
int _year;
int _month;
int _day;
};
注意:①三个函数只能存在一个,根据对象实例化样式来写构造函数;比如像下面:
②全缺省构造函数可以实例化三种不同对象;比如下面只写全缺省构造函数,三种对象依然可以实例化。有实际参数就用实参,实参不全的使用缺省值,没传实参的全部使用缺省值;
#include<iostream>
using namespace std;
//日期类Date
class Date
{
public:
无参构造函数
//Date()
//{
// _year = 1;
// _month = 1;
// _day = 1;
//}
带参构造函数
//Date(int year, int month, int day)
//{
// _year = year;
// _month = month;
// _day = day;
//}
//全缺省构造函数
Date(int year = 2000, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
//成员函数
void print()
{
cout << _year << "/" << _month << "/" << _day << "\n";
}
private:
int _year;
int _month;
int _day;
};
int main()
{
//调用无参构造函数
Date date1;
date1.print();
//调用有参构造函数
Date date2(2024,8,3);
date2.print();
//调用全缺省构造函数
Date date3(2024);
date3.print();
return 0;
}
2、析构函数
1、含义:就是释放空间的作用;然而不是所有空间都是析构函数管理,析构函数不完成对对象本身的销毁,因为局部对象是存储在栈帧中的,函数结束栈帧就被销毁了,局部对象就跟着被释放了,但是像另外动态开辟的对象就要调用析构函数才可以销毁,不然造成内存泄漏。C++中销毁时会自动调用析构函数。
2、析构函数的格式:在构造函数前面加上~
就可以了。
3、析构函数特点:
①和默认构造函数一样,无参数无返回值。
②一个类只能有一个析构函数,用户未定义,系统会生成默认的析构函数。
③对象圣生命周期结束时,系统会自动调用析构函数。
④内置类型成员变量不作处理,自定义类型会调用析构函数。
⑤如果类中没有申请资源时,析构函数可以不写,直接使⽤编译器⽣成的默认析构函数。
⑥当用户显示写析构函数时,无论什么情况,自定义类型成员都会自动调用析构函数。
⑦在一个局部域的多个对象,C++规定先定义的后析构,如同栈先进后出。
比如下面两个题:
3、拷贝构造函数
1、含义:其实是特殊的构造函数,该函数的第一个参数是自身类类型的引用,并且若有其他参数,这些参数必须含有缺省值。
2、拷贝构造函数的特点:
①拷贝构造函数是构造函数的一个重载。
②拷贝构造函数,第一个参数必须是自身类类型的引用。
③C++规定自定义类型对象进行拷贝的时候必须调用拷贝构造函数,自定义类型的传值传参和传值返回都会调用拷贝构造函数。
④用户为定义拷贝构造函数的时候,编译器会自动生成拷贝构造函数。但是自动生成的拷贝构造函数对内置类型成员变量只完成值拷贝(浅拷贝),即按每一个字节拷贝,对于自定义类型要进行拷贝,必须自己写拷贝构造函数,默认生成的无法满足深拷贝需求。
⑥传值返回,会产生临时对象并调用拷贝构造函数;传值引用返回,返回的是对象的别名,不会产生拷贝。
class Date
{
public:
//全缺省构造函数
Date(int year=1,int month=1,int day=1)
{
cout << "调用构造函数:";
_year = year;
_month = month;
_day = day;
}
//拷贝构造函数
Date(const Date& d)
{
cout << "调用拷贝构造函数:";
_year = d._year;
_month = d._month;
_day = d._day;
}
void print()
{
cout << _year << "/" << _month << "/" << _day << "\n";
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date date1(2024);
date1.print();
Date date2(date1);
date2.print();
Date date3 = date1;
date3.print();
return 0;
}
- 两种写法都可以调用拷贝构造函数。
4、运算符重载
1、 何为运算符重载内呢?在C++中当运算符用于类类型的对象时候,需要通过运算符重载的形式指定新的含义。
C++语法规定,类类型对象使用运算符时候,必须转换成调用对应的运算符重载,若没写运算符重载则编译会报错。
2、运算符重载格式:[返回值] operator+运算符(<参数列表>) {函数体}
3、注意:重载运算符函数的参数必须和该运算符的运算对象数量一样。一元运算符有一个参数,二元运算符有两个参数(运算符左侧对应第一个参数,右侧对应第二个参数)
4、若重载运算符函数是类的成员函数的时候,该函数中第一个参数是this
指针,因此第一个运算对象默认传给隐式的this
指针,函数的参数个数就可以少写一个。
5、运算符重载以后,其不改变原来运算符的优先级和结合性。
6、.* :: sizeof ?: .
这5个运算符不能重载。
- 例:重载
==
运算符
若在C++中类类型进行运算的时候,不重载相应的运算符,是无法使用,就像下面:划红线处,会报错
//3、运算符重载,重载函数为全局函数
class Date
{
//友元
//friend bool operator== (const Date& d1, const Date& d2);
public:
Date(int year=1,int month=1,int day=1)
{
_year = year;
_month = month;
_day = day;
}
void print()
{
cout << _year << "/" << _month << "/" << _day << "\n";
}
//private:
int _year;
int _month;
int _day;
};
//重载==
bool operator== (const Date& d1,const Date& d2)
{
return d1._year == d2._year && d1._month == d2._month && d1._day == d2._day;
}
int main()
{
Date date1(2024,8);
date1.print();
Date date2(2024,7);
date2.print();
//两种便是都可以
cout << operator==(date1 , date2) << "\n";
cout << (date1 == date2) <<"\n";
return 0;
}
上面代码的运行结果:
重载函数写为成员函数
//重载运算符==,写为成员函数
class Date
{
public:
//构造函数
Date(int year=1,int month=1,int day=1)
{
_year = year;
_month = month;
_day = day;
}
void print()
{
cout << _year << "/" << _month << "/" << _day << "\n";
}
//重载运算符==,写为成员函数,
//首先==是二元运算符,但写为成员函数后,
// 第一个参数是默认的隐式的this指针,因此只写一个参数
bool operator==(const Date& d1)
{
//this指针可以不写
return this->_year == d1._year
&& this->_month == d1._year
&& this->_day == d1._day;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date date1(2024);
date1.print();
Date date2(2024,2);
date2.print();
cout << date1.operator==(date2) << "\n";//调用格式
cout << (date1==date2) << "\n";//调用格式
return 0;
}
5、赋值运算符重载=
1、首先赋值运算符重载函数是一个默认成员函数。
2、其作用:用于完成两个已经存在的对象直接的拷贝赋值。
3、其特点:
①赋值运算符重载是一个运算符重载,C++规定必须重载为成员函数。由于赋值不改变内容,因此参数建议写为const修饰的类类型的引用,减少传值传参的拷贝,提高效率。
比如:bool operator==(const Date& d1){}
②拥有返回值。也写为当前类类型的引用,传引用返回减拷贝,有返回值也支持连续赋值。
③用户未定义赋值运算符重载函数的时候,编译器会自动生成默认的赋值运算符重载函数,也只是对内置类型完成浅拷贝,对于自定义类型必须调用显示的赋值运算符重载函数。
代码显示
//5、赋值运算符重载
class Date
{
public:
//构造函数
Date(int year = 1, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
void print()
{
cout << _year << "/" << _month << "/" << _day << "\n";
}
//拷贝构造函数
Date(const Date& d)
{
cout << "拷贝构造函数:";
_year = d._year;
_month = d._month;
_day = d._day;
}
//赋值运算符重载
Date& operator=(const Date& d)
{
cout << "赋值运算符重载:";
_year = d._year;
_month = d._month;
_day = d._day;
return *this;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date date1(2024,6,1);
date1.print();
Date date2;
//赋值拷贝,两种都可以。
date2 = date1;
//date2.operator=(date1);
date2.print();
//拷贝构造。
Date date3 = date1;
date3.print();
return 0;
}
✨日期类的实现
1、Date.h
#pragma once
#include<iostream>
using namespace std;
class Date
{
public:
// 获取某年某月的天数
int GetMonthDay(int year, int month)
{
static int month_day_number[] = { -1,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;
}
else
{
return month_day_number[month];
}
}
// 全缺省的构造函数
Date(int year = 1900, int month = 1, int day = 1);
// 拷贝构造函数
// d2(d1)
Date(const Date& d);
void print();
赋值运算符重载
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);
前置--
Date& operator--();
>运算符重载
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);
日期-日期 返回天数
int operator-(const Date& d);
private:
int _year;
int _month;
int _day;
};
2、Date.cpp
#include "Date.h"
Date::Date(int year, int month, int day)
{
cout << "构造函数:";
_year = year;
_month = month;
_day = day;
}
Date::Date(const Date& d)
{
cout << "拷贝构造:";
_year = d._year;
_month = d._month;
_day = d._day;
}
void Date::print()
{
cout << _year << "-" << _month << "-" << _day << "\n";
}
Date& Date::operator=(const Date& d)
{
cout << "赋值重载:";
_year = d._year;
_month = d._month;
_day = d._day;
return *this;
}
Date::~Date()
{
}
Date& Date::operator+=(int day)
{
if (day < 0)
{
return *this -= -day;
}
_day += day;
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year,_month);
_month++;
if (_month > 12)
{
_year++;
_month = 1;
}
}
return *this;
}
Date Date::operator+(int day)
{
Date tmp = *this;
//tmp.operator+=(day);
tmp += day;
return tmp;
}
Date& Date::operator-=(int day)
{
if (day < 0)
{
return *this += -day;
}
_day -= day;
while(_day<=0)
{
_day += GetMonthDay(_year, _month);
_month--;
if (_month < 1)
{
_year--;
_month = 12;
}
}
return *this;
}
Date& Date::operator++()
{
*this += 1;
return *this;
}
Date Date::operator++(int)
{
Date tmp(*this);
*this += 1;
return tmp;
}
Date Date::operator--(int)
{
Date tmp(*this);
*this -= 1;
return tmp;
}
Date& Date::operator--()
{
*this -= 1;
return *this;
}
bool Date::operator>(const Date& d)
{
if (_year > d._year)
{
return true;
}
else if (_year == d._year)
{
if (_month > d._month)
{
return true;
}
else if(_month==d._month)
{
/*if (_day > d._day)
{
return true;
}*/
return _day > d._day;
}
}
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);
}
int Date::operator-(const Date& d)
{
int n = 0;
int falg = 1;
Date max = *this;
Date min = d;
if (*this<d)
{
max = d;
min = *this;
falg = -1;
}
while (min != max)
{
++min;
++n;
}
return n * falg;
}
Date Date::operator-(int day)
{
Date tmp = *this;
//tmp.operator-=(day);
tmp -= day;
return tmp;
}