C++日期类

1.Date中的构造函数

在C++中,有一种特殊的成员函数,它的名字和类名相同,没有返回值,不需要用户显式调用(用户也不能调用),而是在创建对象时自动执行。这种特殊的成员函数就是构造函数,但是对于日期类,这里默认构造函数什么也不做,如果你创建了一个Date d1,系统调用了默认构造函数,你会发现在这里年月日都是随机值,我们为了让d1在初始化的时候就有一个确认的日期,所以自己要写一个全缺省的构造函数,我们在函数说明中给一个确定的值,让每个创建出来的日期类变量都有一个默认的初始值1900.1.1

2.Date中的析构函数

析构函数的作用同构造函数是对应的,这里是销毁某些特定的成员变量,如我们用malloc申请出来的内存,如果不在析构函数中做特定的操作,就很可能导致内存泄漏,不过在Date中的成员并不需要特殊的去释放他,他们生长在栈上,会随函数栈桢的结束自动释放掉,所以这里就可以使用默认的析构函数,我们可以不用书写。

3.Date中的拷贝构造函数

如果类的设计者不写复制构造函数,编译器就会自动生成复制构造函数。大多数情况下,其作用是实现从源对象到目标对象逐个字节的复制,即使得目标对象的每个成员变量都变得和源对象相等。编译器自动生成的复制构造函数称为“默认拷贝构造函数”。
拷贝构造函数必须使用传引用的方式返回,否则会造成无限递归

源码实现

**```

// An highlighted block
#include<iostream>
using namespace std;
class Date
{
	friend ostream& operator<<(ostream& out, const Date& d);
public:
	Date(int year = 1900, int month = 1, int day = 1)
	{
		m_year = year;
		m_month = month;
		m_day = day;
	}
	Date(const Date& d)
	{
		m_year = d.m_year;
		m_month = d.m_month;
		m_day = d.m_day;
	}
	//Date& operator=(const Date &d);
	~Date()
	{}
public:
	bool IsLeap(int year)
	{
		if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
			return true;
		return false;
	}
	int GetDayByYearMonth(int year, int month)
	{
		int days[13] = { 29, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		if (month == 2)
		{
			if (IsLeap(year))
				month = 0;
		}
		return days[month];
	}
public:
	Date operator+(int n) //日期+n天
	{
		int year = m_year;
		int month = m_month;
		int day = m_day;
		int days = GetDayByYearMonth(year, month);

		while (day + n > days)
		{
			month++;
			if (month > 12)
			{
				year++;
				month = 1;
			}
			n -= days;
			days = GetDayByYearMonth(year, month);
		}
		day += n;
		return Date(year, month, day);
	}
	Date& operator+=(int n)
	{
		Date tmp = *this + n;
		*this = tmp;
		return *this;
	}
	Date& operator++()
	{
		*this += 1;
		return *this;
	}
	Date operator++(int)
	{
		Date tmp = *this;
		++* this;
		return tmp;
	}
public:
	Date operator-(int n); //日期-n天
	Date& operator-=(int n);
	Date& operator--();
	Date& operator--(int);
	Date operator-(const Date& d);
public:
	int GetWeek(int year, int month, int day); //星期几
public:
	bool operator>(const Date& d)
	{
		if ((m_year > d.m_year)
			|| (m_year == d.m_year && m_month > d.m_month)
			|| (m_year == d.m_year && m_month == d.m_month && m_day > d.m_day))
			return true;
		return false;
	}
	bool operator <= (const Date& d)
	{
		return !(*this > d);
	}
	bool operator<(const Date& d)
	{
		if ((m_year < d.m_year)
			|| (m_year == d.m_year && m_month < d.m_month)
			|| (m_year == d.m_year && m_month == d.m_month && m_day < d.m_day))
			return true;
		return false;
	}
	bool operator >= (const Date& d)
	{
		return !(*this < d);
	}
	bool operator==(const Date& d)
	{
		return (m_year == d.m_year && m_month == d.m_month && m_day == d.m_day);
	}
	bool operator!=(const Date& d)
	{
		return !(*this == d);
	}
private:
	int m_year;
	int m_month;
	int m_day;
};

ostream& operator<<(ostream& out, const Date& d)
{
	out << d.m_year << "年" << d.m_month << "月" << d.m_day << "日";
	return out;
}



void  main()
{
	Date dt1(2021, 10, 19);
	cout << "dt1 = " << dt1 << endl;

	//Date dt = dt1 + 13;
	//cout<<"dt = "<<dt<<endl;

	//dt1 += 100;
	Date dt2 = dt1++;
	cout << "dt1 = " << dt1 << endl;
	cout << "dt2 = " << dt2 << endl;
}
```****
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 C 语言中,由于没有对象的概念,因此无法像面向对象语言一样使用内嵌子对象。不过,我们可以通过结构体来模拟对象的概念,从而实现类似内嵌子对象的效果。 以日期类为例,我们可以定义一个结构体来表示日期,然后在日期类中嵌入这个结构体作为其成员变量,从而实现日期类的功能。 以下是一个简单的 C 日期类的实现: ``` typedef struct { int year; int month; int day; } Date; typedef struct { Date date; int (*getYear)(const Date *); int (*getMonth)(const Date *); int (*getDay)(const Date *); } DateClass; int getYear(const Date *date) { return date->year; } int getMonth(const Date *date) { return date->month; } int getDay(const Date *date) { return date->day; } DateClass Date_new(int year, int month, int day) { DateClass dateClass = { { year, month, day }, getYear, getMonth, getDay }; return dateClass; } ``` 在上面的代码中,我们首先定义了一个日期结构体 `Date`,表示一个具体的日期。然后,我们定义了一个日期类 `DateClass`,其中包含一个 `Date` 类型的成员变量 `date`,以及三个函数指针,用于获取日期的年、月、日信息。 接着,我们实现了三个函数 `getYear`、`getMonth`、`getDay`,分别用于获取日期的年、月、日信息。最后,我们定义了一个 `Date_new` 函数,用于创建一个新的日期对象,并返回一个 `DateClass` 类型的结构体,该结构体包含了日期对象的所有信息和操作。 使用这个日期类时,我们可以先通过 `Date_new` 函数创建一个日期对象,然后使用该对象的方法获取日期的信息,如下所示: ``` DateClass date = Date_new(2021, 10, 1); printf("Year: %d\n", date.getYear(&date.date)); printf("Month: %d\n", date.getMonth(&date.date)); printf("Day: %d\n", date.getDay(&date.date)); ``` 输出结果为: ``` Year: 2021 Month: 10 Day: 1 ``` 可以看到,我们通过 `Date_new` 函数创建了一个日期对象 `date`,然后使用该对象的方法获取了日期的年、月、日信息,并将其输出到控制台上。这就是一个简单的 C 日期类的实现,其中使用了结构体来模拟对象,并实现了类似内嵌子对象的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值