C++入门篇——类与对象(中篇)
该篇是介绍类与对象的核心篇章,主要介绍类的默认成员函数,操作符重载以及const成员函数等。
1 类的六个默认成员函数
我们知道一个类如果什么都没有,叫做空类。
但是空类并不是什么都没有,编译器会默认生成六个默认成员函数。
如果用户没有显示实现默认成员函数,编译器会自动生成。
2 构造函数
2.1 概念
构造函数主要是负责对类中完成初始化工作,我们通常对一个类中的成员属性,需要进行初始化,比如下面这个日期类。
class Date
{
public:
void Init(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
}
void Print()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
private:
int _year; // 年
int _month; // 月
int _day; // 日
int a;
};
对于Date类,可以通过一个共有方法给对象设置,但是每次创建对象的时候都需要调用该方法来设置,C++的类就设计了一个构造函数,构造函数的名字与类名相同,没有返回值,创建类类型对象时由编译器自动调用,保证每个数据成员都有一个合适的初始值。
那么,这段代码可以改造为以下形式:
class Date
{
public:
Date(int year,int month,int day)
{
_year = year;
_month = month;
_day = day;
}
void Print()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
private:
int _year; // 年
int _month; // 月
int _day; // 日
int a;
};
2.2 特性
- 函数名与类名相同。
- 无返回值,并不是说明返回值是void
- 对象实例化时自动调用
- 可以进行重载。
- 如果不显示实现,编译器会自动生成一个默认的构造函数。
关于编译器自动生成的构造函数,实际上有什么用呢?
class Date
{
public:
/*Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
}*/
void Print()
{
cout << _year << "-" << _month << "-" << _day << endl;
}
private:
int _year; // 年
int _month; // 月
int _day; // 日
int a;
};
int main()
{
Date d;
d.Print();
}
我们发现,编译器生成的默认构造函数,并不会为我们日期类型的成员进行一个初始化,依旧输出随机值。这是因为C++早期实现的时候,并没有对内置类型成员初始化,这是C++的一个缺陷,在C++11之后,对这个缺陷做了一个补丁,对于自定义类型的成员,默认构造函数会去调用自定义类型的构造函数。
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;
};
这里的Date类中包含了Time类,而在对Date使用默认构造函数时,会去调用Time的默认构造函数。而内置类型,比如int类型,就不会去进行初始化,那么C++11因此对这个缺陷打了补丁,即内置类型成员变量在类中声明时,可以给默认值。
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 = 1979;
int _month = 1;
int _day = 1;
// 自定义类型
Time _t;
};
我们知道,构造函数也可以进行重载,无参的构造函数和全缺省的构造函数均为默认构造函数。并且只能有一个。注意:显示编写了半缺省的构造函数,编译器不会提供默认构造函数,除非显示实现默认构造函数。或者使用C++11的=default
。
class Date
{
public:
Date(int year)
{
_year = year;
}
private:
// 基本类型(内置类型)
int _year;
int _month;
int _day;
};
int main()
{
Date d;
}
修正后:
class Date
{
public:
Date() = default; // 或者 Date() { }
Date(int year)
{
_year = year;
}
private:
// 基本类型(内置类型)
int _year;
int _month;
int _day;
};
int main()
{
Date d;
}
3 析构函数
3.1 概念
通过前面对构造函数的理解,其实析构函数,是对对象进行销毁工作。我们知道,在对象创建的时候会自动调用构造函数,那么对象销毁的时候会自动调用析构函数。
3.2 特性
- 析构函数的名字是在类名前加上~
- 无参数无返回值
- 一个类只能有一个析构函数。不能进行重载,如果没有显示实现,系统同样也会默认生成一个析构函数。
- 对象的生命周期结束后,自动调用析构函数。
使用场景通常是对类中已经申请的资源,进行回收,否则会造成内存泄漏。我们在实现一个栈的时候,通常需要malloc去申请一块堆区的内存空间,但是程序员很容易会忘记进行内存释放,因此造成内存泄漏,可以对此进行改进。
typedef int DataType;
class Stack
{
public:
Stack(size_t capacity = 3)
{
_array = (DataType*)malloc(sizeof(DataType) * capacity);
if (_array == NULL)
{
perror("malloc failed");
return;
}
_capacity = capacity;
_size = 0;
}
~Stack()
{
free(_array);
_array = NULL;
_capacity = 0;
_size = 0;
}
private:
DataType* _array;
size_t _capacity;
size_t _size;
};
4 拷贝构造函数
4.1 概念
我们在创建对象的时候,可能会需要一个与该对象一模一样的新对象。比如现实生活中的双胞胎,他们的年龄和出生日期都是相同的,我们会使用一个已有的对象拷贝出另一个对象。那么因此,会使用到拷贝构造函数。
拷贝构造函数:只有单个形参,该形参是对本类类型对象的引用(一般常用const修饰),在用已存在的类类型
对象创建新对象时由编译器自动调用。
4.2 特性
- 拷贝构造函数是一个特殊的成员函数,其实是构造函数的一个重载形式。
- 参数只能有一个并且必须是类类型对象的引用,使用传值方式编译器直接报错,因为会引发
无穷递归调用。- 建议在参数前加上const修饰。
使用方法:
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
// Date(const Date d) // 错误写法:编译报错,会引发无穷递归
Date(const Date& d) // 正确写法
{
_year = d._year;
_month = d._month;
_day = d._day;
}
private:
int _year;
int _month;
int _day;
};
我们知道以传值的方式去传参,编译器会生成一个局部变量,而在这个拷贝构造函数中,如果未使用引用传递,在生成局部变量后,又会再生成一个,因此,会造成不断递归。
如果我们没有定义一个拷贝构造函数,编译器会自动为我们生成一个。一般来说,编译器自动生成的拷贝构造函数,会将给定对象中每个非static成员拷贝到正在创建的对象中。这种拷贝,称为浅拷贝。
但是,如果在遇到成员中需要进行malloc分配资源的时候,这种浅拷贝会带来一定的问题。比如:
typedef int DataType;
class Stack
{
public:
Stack(size_t capacity = 3)
{
_array = (DataType*)malloc(sizeof(DataType) * capacity);
if (_array == NULL)
{
perror("malloc failed");
return;
}
_capacity = capacity;
_size = 0;
}
~Stack()
{
free(_array);
_array = NULL;
_capacity = 0;
_size = 0;
}
private:
DataType* _array;
size_t _capacity;
size_t _size;
};
int main()
{
Stack s1;
Stack s2(s1);
}
这段代码,会出现什么问题呢?我们来分析一下两个对象的内存布局。
对于这种问题最好的办法,就是实现一个深拷贝,这里暂时不作具体实现。
5 赋值操作符重载
5.1 运算符重载
C++为了增强代码的可读性,引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其返回值类
型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。
函数原型:返回值类型 operator 操作符符号
注意:
- 不能通过连接其他符号来创建新的操作符:比如operator@
- 重载操作符必须有一个类类型参数
- 对于内置类型的运算符,不可以改变其含义,比如加法操作符,不能重载为减法实现。
- 作为类成员函数重载时,其形参看起来比操作数数目少1,因为成员函数的第一个参数为隐藏的this
".*" "::" "sizeof" "?:" "."
,这五个操作符不可以重载。
5.2 赋值运算符重载
格式如下:
- 参数类型:const T&,引用传递可以提高效率。
- 返回值类型:T &,返回引用同样也可以提高效率,并且可以实现连续赋值。
- 检查是否对自己赋值。
- 返回*this。
比如实现日期类的赋值运算符重载版本:
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;
}
return *this;
}
private:
int _year;
int _month;
int _day;
};
赋值运算符重载,如果不显示实现,编译器也会默认生成一个版本,同样与拷贝构造函数相比,也会有深浅拷贝的问题。
6 日期类的完整实现
我们通过学习了一系列的默认成员函数,以及操作符重载,是否可以实现一个完整的日期类,这个日期类需要和内置类型一样,能够使用运算符去对日期类进行操作。
date.h
class Date {
friend ostream& operator<<(ostream& out, const Date& d);
friend istream& operator>>(istream& in, Date& d);
public:
// 获取某年某月的天数
int GetMonthDay(int year, int month)
{
static int days[13] = { 0, 31,28,31,30,31,30,31,31,30,31,30,31 };
// 如果是闰年且2月的话,则返回29天
if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
return 29;
return days[month];
}
bool CheckDate()
{
if (_year >= 1
&& _month > 0 && _month < 13
&& _day > 0 && _day <= GetMonthDay(_year, _month))
return true;
else
return false;
}
// 全缺省的构造函数
Date(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
assert(CheckDate());
}
// 拷贝构造函数
Date(const Date& d);
~Date();
// 赋值运算符重载
Date& operator=(const Date& d);
// 析构函数
// 日期+=天数
Date& operator+=(int day);
// 日期+天数
Date operator+(int day) const;
// 日期-=天数
Date& operator-=(int day);
// 日期-天数
Date operator-(int day) const;
// 前置++
Date& operator++();
// 后置++
Date operator++(int);
// 后置--
Date operator--(int);
// 前置--
Date& operator--();
// >运算符重载
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;
// !=运算符重载
bool operator != (const Date& d) const;
// 日期-日期 返回天数
int operator-(const Date& d) const;
// 打印数据
void Print() const;
private:
int _year;
int _month;
int _day;
};
// 流插入重载
inline ostream& operator<<(ostream& cout, const Date& d)
{
cout << d._year << "年" << d._month << "月" << d._day << "日" << endl;
return cout;
}
// 流提取重载
inline istream& operator>>(istream& cin, Date& d)
{
cin >> d._year >> d._month >> d._day;
assert(d.CheckDate());
return cin;
}
Date.cpp
// date.cpp
#include "date.h"
// 拷贝构造函数
Date::Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
// 赋值运算符重载
Date& Date::operator=(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
return *this;
}
// 析构函数
Date::~Date()
{}
// 日期+=天数
Date& Date::operator+=(int day)
{
_day += day;
while (_day > GetMonthDay(_year, _month))
{
_day -= GetMonthDay(_year, _month);
++_month;
if (_month == 13)
{
_month = 1;
_year++;
}
}
return *this;
}
// 日期+天数
Date Date::operator+(int day) const
{
Date ret(*this);
ret += day;
return ret;
}
// 日期-=天数
Date& Date::operator-=(int day)
{
_day -= day;
while (_day < 1)
{
_day += GetMonthDay(_year, _month);
--_month;
if (_month == 0)
{
_month = 12;
_year--;
}
}
return *this;
}
// 日期-天数
Date Date::operator-(int day) const
{
Date ret(*this);
ret -= day;
return ret;
}
// 前置++
Date& Date::operator++()
{
return *this += 1;
}
// 后置++
Date Date::operator++(int)
{
Date ret(*this);
++(*this);
return ret;
}
// 前置--
Date& Date::operator--()
{
return *this -= 1;
}
// 后置--
Date Date::operator--(int)
{
Date ret(*this);
--(*this);
return ret;
}
// >运算符重载
bool Date::operator>(const Date& d) const
{
// 优先比较年份,在比较月份,在比较日
if((_year > d._year)
|| (_year == d._year && _month > d._month)
|| (_year == d._year && _month == d._month && _day > d._day)
)
return true;
else
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);
}
// !=运算符重载
bool Date::operator != (const Date& d) const
{
return !(*this == d);
}
// 日期-日期 返回天数
int Date::operator-(const Date& d) const
{
int flag = 1;
Date max = *this;
Date min = d;
if (*this < d)
{
max = d;
min = *this;
flag = -1;
}
int n = 0;
while (min != max)
{
++min;
++n;
}
return n*flag;
}
void Date::Print() const
{
cout << _year << "年" << _month << "月" << _day << "日" << endl;
}
7 const 成员
将const修饰的“成员函数”称之为const成员函数,const修饰类成员函数,实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改。
我们通常打印类中的成员是不需要修改类成员的值的。所以可以将改成员函数设为const成员函数。const放在函数的尾部。
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
void Display() const
{
cout << "year:" << _year << "\tmonth:" << _month << "\tday:" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
8 取地址及const取地址操作符重载
这两个默认成员函数一般不用重新定义 ,编译器默认会生成。
class Date
{
public :
Date* operator&()
{
return this ;
}
const Date* operator&()const
{
return this ;
}
private :
int _year ; // 年
int _month ; // 月
int _day ; // 日
};
饰的“成员函数”称之为const成员函数,const修饰类成员函数,实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改。
我们通常打印类中的成员是不需要修改类成员的值的。所以可以将改成员函数设为const成员函数。const放在函数的尾部。
class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
void Display() const
{
cout << "year:" << _year << "\tmonth:" << _month << "\tday:" << _day << endl;
}
private:
int _year;
int _month;
int _day;
};
8 取地址及const取地址操作符重载
这两个默认成员函数一般不用重新定义 ,编译器默认会生成。
class Date
{
public :
Date* operator&()
{
return this ;
}
const Date* operator&()const
{
return this ;
}
private :
int _year ; // 年
int _month ; // 月
int _day ; // 日
};
这两个运算符一般不需要重载,使用编译器生成的默认取地址的重载即可,只有特殊情况,才需要重载,比如想让别人获取到指定的内容!