本章是继C++类和对象启点之章的后续,进一步深入学习掌握C++类和对象
对于任何一个类来说,在我们不主动写默认成员函数时,会自动生成默认成员函数,总共有6个默认成员函数

在C语言数据结构时,创建结构变量我们经常需要自行调用结构初始化函数,否则可能会报错,为了避免忘记调用初始化函数,C++对此做出了优化,即在创建对象时自动调用初始化函数,也就是构造函数
构造函数是特殊的成员函数,在由类创建对象时对对象的成员变量进行初始化,创建类类型对象时由编译器自动调用,保证每个数据成员都有一个合适的初始值,并且在对象的生命周期内只调用一次
class Date
{
public:
// 1.无参构造函数
Date()
{
cout << "Date()" << endl;
}
// 2.带参构造函数
Date(int year, int month, int day)
{
cout << "Date(int year, int month, int day)" << endl;
_year = year;
_month = month;
_day = day;
}
private:
int _year;
int _month;
int _day;
};
void TestDate()
{
Date d1; // 调用无参构造函数
Date d2(2022, 2, 1); // 调用带参的构造函数
// 注意:如果通过无参构造函数创建对象时,对象后面不用跟括号,否则就成了函数声明
// 以下代码的函数:声明了d3函数,该函数无参,返回一个日期类型的对象
Date d3();
}
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;
}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;
}
对于不传参构造来说,初始化调用无参构造函数或者全缺省构造函数都行,而编译器不能明确知道该调用哪一个(存在二义性/矛盾),也就报错了
class Time
{
public:
Time()//自己写的默认构造函数
{
cout << "Time()" << endl;
_hour = 0;
_minute = 0;
_second = 0;
}
private:
int _hour;
int _minute;
int _second;
};
class Date
{
/*Date()
{
cout << "Date" << endl;
}*/
private:
// 基本类型(内置类型)
int _year;
int _month;
int _day;
// 自定义类型
Time _t;
};
int main()
{
Date d;
return 0;
}
注:如果自定类型的默认构造函数也是编译器默认生成的话,还是相当于不会处理

注:所以一般来说都建议自己写构造函数进行初始化成员变量
对于一般的命名来说在一定的情况中可能或造成命名冲突
class Date
{
public:
Date(int year)
{
// 这里的year到底是成员变量,还是函数形参?指定不明,编译器会根据就近原则选择是函数形参变量
year = year;
}
private:
int year;
};
class Date
{
public:
Date(int year)
{
_year = year;
}
private:
int _year;
};class Date
{
public:
Date(int year)
{
m_year = year;
}
private:
int m_year;
};和构造函数一样,因为一些类型的创建后需要自行释放动态开辟的空间(避免内存泄漏),为了避免忘记,C++也引入了析构函数
析构函数不是完成对象的销毁,局部对象销毁工作是由编译器完成的,而对象在销毁时会自动调用析构函数,完成类的一些资源清理工作(释放动态开辟的空间)
typedef int DataType;
class SeqList
{
public:
SeqList(int capacity = 10)//构造函数
{
_pData = (DataType*)malloc(capacity * sizeof(DataType));
assert(_pData);
_size = 0;
_capacity = capacity;
}
~SeqList()//析构函数
{
cout << "~SeqList()" << endl;
if (_pData)
{
free(_pData); // 释放堆上的空间
_pData = NULL; // 将指针置为空
_capacity = 0;
_size = 0;
}
}
private:
int* _pData;
size_t _size;
size_t _capacity;
};
int main()
{
SeqList sl;
return 0;
}
注:一般来说如果成员变量没有动态开辟的空间,可以不用自己写,编译器自动生成的就足够用
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;
}
在创建对象时,创建一个与一个对象一某一样的新对象 只有单个形参,该形参是对本 类类型对象的引用**(一般常用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;
}
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;
};
int main()
{
Date d1;
// 这里d2调用的默认拷贝构造完成拷贝,d2和d1的值也是一样的。
Date d2(d1);
return 0;
}class String
{
public:
String(const char* str = "xxxx")
{
_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);
}当两个同类对象的成员指针变量同时指向一个动态开辟的空间,如果一个对象对这空间进行释放,而另一个对象却依旧保存着该地址(野指针),如果进行操作该空间(再次free等)则会造成程序崩溃

C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似
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(2018, 9, 26);
Date d2(2018, 9, 27);
cout << (d1 == d2) << endl;
}
对于友元的知识在类和对象最终章会详细讲解,这里只做了解
注:一般来说都建议将操作符重载设定在类里面(重载成成员函数)
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(2018, 9, 26);
Date d2(2018, 9, 27);
cout << (d1 == d2) << endl;
}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;
}
Date& operator=(const Date& d)
{
if (this != &d)//避免对象赋值自己
{
_year = d._year;
_month = d._month;
_day = d._day;
}
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1(2022,2,1);//调用构造函数
Date d2;
d2=d1;//调用赋值操作符重载
return 0;
}
在以目前学会的类和对象知识,我们可以上手实现一个Date类出来
class Date
{
public:
// 获取某年某月的天数
int GetMonthDay(int year, int month);
// 全缺省的构造函数
Date(int year = 1900, int month = 1, int day = 1);
// 拷贝构造函数
// d2(d1)
Date(const Date& d);
// 赋值运算符重载
// 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);
// >=运算符重载
inline 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;
};将const修饰的类成员函数称之为const成员函数,const修饰类成员函数,实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改

class Date
{
public:
void Display()
{
cout << "Display ()" << endl;
cout << "year:" << _year << endl;
cout << "month:" << _month << endl;
cout << "day:" << _day << endl << endl;
}
void Display() const
{
cout << "Display () const" << endl;
cout << "year:" << _year << endl;
cout << "month:" << _month << endl;
cout << "day:" << _day << endl << endl;
}
private:
int _year; // 年
int _month; // 月
int _day; // 日
};
void Test()
{
Date d1;
d1.Display();
const Date d2;
d2.Display();
}不能,const对象即不能修改,而非const成员函数则需要对象能可读可写(权限的扩大)
能,非const对象即是可读可写,调用const成员函数则只需要对象能可读就行(权限的缩小)
不能,const成员函数即无法通过this修改对象不能修改,而非const成员函数则需要对象能可读可写(权限的扩大)
能,非const成员函数即能通过this修改对象可读可写,调用const成员函数则只需要对象能可读就行(权限的缩小)
这两个默认成员函数一般不用重新定义 ,编译器默认会生成
class Date
{
public:
Date* operator&()
{
return this;
}
const Date* operator&()const
{
return this;
}
private:
int _year; // 年
int _month; // 月
int _day; // 日
};这两个运算符一般不需要重载,使用编译器生成的默认取地址的重载即可,只有特殊情况,才需要重载,比如想让别人获取到指定的内容(不想让别人找到对象地址)!