目录
一.构造函数
1.概念
2.特性
class Date
{
public :
// 1.无参构造函数
Date ()
{}
// 2.带参构造函数
Date (int year, int month , int day )
{
_year = year ;
_month = month ;
_day = day ;
}
private :
int _year ;
int _month ;
int _day ;
};
void TestDate()
{
Date d1; // 调用无参构造函数
Date d2 (2015, 1, 1); // 调用带参的构造函数
}
①
②
// 注意:如果通过无参构造函数创建对象时,对象后面不用跟括号,否则就成了函数声明
// 以下代码的函数:声明了d3函数,该函数无参,返回一个日期类型的对象
Date d3();
二.析构函数
1.概念
2.特性
typedef int DataType;
class SeqList
{
public :
SeqList (int capacity = 10)
{
_pData = (DataType*)malloc(capacity * sizeof(DataType));
assert(_pData);
_size = 0;
_capacity = capacity;
}
~SeqList() //完成清理工作,防止造成内存泄漏
{
if (_pData)
{
free(_pData ); // 释放堆上的空间
_pData = NULL; // 将指针置为空
_capacity = 0;
_size = 0;
}
}
private :
int* _pData ;
size_t _size;
size_t _capacity;
};
⑤关于编译器自动生成的析构函数,是否会完成一些事情呢?下面的程序我们会看到,编译器生成的默认析构函数,对会自定类型成员调用它的析构函数 ( 最后打印 ~String() )
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; //默认调用String的析构函数
int _age;
};
int main()
{
Person p;
return 0;
}
小补充:
三.拷贝构造
1.概念
构造函数:只有单个形参,该形参是对本类的 类型对象的引用(一般常用const修饰),用已存在的类的 类型对象创建新对象时由编译器自动调用。
class Date
{
public:
Date(int year = 1900, 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;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1;
Date d2(d1); //传一个对象
return 0;
}
2.特性
要调用拷贝构造函数就需要先传参,如果传参使用传值传参,那么在传参过程中又需要进行对象的拷贝构造,如此循环往复,最终引发无穷递归调用。
// 这里会发现下面的程序会崩溃掉?这里就需要深拷贝去解决。
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;
};
int main()
{
String s1("hello");
String s2(s1);
}
四.赋值运算符重载
1.运算符重载
Date d1(0 , 0 , 0);Date d2(1 , 1 , 1 );d1 == d2 ; // 可读性高(书写简单)Compare(d1, d2);// 可读性差(书写麻烦)
// 全局的operator==
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
{
_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;
}
void Test ()
{
Date d1(2022, 9, 26);
Date d2(2022, 9, 27);
cout<<(d1 == d2)<<endl;
}
(2)运算符重载作为类的成员函数
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
{
_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;
};
void Test ()
{
Date d1(2022, 9, 26);
Date d2(2022, 9, 27);
cout<<(d1 == d2)<<endl;
}
2.补充
④
⑤
五.const成员
1.概念
将const修饰的类成员函数称之为const成员函数,const修饰类成员函数,实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改。
2.const对象和const成员函数
①. const对象可以调用非const成员函数吗? //不能 ,权限的放大
②. 非const对象可以调用const成员函数吗?//可以,权限缩小
③. const成员函数内可以调用其它的非const成员函数吗?//不能,权限放大
④. 非const成员函数内可以调用其它的const成员函数吗?//可以,权限缩小
3.取地址及const取地址操作符重载
class Date
{
public :
Date* operator&()
{
return this ;
}
const Date* operator&()const
{
return this ;
}
private :
int _year ; // 年
int _month ; // 月
int _day ; // 日
};