类和对象——part2

类和对象的内容真的好多啊

 

目录 

1. 类的6个默认成员函数

2. 构造函数

2.1概念

2.2特性

3. 析构函数

3.1概念

3.2特性

4. 拷贝构造函数

4.1概念

4.2特性

5. 赋值操作符重载

5.1运算符重载

5.2赋值运算符重载

6. 日期类的实现

7. const成员函数

8. 取地址及const取地址操作符重载


1. 类的6个默认成员函数

如果一个类中什么成员都没有,简称为空类。空类中什么都没有吗?并不是的,任何一个类在我们不写的情况下,都会自动生成下面6个默认成员函数。

2. 构造函数

2.1概念

构造函数是一个特殊的成员函数,名字与类名相同,创建类类型对象时由编译器自动调用,保证每个数据成员都有 一个合适的初始值,并且在对象的生命周期内只调用一次。

2.2特性

构造函数是特殊的成员函数,需要注意的是,构造函数的虽然名称叫构造,但是需要注意的是构造函数的主要任务并不是开空间创建对象,而是初始化对象
其特征如下:
1. 函数名与类名相同。
2. 无返回值。
3. 对象实例化时编译器自动调用对应的构造函数。
4. 构造函数可以重载。

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();
}

5. 如果类中没有显式定义构造函数,则C++编译器会自动生成一个无参的默认构造函数,一旦用户显式定义编译器将不再生成

class Date
{
public:
private:
	int _year;
	int _month;
	int _day;
};
void TestDate()
{
	Date d1; // 调用无参构造函数
}

没有定义构造函数,对象也可以创建成功,因此此处调用的是编译器生成的默认构造函数

但是如果像下面这样就不行了,因为定义了有参数的构造函数而没有定义默认构造函数

class Date
{
public:
	Date(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}
private:
	int _year;
	int _month;
	int _day;
};
void TestDate()
{
	Date d1; // 调用无参构造函数
}


6. 无参的构造函数和全缺省的构造函数都称为默认构造函数,并且默认构造函数只能有一个。注意:无参构造函数、全缺省构造函数、我们没写编译器默认生成的构造函数,都可以认为是默认成员函数。

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;
};

像这样的话会运行失败,因为在调用默认构造函数的时候对构造函数的调用存在歧义

7.内置类型/基本类型:int、char、double、指针
自定义类型:class、struct定义的对象
默认生成的构造函数对内置类型成员变量不做处理,对于自定义类型变量才会处理。
处理的方式就是调用这个自定义

下面来写代码验证一下

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;
};
class Test
{
public:
	int _a;
	char _b;
	Date _c;
};
void TestDate()
{
	Test d1; // 调用无参构造函数
}
int main()
{
	TestDate();
	return 0;
}

 那么这种机制有没有可用之处呢?当然有,比如类中的成员全是自定义类型我们就可以使用默认的构造函数。如果有内置类型或者需要显示传参初始化,那么我们就要自己实现构造函数。


 

3. 析构函数

3.1概念

析构函数:与构造函数功能相反,析构函数不是完成对象的销毁,局部对象销毁工作是由编译器完成的。而对象在销毁时会自动调用析构函数,完成类的一些资源清理工作
注意这里是资源清理,如果我们的构造函数只是对我们的内置类型进行了一些初始化,那我们并没有什么需要清理的,因为这些成员变量出了类的作用域就会随着类而一起销毁,因此也就没有写析构函数的必要。但是如果在构造函数里面用malloc或者new在堆中开辟了空间,那么我们要在析构函数中释放掉,这时候析构函数就是必要的。

3.2特性

析构函数是特殊的成员函数。
其特征如下:
1. 析构函数名是在类名前加上字符 ~。
2. 无参数无返回值。
3. 一个类有且只有一个析构函数。若未显式定义,系统会自动生成默认的析构函数。
4. 对象生命周期结束时,C++编译系统系统自动调用析构函数。
5. 关于编译器自动生成的析构函数,是否会完成一些事情呢?下面的程序我们会看到,编译器生成的默认析构函数,对会自定类型成员调用它的析构函数。和上面的构造一样

4. 拷贝构造函数

4.1概念

拷贝构造函数:只有单个形参,该形参是对本类类型对象的引用(一般常用const修饰),在用已存在的类型对象创建新对象时由编译器自动调用

4.2特性

拷贝构造函数也是特殊的成员函数,其特征如下:
1. 拷贝构造函数是构造函数的一个重载形式。
2. 拷贝构造函数的参数只有一个且必须使用引用传参使用传值方式会引发无穷递归调用

当我们使用传值方式的时候,传值传参调用拷贝构造函数,而拷贝构造函数又是传值,又需要调用拷贝构造,因此无限循环。

我们不写拷贝构造编译器会自动生成一个拷贝构造函数
1.对内置类型完成值拷贝/浅拷贝
但是如果有指针类型(注意不是数组,数组是可以的)(数据存在堆),那么拷贝后两个对象的指针将指向同一块内存,当析构的时候就会析构两次,程序崩溃。修改指针指向内容的时候也会互相影响因此要自己实现拷贝构造,进行深拷贝

2.有自定义类型的成员,去调用这个成员的拷贝构造
结论:一般的类,自己生成拷贝构造就够用了,只有像栈这样的类,自己直接管理资源(指针),需要自己实现深拷贝

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 = "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);
}

 

5. 赋值操作符重载

5.1运算符重载

C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其返回值类型,函数名字以及参数列表,其返回值类型与参数列表与普通的函数类似。
函数名字为:关键字operator后面接需要重载的运算符符号。
函数原型:返回值类型 operator操作符(参数列表)
PS!!!:

1.不能通过连接其他符号来创建新的操作符:比如operator@
2.重载操作符必须有一个类类型或者枚举类型的操作数
3.用于内置类型的操作符,其含义不能改变,例如:内置的整型+,不能改变其含义
4.作为类成员的重载函数时,其形参比操作数数目少1,因为类的非静态成员函数参数里隐含着一个this指针
5.操作符有一个默认的形参this,限定为
第一个形参
6.  .* 、:: 、sizeof 、?: 、. 注意以上5个运算符不能重载。这个经常在笔试选择题中出现。

 

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);
	Date d3(2018, 9, 26);
	if (d1 == d2)
		cout << "相等" << endl;
	else
		cout << "不相等" << endl;
	if(d1==d3)
		cout << "相等" << endl;
	else
		cout << "不相等" << endl;
}
int main()
{
	Test();
	return 0;
}

5.2赋值运算符重载

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;
};

PS:

1. 参数类型(和拷贝一样是对象的别名)
2. 返回值(对象的引用)
3. 检测是否自己给自己赋值
4. 返回*this
5. 一个类如果没有显式定义赋值运算符重载,编译器也会生成一个,完成对象按字节序的值拷贝

当然,和拷贝构造一样存在深浅拷贝的问题


 

6. 日期类的实现

class Date
{
private:
	int _year;
	int _month;
	int _day;
public:
	// 获取某年某月的天数
	int GetMonthDay(int year, int month)
	{
		const static int arr_month[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
		if (year >= 1 && month >= 1 && month <= 12)
		{
			if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) && month == 2)
				return 29;
			return arr_month[month];
		}
	}
	//全缺省构造函数
	Date(int year = 2000, int month = 1, int day = 1)
	{
		if (year >= 1 &&
			month <= 12 && month >= 1 &&
			day >= 1 && day <= GetMonthDay(year, month))
		{
			_year = year;
			_month = month;
			_day = day;
		}
		else
		{
			cout << "日期非法" << endl;
		}
	}
	// 拷贝构造函数
	Date(const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}

	// 赋值运算符重载
	// d2 = d3 -> d2.operator=(&d2, d3)
	Date& operator=(const Date& d)
	{
		if (this != &d)
		{
			_year = d._year;
			_month = d._month;
			_day = d._day;
		}
		return *this;
	}
	// 析构函数
	~Date() {}

	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
	// 日期+=天数
	Date& operator+=(int day)
	{
		if (day < 0)
		{
			return *this -= -day;
		}
		_day += day;
		while (_day > GetMonthDay(_year, _month))
		{
			_day -= GetMonthDay(_year, _month);
			_month++;
			if (_month > 12)
			{
				_year++;
				_month = 1;
			}
		}
		return *this;
	}
	// 日期+天数
	Date operator+(int day)const
	{
		Date ret(*this);
		ret += day;
		return ret;
	}
	// 日期-天数

	Date operator-(int day)const
	{
		Date ret(*this);
		ret -= day;
		return ret;
	}
	// 日期-=天数
	// d -= 100
	// d -= -100
	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;
	}

	// 前置++
// ++d -> d.operator++(&d)
	Date& operator++()
	{
		*this += 1;
		return *this;
	}
	Date operator++(int) // 后置
	{
		Date tmp(*this);
		*this += 1;
		return tmp;
	}

	Date& operator--()     // 前置
	{
		*this -= 1;
		return *this;
	}
	Date operator--(int) // 后置
	{
		Date tmp(*this);
		*this -= 1;
		return tmp;
	}
	// d1 > d2
  // >运算符重载
	bool operator>(const Date& d) const
	{
		if (_year > d._year)
		{
			return true;
		}
		else if (_year == d._year)
		{
			if (_month > d._month)
			{
				return true;
			}
			else if (_month == d._month)
			{
				if (_day > d._day)
				{
					return true;
				}
			}
		}
		return false;
	}

	// ==运算符重载
	bool operator==(const Date& d) const
	{
		return _year == d._year
			&& _month == d._month
			&& _day == d._day;
	}

	// >=运算符重载
	bool operator >= (const Date& d) const
	{
		return *this > d || *this == d;
	}



	// <运算符重载

	bool operator < (const Date& d) const
	{
		return !(*this >= d);
	}



	// <=运算符重载

	bool operator <= (const Date& d) const
	{
		return !(*this > d);
	}

	// !=运算符重载
	bool operator != (const Date& d) const
	{
		return !(*this == d);
	}

	int operator-(Date& d)const;
};

int Date::operator-(Date& d)const
{
	int flag = 1;
	Date max = *this;
	Date min = d;
	if (*this < d)
	{
		min = *this;
		max = d;
		flag = -1;
	}

	int n = 0;
	while (min != max)
	{
		++n;
		++min;
	}

	return n * flag;
}

 

7. const成员函数

将const修饰的类成员函数称之为const成员函数,const修饰类成员函数,实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改。

1. const对象可以调用非const成员函数吗?  不可以
2. 非const对象可以调用const成员函数吗?  可以
3. const成员函数内可以调用其它的非const成员函数吗?   不可以
4. 非const成员函数内可以调用其它的const成员函数吗?   可以
 

8. 取地址及const取地址操作符重载

这两个默认成员函数一般不用重新定义 ,编译器默认会生成
 

class Date
{
public:
	Date* operator&()
	{
		return this;
	}
	const Date* operator&()const
	{
		return this;
	}
private:
	int _year; // 年
	int _month; // 月
	int _day; // 日
};

这两个运算符一般是不需要重载的!!


终于

 

  • 10
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

printf("雷猴");

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值