目录
在C++中,类是面向对象编程一个重要的概念,为了更加方便地使用类,类产生了六个默认成员参数,让我们能够更加方便,快捷地使用类。
默认成员函数
默认成员函数是什么呢?
用户没有显式实现,编译器会生成的成员函数称为默认成员函数。
例如在一个空类中,虽然我们没有声明任何数据,但是其实它里面还是存在这六个默认成员函数。
默认成员函数的功能
类的六个默认构造函数的功能如下图所示:
默认成员函数详解
构造函数
构造函数的特征:
1.函数名与类名相同
2.无返回值(并不是void)
3.对象实例化时自动调用
4.允许函数重载
什么是构造函数呢?
构造函数就是完成类中数据初始化的函数,在类中我们可以使用默认生成的构造函数,也可以自定义构造函数。
默认构造函数的作用:
默认构造函数可以自动帮助用户完成类的初始化工作,避免了用户忘记或者遗漏初始化工作。
自动生成的构造函数特点:
当我们不去自定义构造函数时,编译器会自动生成默认构造函数,
默认构造函数的特点是:对内置类型不做处理,对自定义类型会调用其默认构造。
C++11中的调整
c++11为了弥补默认构造函数的缺陷,对这个问题打了一个补丁,即在类声明时,可以给内置类型缺省值,如下所示
//老版本
class Date
{
private:
int year;
int month;
int day;
}
//C++11后
class Date
{
private:
int year = 2023;
int month = 1;
int day = 1;
}
给了缺省值,在使用默认构造函数就会自动把缺省值赋值给内置类型的变量。
因此,当类中存在内置类型且没有缺省值的情况下,我们应该考虑自定义构造函数,并且根据情况,我们可以将自定义构造函数构造成全缺省,无参数,或者半缺省函数。
日期类Date中的全缺省构造函数如下:
//构造函数
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
析构函数
析构函数完成对对象中资源的清理
但是析构函数不参与对于类的销毁,对于内置类型的空间也不参与销毁,这都是编译器完成的操作(这是由于静态空间会自动销毁)。若存在动态开辟的空间,需要自定义析构函数。
析构函数特征:
析构函数是特殊的成员函数,其特征如下:
1. 析构函数名是在类名前加上字符 ~。
2. 无参数无返回值类型。
3. 一个类只能有一个析构函数。若未显式定义,系统会自动生成默认的析构函数。
4. 对象生命周期结束时,C++编译系统系统自动调用析构函数。5.析构函数不能重载
默认生成析构函数的特点 :
1.对内置类型不做处理
2.对自定义类型调用其析构函数
需要自定义析构函数的场景:
存在动态开辟空间的类,需要自己实现析构函数才能完成对于动态空间的清理。
日期类Date中的自定义析构函数:
//析构函数
~Date()
{
_year = 0;
_month = 0;
_day = 0;
}
拷贝构造函数
拷贝构造函数完成同类对象初始化创建对象
拷贝构造函数的特征:
1. 拷贝构造函数是构造函数的一个重载形式。
2. 拷贝构造函数的参数只有一个且必须是类类型对象的引用,使用传值方式编译器直接报错,因为会引发无穷递归调用
之所以不使用引用会发生无穷递归,原因如下:
Date (Date d1)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
//在这个拷贝构造中,如果我们使用这个拷贝构造函数,将实参传递给形参的过程是一次拷贝
//会再次使用拷贝构造函数,再次将实参传递给形参的过程又是一次拷贝,循环往复,就会形成
//无穷递归
Date(const Date& d)//const防止反向拷贝
{
_year = d._year;
_month = d._month;
_day = d._day;
}
//采用引用传参,不会有实参传递给形参的的拷贝过程,因此不会发生无穷递归
如下是拷贝构造的常见使用方法:
Date d(2023,5,1);//调用构造函数创建类d1
Date d2(d1); //使用拷贝构造创建与类d1相同的类d2
默认生成的拷贝构造的缺陷:
对于内置类型直接拷贝,对于自定义类型使用默认拷贝构造
默认的拷贝构造函数对象按内存存储按字节序完成拷贝,这种拷贝叫做浅拷贝,或者值拷贝,
对于动态开辟空间中的数据,浅拷贝明显是不足以实现拷贝构造功能的。
因此存在动态资源的类必须自定义拷贝构造函数。
赋值运算符重载函数
功能:完成已经存在的两个对象之间的复制
运算符重载是具有特殊函数名的函数,也具有其返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似
函数名:关键字operator后面接需要重载的运算符符号。
函数参数:运算符操作数
函数返回值:运算符运算后结果
函数原型:返回值类型 operator操作符(参数列表)
赋值运算符重载有两种场景:1.单次赋值 2.连续赋值
基于这两种要求,我们实现如下的赋值运算符重载
Date& operator=(const Date& d)//返回值为Date& 是为了实现连续赋值,const防止反向赋值
{
if (this != &d)//防止自身赋值
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return (*this);
}
默认生成赋值重载的功能:
1.内置类型进行值拷贝
2.自定义类型调用其赋值重载
默认生成赋值重载的缺陷:
涉及动态内存管理的类无法完成。这是因为默认生成的赋值重载对于内置是单纯的值拷贝
对于以下情况
class Test
{
private:
int * p=(int*)malloc(sizeof(int)*4);
}
int main()
{
Test t1;
Test t2;
t2 = t1;
return 0;
}
如果使用默认赋值重载,无异于使用 t2=t1; ,这样就遗失了动态开辟的 t1 空间,造成了内存泄漏,所以,对于有动态开辟内存的类。我们需要自定义赋值重载。
日期类实现
//Date.h
#pragma once
#include<iostream>
class Date
{
public:
//构造函数
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
//析构函数
~Date()
{
_year = 0;
_month = 0;
_day = 0;
}
//赋值重载函数
Date& operator=(const Date& d)
{
if (this != &d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return (*this);
}
// 拷贝构造函数
Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
void Print()
{
std::cout << _year << "年" << _month << "月" << _day << "日" << std::endl;
}
// 获取某年某月的天数
int GetMonthDay(int year, int month);
// 日期+=天数
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 d2);
bool operator<(const Date d2);
bool operator==(const Date d2);
bool operator>=(const Date d2);
bool operator<=(const Date d2);
bool operator!=(const Date d2);
private:
int _year = 1 ;
int _month = 1 ;
int _day = 1 ;
};
//Date.cpp文件
#include"Date.h"
bool Date::operator>(const Date d2)
{
if (_year > d2._year)
{
return true;
}
else if (_month > d2._month)
{
return true;
}
else if (_month > d2._day)
{
return true;
}
return false;
}
bool Date::operator<(const Date d2)
{
if (_year < d2._year)
{
return true;
}
else if (_month < d2._month)
{
return true;
}
else if (_month < d2._day)
{
return true;
}
return false;
}
bool Date::operator ==(const Date d2)
{
return (_year == d2._year) && (_month == d2._month) && (_day == d2._day);
}
bool Date::operator >=(const Date d2)
{
return (*this == d2) || (*this > d2);
}
bool Date::operator<=(const Date d2)
{
return (*this == d2) || (*this < d2);
}
bool Date::operator!=(const Date d2)
{
return !(*this==d2);
}
int Date::GetMonthDay(int year, int month)
{
int day[12] = { 31,29,31,30,31,30,31,31,30,31,30,31 };
if ((year % 10 == 0 && year % 100 != 0) || (year % 400) == 0)
{
return day[month - 1];
}
else
{
if (month == 2)
{
return 28;
}
return day[month - 1];
}
}
Date& Date::operator+=(int day)
{
_day += day;
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
_month++;
if (_month > 12)
{
_month = 1;
_year++;
}
}
return (*this);
}
Date Date::operator+(int day)
{
Date tmp=(*this);
tmp += _day;
return tmp;
}
Date Date::operator-(int day)
{
Date tmp=(*this);
tmp -= day;
return tmp;
}
Date& Date::operator-=(int day)
{
if (_day > day)
{
_day -= day;
return (*this);
}
else
{
while (_day < day)
{
_day += GetMonthDay(_year, _month-1);
_month--;
}
_day -= day;
return (*this);
}
}
// 前置++
Date& Date::operator++()
{
_day++;
if (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
_month++;
}
return (*this);
}
// 后置++
Date Date::operator++(int)
{
Date tmp = (*this);
++tmp;
return tmp;
}
// 后置--
Date Date::operator--(int)
{
Date tmp = (*this);
tmp._day--;
if (tmp._day < 1)
{
tmp._day += GetMonthDay(tmp._year, tmp._month - 1);
tmp._month--;
}
return tmp;
}
// 前置--
Date& Date::operator--()
{
_day--;
if (_day < 1)
{
_day += GetMonthDay(_year, _month - 1);
_month--;
}
return (*this);
}