构造函数 析构函数 拷贝构造函数 运算符重载 this 指针

   目录

构造函数:

    析构函数:

拷贝构造函数

this指针 :


构造函数:

      构造函数就是你在定义一个类对象的时候 它会自动调用 的一个函数,默认调用得到是无参构造函数,当然了 你也可以定义 缺省构造函数 引用构造函数, 构造函数的特征可以概括如下:1函数名与类名相同。

2. 无返回值。

3. 对象实例化时编译器自动调用对应的构造函数。

4. 构造函数可以重载。 同时要明确构造函数的作用:务并不是开空间创建对象,而是初始化对象

    析构函数:

   析构函数也容易理解 就是定义完一个类对象后 当这个对象的声明周期到了后 也会自动调用的一个函数,(与构造函数功能相反,析构函数不是完成对象的销毁,局部对象销毁工作是由编译器完成的。而 对象在销毁时会自动调用析构函数,完成类的一些资源清理工作。)

 析构函数的特征如下:1. 析构函数名是在类名前加上字符 ~。

2. 无参数无返回值。

3. 一个类有且只有一个析构函数。若未显式定义,系统会自动生成默认的析构函数。(析构函数是没有函数重载的)

4. 对象生命周期结束时,C++编译系统系统自动调用析构函数

拷贝构造函数

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

 可以简单理解为 你先在有一个class类 你用这个类创建了一个对象。 创建完成后,你又创建了对象,当你想用第一个对象初始化第二个对象的时候 就用到了拷贝构造函数,

运算符重载:  形式上是 :  类名 operator 加重载的运算符 (参数列表)

很简单 比如定了一个类的两个对象 d1 d2  你想d1+d2可以直接加吗? 是不可以的 你需要先进行运算符的重载。

this指针 :

  在c++中 每一个对象都能通过this指针来访问自己的地址,this指针是所有的成员函数的隐含参数,因此 在成员函数内部,可以用来调用对象。

给一个简单的小例题 包含了上述几种概念的用法 代码拿过来直接就可以跑、

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;

class Date {

public:

	int getdayfun(int year, int month)
	{
		int day[] = { 0,31,28,31,30,31,30,31,30,31,30,31,31,31 };
		if (month == 2 && ( year%4==0 && year%100!= 0 || year %400 ==0))
		{
			day[month] += 1;
		}
		return day[month];
	}

	 Date(int year=0,int month=1,int day=1)
	{
		 this->_year = year;
		 if (month <= 12 && day <= getdayfun(year, month))
		 {
			 this->_month = month;
			 this->_day = day;
		 }
		 else
		 {
			 cout << "输入错误" << endl;
		 }
	}
	 ~Date()
	 {
		// cout << "调用析构函数" << endl;
	 }
	 Date(const Date& d) //引用构造 无参构造 缺省构造
	 {
		 this->_year = d._year;
		 this->_month = d._month;
		 this->_day = d._day;
	 
	 }
	 bool operator<(const Date& d)
	 {
		 if (this->_year < d._year)
		 {
			 return true;
		 }
		 else if (this->_year == d._year && this->_month < d._month)
		 {
			 return true;
		 }
		 else if (this->_year == d._year && this->_month == d._month && this->_day < d._day)
		 {
			 return true;
		 }
		 else
		 {
			 return false;
		 }
	 
	 }
	 bool operator==(const Date& d)
	 {
		 return this->_year == d._year && this->_month == d._month && this->_day == d._day;
	 }
	 bool operator<=(const Date& d)
	 {
		 return (*this < d || *this == d);
	 }
	 bool operator >(const Date& d)
	 {
		 return !(*this <= d);
	 }
	 bool operator >=(const Date& d)
	 {
	 
		 return !(*this < d);
	 }
	 bool operator !=(const Date& d)
	 {
		 return !(*this == d);
	 }

	 Date operator +(const int a)
	 { 
		 Date ret(*this);
		 ret._day += a;
		 while (ret._day > getdayfun(ret._year, ret._month))
		 {
			 if (ret._month < 12)
			 {
				 ret._month += 1;
				 ret._day -= getdayfun(ret._year, ret._month);
			 }
			 else
			 {
				 ret._year += 1;
				 ret._month = 1;
				 ret._day -= getdayfun(ret._year, ret._month);
			 }
		 }
		 return ret;
	 }
	 Date operator +=(const int a)
	 {

		 _day += a;
		 while (_day > getdayfun(_year, _month))
		 {
			 if (_month < 12)
			 {
				 _month += 1;
				 _day -= getdayfun(_year, _month);
			 }
			 else
			 {
				 _year += 1;
				 _month = 1;
				 _day -= getdayfun(_year, _month);
			 }
		 }

		 return *this;
      }
 void print()
 {
	 cout << this->_year << "-" << this->_month << "-" << this->_day << endl;
 }
private:
	int _year;
	int _month;
	int _day;
};

int main()
{
		
	Date d1(2000,3,25);
	d1.print();
	Date d2(1, 3, 31);
	d2.print();
	Date d3(d1);
	d3.print();
	//操作符重载
	//cout << (d3 < d1) << endl;
	//cout << (d3 == d1) << endl;
	//cout << (d3 > d1) << endl;
	//判断输入日期的正确性
	Date d4 = d3 + 555;
	 d3 += 5;
	 d3.print();
	d4.print();




	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值