一、 类的6个默认成员函数
默认成员函数,简单来说,就是如果我们不在编译器里进行写入,那编译器会自动生成默认成员函数,如果写了,就用自己写的。
附:有些编译器默认生成不可用,需要自己写。
有些编译器生成可用,不需要写。
二、 构造函数
1.构造函数的概念:构造函数是一个特殊的成员函数,名字与类名相同,创建类类型对象时由编译器自动调用,保证每个数据成员都有 一个合适的初始值,并且在对象的生命周期内只调用一次。
构造函数并不是构造一个对象,而指的是去初始化这个对象。
2.构造函数的四个特性:
(1)函数名与类名相同
(2)没有返回值
(3)对象实例化时编译器自动调用对应的构造函数
(4)构造函数可以重载
(5) 如果类中没有显式定义构造函数,则C++编译器会自动生成一个无参的默认构造函数,如果用户显式定义编译器将不再生成。
class Date
{
public:
/*
// 如果用户显式定义了构造函数,编译器将不再生成
Date (int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
}
*/
private:
int _year;
int _month;
int _day;
};
void Test()
{
// 没有定义构造函数,对象也可以创建成功,因此此处调用的是编译器生成的默认构造函数
Date d;
}
(6) 无参的构造函数和全缺省的构造函数都称为默认构造函数,并且默认构造函数只能有一个。
无参构造函数、全缺省构造函数、编译器默认生成的构造函数,都可以当成默认成员函数。
class Stack
{
public:
Stack(int capacity = 4)
{
_a =(int*) malloc(sizeof(int)* capacity);
_size = 0;
_capacity = capacity;
}
private:
int _size;
int _capacity;
int* _a;
};
int main()
{
Stack St1;
Stack St2(10);
}
(7)Date() 和 Date (int year = 1900, int month = 1, int day = 1) 不能同时存在
如果两者同时存在,那么下方代码d2的构造,初始化会存在歧义。
class Date
{
public:
Date()
{
_year = 1900 ;
_month = 1 ;
_day = 1;
}
Date (int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
private :
int _year ;
int _month ;
int _day ;
};
// 以下测试函数能通过编译吗?
void Test()
{
Date d1(2000,1,1);
Date d2;
}
(8)成员变量中的两种类型:基本类型(内置类型)和自定义类型
在没有写构造函数的情况下,编译器会自动生成默认构造函数:
对于成员变量中的基本类型,构造函数不做任何事情。
对于成员变量中的自定义类型,它会去调用自己的默认构造函数必须为 (不传参数的构造函数)
class Date
{
private:
// 基本类型(内置类型)
int _year;
int _month;
int _day;
// 自定义类型
Time _t;
};
int main()
{
Date d;
return 0; }
三.、析构函数
**1.析构函数的概念:**对象在销毁时会调用析构函数,完成类的资源清理工作。
2.析构函数的特性:
- 析构函数名是在类名前加上字符 ~。
- 无参数无返回值。
- 一个类有且只有一个析构函数。若未显式定义,系统会自动生成默认的析构函数。
- 对象生命周期结束时,C++编译系统系统自动调用析构函数。
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
//析构函数
~Date()
{
//日期类没有需要清理的资源,严格来说,Date不需要写析构函数
//系统自动默认生成
}
void print()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
class Stack
{
public:
Stack(int capacity=4)
{
cout << "hello" << endl;
_a = (int*)malloc(sizeof(int)* capacity);
_size = 0;
_capacity = capacity;
}
//日期类没有资源要清理,不需要自己写析构函数
//而像Stack这种类,要清理释放资源,一定要自己实现析构函数
//Stack的析构函数
~Stack()
{
cout <<"hello" << endl;
free(_a);
_a = nullptr;
_size = 0;
_capacity = 0;
}
private:
int _size;
int _capacity;
int *_a;
};
int main()
{
Date d1;
Stack T1;
}
3.默认析构函数,会针对成员变量中自定义类型String _name来处理,调用它的析构函数,而对于内置类型不进行处理。
注:此处析构函数跟构造函数相类似。
class String
{
public:
String(const char* str = "jack")
{
_str = (char*)malloc(strlen(str) + 1);
strcpy(_str, str);
}
//析构函数
~String()
{
cout << "~String()" << endl;
free(_str);
}
private:
char* _str;
};
class Person
{
private:
//自定义类型
String _name;
//基本类型(内置类型)
int _age;
};
int main()
{
Person p;
return 0;
四、 拷贝构造函数
1.构造函数:只有单个形参,该形参是对本类类型对象的引用(一般常用const修饰),在用已存在的类类型对象创建新对象时由编译器自动调用。
2.特性:
1.拷贝构造函数是构造函数的一个重载形式。
2. 拷贝构造函数的参数只有一个且必须使用引用传参,使用传值方式会引发无穷递归调用。
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
Date(const Date& d)
//const可以避免在赋值时写反,避免错误
{
_year = d._year;
_month = d._month;
_day = d._day;
}
void print()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1;
return 0;
}
如果不使用引用传参,则会引发无穷递归。而使用引用传参,调用拷贝构造函数需要传参,形参是实参的别名,就直接进入了。
void f(Date d)
{
}
int main()
{
Date d1;
d1.print();
Date d2(d1);
d2.print();
//自定义类型的传值传参,不仅仅是把对象空间的值拷贝过来
//必须要调用拷贝构造函数来完成,否则会有深浅拷贝的问题
f(d2);
return 0;
}
3.若未显示定义,系统生成默认的拷贝构造函数。 默认的拷贝构造函数对象按内存存储按字节序完成拷贝,这种拷贝我们叫做浅拷贝,或者值拷贝。
对于日期类这种拷贝,叫做浅拷贝。
对于Stack这样关注着内存块资源的类,需要自己实现拷贝构造,搞成深拷贝。
五、 赋值操作符重载
1.运算符重载概念:运算符重载是具有特殊函数名的函数,也具有其返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。
(自定义类型可以像内置类型使用运算符)自定义类型在默认情况下是不能使用运算符的,
如果要使用,那么就需要重载运算符。
函数名字为:关键字operator后面接需要重载的运算符符号。
函数原型:返回值类型 operator操作符(参数列表)
2.注意:
1.不能通过连接其他符号来创建新的操作符:比如operator@
2.重载操作符必须有一个类类型或者枚举类型的操作数
3.用于内置类型的操作符,其含义不能改变,例如:内置的整型+,不能改变其含义
作为类成员的重载函数时,其形参看起来比操作数数目少1成员函数的操作符有一个默认的形参this,限定为第一个形参
4…* 、:: 、sizeof 、?: 、. 注意以上5个运算符不能重载。这个经常在笔试选择题中出现。
class Date
{
public:
Date(int year = 1900, 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;
};
//定义全局的 operator==
// 这里会发现运算符重载成全局的就需要成员变量是共有的,那么问题来了,封装性如何保证?
// 这里其实可以用我们后面学习的友元解决,或者干脆重载成成员函数。
bool operator==(Date d1, Date d2)
{
return d1._year == d2._year && d1._month == d2._month && d1._day == d2._day;
}
int main()
{
Date d1;
d1.print();
Date d2(d1);
d2.print();
operator==(d1, d2);
d1 == d2;
return 0;
}
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
void print()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
//d1 = d2;->d1.operator(d2);->d1.operator(&d1,d2)
bool operator==(const Date& d)
{
return _year == d._year && _month == d._month && _day == d._day;
}
//private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1;
d1.print();
Date d2(d1);
d2.print();
d1.operator==(d2);
d1==d2;
return 0;
}
3.赋值运算符的重载:
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
void print()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
//完整的赋值运算符重载
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()
{
Date d1;
d1.print();
Date d2(d1);
d2.print();
/*d1.operator==(d2);
d1==d2;
return 0;*/
Date d3(2021, 4, 17);
//d2 = d3;
//d1 = d2;
d1 =d2 = d3;
d1.print();
return 0;
}
赋值运算符重载的特点:
- 参数类型
- 返回值
- 检测是否自己给自己赋值
- 返回*this
- 一个类如果没有显式定义赋值运算符重载,编译器也会生成一个,完成对象按字节序的值拷贝。
**总结:**operator=跟构造拷贝类似,我们不实现,编译器会默认生成,operator=,会完成按字节的值拷贝(浅拷贝)。
对于Date(日期类),默认生成的构造拷贝和operator=都可以用,不需要去写。
对于Stack这样的类,默认生成的构造拷贝和operator=为浅拷贝都不能用,需要我们自己实现深拷贝。
六、 const成员函数
1.const修饰类的成员函数:将const修饰的类成员函数称之为const成员函数,const修饰类成员函数,实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改。
2.小结论:
- const对象不可以调用非const成员函数
- 非const对象可以调用const成员函数
- const成员函数内不可以调用其它的非const成员函数
- 非const成员函数内可以调用其它的const成员函数
- 能否调用主要看this指针能否传过去,能否传递,主要看的是权限的缩小(ok)还是放大(no)
七、 取地址及const取地址操作符重载
class Date
{
public :
//如果假设想让非const对象取地址,可以return nullptr;
//不return *this;
Date* operator&() //普通对象取地址重载
{
return this ;
}
const Date* operator&()const //const取地址重载
{
return this ;
}
private :
int _year ; // 年
int _month ; // 月
int _day ; // 日
};
这两个运算符一般不需要重载,使用编译器生成的默认取地址的重载即可,只有特殊情况,才需要重载,比如想让别人获取到指定的内容。