🚀前言:
本文主要讲述了类的6个默认成员函数:构造函数/析构函数/拷贝函数/赋值函数/(const)取地址操作符重载函数,并讲述了操作符重载operator相关的函数。
目录
1.类的6个默认成员函数:
默认成员函数:用户没有显式实现,编译器会生成的成员函数称为默认成员函数。
2.构造函数:
2.1定义:
构造函数是一个特殊的成员函数,名字与类名相同,创建类类型对象时由编译器自动调用,以保证 每个数据成员都有 一个合适的初始值,并且在对象整个生命周期内只调用一次。
class Date
{
public:
//构造函数
Date(int year = 1,int month = 1,int day = 1)
{
cout << "Date()" << endl;
_year = year;
_month = month;
_day = day;
}
void Print()
{
cout << _year << "/" << _month << "/" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1(2024, 9, 8);
d1.Print();
Date d2(2018, 5, 5);
d2.Print();
return 0;
}
2.2特性:
构造函数是特殊的成员函数,需要注意的是,构造函数虽然名称叫构造,但是构造函数的主要任 务并不是开空间创建对象,而是初始化对象。
1. 函数名与类名相同。
2. 无返回值。
3. 对象实例化时编译器自动调用对应的构造函数。
4. 构造函数可以重载。
5. 如果类中没有显式定义构造函数,则C++编译器会自动生成一个无参的默认构造函数,一旦 用户显式定义编译器将不再生成。
6.编译器会自动调用自定义类型成员变量的默认构造函数。(内置类型成员变量不会)。
class Time
{
public:
Time()
{
cout << "Time()" << endl;
_hour = 0;
_minute = 0;
_second = 0;
}
private:
int _hour;
int _minute;
int _second;
};
class Date
{
private:
// 基本类型(内置类型)
int _year;
int _month;
int _day;
// 自定义类型
Time _t;
};
int main()
{
Date d;
return 0;
}
7.注意:C++11 中针对内置类型成员不初始化的缺陷,又打了补丁,即:内置类型成员变量在类中声明时可以给默认值。
class Date
{
private://是声明,不是定义。值为默认参数
// 基本类型(内置类型)
int _year = 1970;
int _month = 1;
int _day = 1;
// 自定义类型
Time _t;
};
8.默认构造:无参构造函数/全缺省构造函数/我们没写编译器默认生成的构造函数
3.析构函数:
3.1定义:
析构函数:与构造函数功能相反,析构函数不是完成对对象本身的销毁,局部对象销毁工作是由 编译器完成的。而对象在销毁时会自动调用析构函数,完成对象中资源的清理工作。
3.1特性:
1.函数名是 ~类名
~Date()
{
cout << " ~Date()"<<endl;
_year = 0;
_month = 0;
_day = 0;
}
2.无返回值
3. 一个类只能有一个析构函数。若未显式定义,系统会自动生成默认的析构函数。注意:析构函数不能重载。
4. 对象生命周期结束时,C++编译系统系统自动调用析构函数。
5.编译器生成的默认析构函数,会对自定类型成员调用它的析构函数。
class Time
{
public:
~Time()
{
cout << "~Time()" << endl;
}
private:
int _hour;
int _minute;
int _second;
};
class Date
{
private:
// 基本类型(内置类型)
int _year = 2024;
int _month = 9;
int _day = 8;
// 自定义类型
Time _t;
};
int main()
{
Date d;
return 0;
}
6. 如果类中没有申请资源时,析构函数可以不写,直接使用编译器生成的默认析构函数,比如 Date类;有资源申请时,一定要写,否则会造成资源泄漏,比如Stack类。
~Stack()
{
cout << "~Stack()" << endl;
free(_array);
_array = nullptr;
_capacity = 0;
_size = 0;
}
4.拷贝构造函数:
4.1定义:
将一个已经生成的类对象拷贝给一个未生成的对象。
4.2特性:
1.函数名是类名,参数是(const 类名& 对象)
Date(const Date& d) // 正确写法
//Date(const Date d) // 错误写法:编译报错,会引发无穷递归
{
cout << "Date(const Date & d)" << endl;
_year = d._year;
_month = d._month;
_day = d._day;
}
2. 拷贝构造函数的参数只有一个且必须是类类型对象的引用,使用传值方式编译器直接报错, 因为会引发无穷递归调用。
eg.Date(const Date d)
3. 若未显式定义,编译器会生成默认的拷贝构造函数。 默认的拷贝构造函数对象按内存存储按字节序完成拷贝,这种拷贝叫做浅拷贝,或者值拷贝。
class Time
{
public:
Time()
{
_hour = 1;
_minute = 1;
_second = 1;
}
Time(const Time& t)
{
_hour = t._hour;
_minute = t._minute;
_second = t._second;
cout << "Time::Time(const Time&)" << endl;
}
private:
int _hour;
int _minute;
int _second;
};
class Date
{
private:
// 基本类型(内置类型)
int _year = 2024;
int _month = 9;
int _day = 8;
// 自定义类型
Time _t;
};
int main()
{
Date d1;
Date d2(d1);
return 0;
}
注意:在编译器生成的默认拷贝构造函数中,内置类型是按照字节方式直接拷贝的,而自定义类型是调用其拷贝构造函数完成拷贝的。
4. 编译器生成的默认拷贝构造函数已经可以完成字节序的值拷贝了,像日期类这样的类是没必要的,但Stack类是必要的。
// 这里会发现下面的程序会崩溃掉,就需要深拷贝去解决。
typedef int DataType;
class Stack
{
public:
Stack(size_t capacity = 10)
{
_array = (DataType*)malloc(capacity * sizeof(DataType));
if (nullptr == _array)
{
perror("malloc申请空间失败");
return;
}
_size = 0;
_capacity = capacity;
}
void Push(const DataType& data)
{
// CheckCapacity();
_array[_size] = data;
_size++;
}
~Stack()
{
if (_array)
{
free(_array);
_array = nullptr;
_capacity = 0;
_size = 0;
}
}
private:
DataType *_array;
size_t _size;
size_t _capacity;
};
int main()
{
Stack s1;
s1.Push(1);
s1.Push(2);
s1.Push(3);
s1.Push(4);
Stack s2(s1);
return 0;
}
原因:s1,s2指向同一个地址,当析构时,会出现空间多次销毁,报错。
注意:类中如果没有涉及资源申请时,拷贝构造函数是否写都可以;一旦涉及到资源申请 时,则拷贝构造函数是一定要写的,否则就是浅拷贝。
5. 拷贝构造函数典型调用场景: 使用已存在对象创建新对象,函数参数类型为类类型对象,函数返回值类型为类类型对象。
5.赋值运算符重载:
5.1运算符重载:
1.C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。
2.函数名字为:关键字operator后面接需要重载的运算符符号。
3. 函数原型:返回值类型 operator操作符(参数列表)。
4.注意:
a.不能通过连接其他符号来创建新的操作符:比如operator@
b.重载操作符必须有一个类类型参数。
c.用于内置类型的运算符,其含义不能改变,例如:内置的整型+,不能改变其含义。
d.作为类成员函数重载时,其形参看起来比操作数数目少1,因为成员函数的第一个参数为隐藏的this。
e. .* :: sizeof ?: . 注意以上5个运算符不能重载。
5.2重载operator==
5.2.1全局的operator==
class Date
{
public:
Date(int year = 2024, int month = 9, int day = 8)
{
_year = year;
_month = month;
_day = day;
}
//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 d1(2024, 9 , 8);
Date d2(2018, 5 , 5);
cout<<(d1 == d2)<<endl;
return 0;
}
5.2.2类成员的operator==
class Date
{
public:
Date(int year = 2024, int month = 9, int day = 8)
{
_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;
};
5.2.3友元的operator==
class Date
{
public:
//友元friend,使用后可以访问私有变量。
friend bool operator==(const Date& d1, const Date& d2);
Date(int year = 2024, int month = 9, int day = 8)
{
_year = year;
_month = month;
_day = day;
}
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 d1(2024, 9 , 8);
Date d2(2018, 5 , 5);
cout<<(d1 == d2)<<endl;
return 0;
}
5.3赋值运算符重载的实现:
5.3.1.赋值运算符重载格式:
1.参数类型:const T&,传递引用可以提高传参效率。
2.返回值类型:T&,返回引用可以提高返回的效率,有返回值目的是为了支持连续赋值 。
3.检测是否自己给自己赋值。
4.返回*this :要复合连续赋值的含义。
5.3.2.赋值运算符重载实现:
class Date
{
public :
Date(int year = 1, 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;
}
return *this;
}
private:
int _year ;
int _month ;
int _day ;
};
5.3.3赋值运算符的特性:
1. 赋值运算符只能重载成类的成员函数不能重载成全局函数
class Date
{
public:
Date(int year = 1, 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 =”必须是非静态成员
原因:赋值运算符如果不显式实现,编译器会生成一个默认的。此时用户再在类外自己实现 一个全局的赋值运算符重载,就和编译器在类中生成的默认赋值运算符重载冲突了,故赋值运算符重载只能是类的成员函数
2.用户没有显式实现时,编译器会生成一个默认赋值运算符重载,以值的方式逐字节拷贝。
注 意:内置类型成员变量是直接赋值的,而自定义类型成员变量需要调用对应类的赋值运算符重载完成赋值。若为Date类可以不用写赋值重载函数,而Stack则必须自己写赋值重载函数,原因:与4.2.4相似。
class Time
{
public:
Time()
{
_hour = 1;
_minute = 1;
_second = 1;
}
Time& operator=(const Time& t)
{
if (this != &t)
{
_hour = t._hour;
_minute = t._minute;
_second = t._second;
}
return *this;
}
private:
int _hour;
int _minute;
int _second;
};
class Date
{
private:
// 基本类型(内置类型)
int _year = 1;
int _month = 1;
int _day = 1;
// 自定义类型
Time _t;
};
int main()
{
Date d1;
Date d2;
d1 = d2;
return 0;
}
6.const成员函数:
1.定义:
将const修饰的“成员函数”称之为const成员函数,const修饰类成员函数,实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改。
2.代码演示:
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; // 日
};
int main()
{
Date d1(2024, 9, 10);
d1.Print();
const Date d2(2018, 5, 5);
d2.Print();
return 0;
}
3.思考:
1. const对象可以调用非const成员函数吗? 不可以
2. 非const对象可以调用const成员函数吗? 可以
3. const成员函数内可以调用其它的非const成员函数吗? 不可以
4. 非const成员函数内可以调用其它的const成员函数吗? 可以
7.取地址及const取地址操作符重载:
这两个默认成员函数一般不用重新定义 ,编译器默认会生成。
class Date
{
public :
Date* operator&()
{
return this;
}
const Date* operator&()const
{
return this ;
}
private :
int _year ; // 年
int _month ; // 月
int _day ; // 日
};
两个运算符一般不需要重载,使用编译器生成的默认取地址的重载即可,只有特殊情况,才需 要重载,比如想让别人获取到指定的内容!
8.Date类代码实现:
8.1Date.h
#pragma once
#include<iostream>
using namespace std;
class Date
{
//友元声明
friend ostream& operator<< (ostream& out, const Date& d);
friend istream& operator>> (istream& in, Date& d);
public:
Date(int year = 1, int month = 1, int day = 1);
//void Print(const Date* this);
void Print()const;
/*拷贝构造
Date(const Date& d){}*/
//赋值=
Date& operator=(const Date& d);
//总结:只读函数可以加const,内部不涉及修改的函数
bool operator< (const Date& d) const;
bool operator== (const Date& d) const;
bool operator<= (const Date& d) const;
bool operator> (const Date& d) const;
bool operator>= (const Date& d) const;
Date& operator+=(int day);
Date operator+(int day) const;
Date& operator-=(int day);
Date operator-(int day) const;
//函数重载,运算符重载
//++d
Date& operator++();
//d++,调用时会特殊处理加一个int参数,来占位。
Date operator++(int);
Date& operator--();
Date operator--(int);
int operator-(const Date& d) const;
//void operator<< (ostream& out) const;
private:
int _year;
int _month;
int _day;
};
8.2Date.cpp
#include "Date.h"
int GetMonthDay(int year, int month)
{
const static int arrayMonthDay[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 arrayMonthDay[month];
}
Date::Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
//检查日期
if (month < 1 || month>12 || day<1||day > GetMonthDay(year, month))
{
cout << "非法日期" << endl;
//exit(1);
}
}
void Date:: Print()const
{
cout << _year << "年" << _month << "月" << _day << "日 " << endl;
}
//d1 = d3
Date& Date::operator=(const Date& d)
{
if (&d!=this)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
return *this;
}
bool Date:: operator< (const Date& d) const
{
if (_year < d._year)
{
return true;
}
else if (_year == d._year && _month < d._month)
{
return true;
}
else if (_year == d._year && _month == d._month && _day < d._day)
{
return true;
}
return false;
}
bool Date:: operator== (const Date& d) const
{
return _year == d._year && _month == d._month && _day == d._day;
}
bool Date:: operator<= (const Date& d) const
{
return *this < d || *this == d;
}
bool Date:: operator> (const Date& d) const
{
return !(*this <= d);
}
bool Date:: operator>= (const Date& d) const
{
return *this > d || *this == d;
}
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 == 13)
{
_year++;
_month = 1;
}
}
return *this;
}
Date Date:: operator+(int day) const
{
if (day < 0)
{
return *this - (-day);
}
Date tmp(*this);
tmp += day;
return tmp;
}
Date& 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 Date:: operator-(int day) const
{
if (day < 0)
{
return *this - (-day);
}
Date tmp(*this);
tmp -= day;
return tmp;
}
Date& Date:: operator++()
{
*this += 1;
return *this;
}
Date Date:: operator++(int)
{
Date tmp(*this);
*this += 1;
return tmp;
}
Date& Date:: operator--()
{
*this -= 1;
return *this;
}
Date Date:: operator--(int)
{
Date tmp = *this;
*this -= 1;
return tmp;
}
int Date:: operator-(const Date& d) const
{
Date max = *this;
Date min = d;
int flag = 1;
if (max < min)
{
max = d;
min = *this;
flag = -1;
}
int counts = 0;//计算
while (max > min)
{
max--;
counts++;
}
return flag * counts;
}
/*void Date:: operator<<(ostream& out) const
{
out << _year << "年" << _month << "月" << _day << "日 " << endl;
}*/
ostream& operator<< (ostream& out, const Date& d)
{
out << d._year << "年" << d._month << "月" << d._day << "日 " << endl;
return out;
}
istream& operator>> (istream& in,Date& d)
{
in >> d._year >> d._month >> d._day;
return in;
}
8.3Test.cpp
#include "Date.h"
void TestDate1()
{
Date d1(2023, 8,6);
//拷贝构造,一个已经存在的对象去初始化另一个要构造的对象。
Date d2(d1);
d1.Print();
d2.Print();
Date d3(2023, 8, 12);
//赋值,两个都对象都存在,一共赋值给另一个。
d1 = d2 = d3;
d1.Print();
d2.Print();
d3.Print();
}
void TestDate2()
{
Date d1(2023,7 , 27);
d1 += 20000;
d1.Print();
Date d2(2024, 8, 31);
Date ret = d2;//拷贝构造(已经存在一个对象)
}
void TestDate3()
{
Date d1(2023, 7, 27);
d1 -= 20000;
d1.Print();
}
void TestDate4()
{
Date d1(2024, 8, 31);
d1 -= 200;
d1.Print();
Date d2(2024, 8, 31);
d2 += -200;
d2.Print();
}
void TestDate5()
{
Date d1(2024, 8, 31);
Date ret1 = ++d1;
//Date ret1 = d1.operator++();
ret1.Print();
Date d2(2024, 8, 31);
Date ret2 = d2++;
//Date ret2 = d2.operator++(0);//参数只是为了匹配
ret2.Print();
}
void TestDate6()
{
Date d1(2024, 8, 31);
Date ret1 = --d1;
ret1.Print();
Date d2(2024, 8, 31);
Date ret2 = d2--;
ret2.Print();
}
void TestDate7()
{
Date d1(2024, 9, 1);
Date d2(2018, 5, 5);
int ret = d1 - d2;
cout << ret << endl;
}
void TestDate8()
{
const Date d1(2024, 9, 1);
d1.Print();
Date d2(2024, 9, 1);
d2.Print();
//权限可以平移缩小,不能放大。
}
void TestDate9()
{
Date d1(2024, 9, 1);
Date d2(2024, 10, 1);
cout << d1 << d2;
cin >> d1;
cout << d1;
}
int main()
{
TestDate9();
return 0;
}