C++学习第六天

创作过程中难免有不足,若您发现本文内容有误,恳请不吝赐教。

提示:以下是本篇文章正文内容,下面案例可供参考。


一、回顾

//Date.h

#pragma once
#include<iostream>
using namespace std;

class Date
{
public:
	Date(int _year = 1, int _month = 1, int _day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
private:
	int year;
	int month;
	int day;
};

日期类实现声明和定义的分离:

//Date.h

#pragma once
#include<iostream>
using namespace std;

class Date
{
public:
	Date(int _year = 1, int _month = 1, int _day = 1);
private:
	int year;
	int month;
	int day;
};
//Date.cpp

#include"Date.h"

Date::Date(int _year = 1, int _month = 1, int _day = 1)
{
	_year = year;
	_month = month;
	_day = day;
}

//Test.cpp

#include"Date.h"

int main()
{
	Date d1(2024, 11, 11);
	Date d2;

	return 0;
}

1.注意:

           缺省参数声明和定义不能同时存在,必须在声明处给。

//Date.cpp

#include"Date.h"

Date::Date(int _year, int _month, int _day)
{
	_year = year;
	_month = month;
	_day = day;
}

2.检查日期

//Date.h

#pragma once
#include<iostream>
using namespace std;

class Date
{
public:
	int GetMonthDay(int year, int month);
	Date(int _year = 1, int _month = 1, int _day = 1);
	void Print();
private:
	int _year;
	int _month;
	int _day;
};
//Date.cpp

#include"Date.h"

int Date::GetMonthDay(int year, int month)
{
	static int monthArray[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
	if (month == 2 
		&& ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
		return 29;
	return monthArray[month];
}

Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;

	//检查日期是否合法
	if (month < 1 || month>12 || day<1 || day>GetMonthDay(year, month))
		cout << "非法日期" << endl;
}

void Date::Print()
{
	cout << _year << "年" << _month << "月" << _day << "日" << endl;
}
//Test.cpp

#include"Date.h"

int main()
{
	Date d1(2024, 11, 11);
	d1.Print();

	Date d2;
	d2.Print();

	Date d3(2024, 2, 30);
	d3.Print();

	return 0;
}


3.日期类拷贝

//Test.cpp

#include"Date.h"

int main()
{
	Date d1(2024, 11, 11);
    // 拷贝构造,一个已经存在的对象去初始化另一个要创建对象
	Date d2(d1);

	d1.Print();
	d2.Print();

	return 0;
}


4.赋值

//Test.cpp

#include"Date.h"

int main()
{
	Date d1(2024, 11, 11);
	Date d2(2023, 11, 11);

	// 赋值,两个已经存在对象进行拷贝
	d1 = d2;
	//d1.operator=(d2)

	d1.Print();
	d2.Print();

	return 0;
}
//Date.h

#pragma once
#include<iostream>
using namespace std;

class Date
{
public:
	int GetMonthDay(int year, int month);
	Date(int _year = 1, int _month = 1, int _day = 1);
	void Print();

	void operator=(const Date& d);

private:
	int _year;
	int _month;
	int _day;
};
//Date.cpp
void operator=(const Date& d)
{
	this->_year = d._year;
	this->_month = d._month;
	this->_day = d._day;
}


//Test.cpp

#include"Date.h"

int main()
{
	Date d1(2024, 11, 11);
	Date d2(2003, 11, 11);
    Date d3(2013, 11, 11);
	
    //如果是这种情况,上面的代码实现不了
	d1 = d2 = d3;

	return 0;
}
/*void operator=(const Date& d)
	{
		this->_year = d._year;
		this->_month = d._month;
		this->_day = d._day;
	}*/

//需要把函数改成如下,使用引用返回
Date& operator=(const Date& d)
{
	this->_year = d._year;
	this->_month = d._month;
	this->_day = d._day;


    //this指针是这个函数的一个隐藏的参数,现在引用返回的是this指针指向的内容。
    //this指针指向的内容还没有被释放,可以引用返回,
    //那this指针它出了这个函数的作用域,它就释放了。
	return *this;
}

//用if预防自己给自己赋值的情况 ( 如:d1 = d1 )
Date& operator=(const Date& d)
{
	if(this != &d)
    {
        this->_year = d._year;
	    this->_month = d._month;
	    this->_day = d._day;
    }

	return *this;
}

5.最后一点

     日期类的默认成员函数我们不写会自动生成,完成浅拷贝,所以上面的operator=我们不写也可以完成 (d1=d2;) 等操作。


二、+复用+=

//Date.h

#pragma once
#include<iostream>
using namespace std;

class Date
{
public:
	int GetMonthDay(int year, int month);
	Date(int year = 1, int month = 1, int day = 1);
	void Print();

	bool operator<(const Date& d);
	bool operator==(const Date& d);
	bool operator<=(const Date& d);
	bool operator>(const Date& d);
	bool operator>=(const Date& d);
	bool operator!=(const Date& d);

	Date& operator+=(int day);
	Date operator+(int day);

private:
	int _year;
	int _month;
	int _day;
};
//Date.cpp

#include"Date.h"

int Date::GetMonthDay(int year, int month)
{
	static int monthArray[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
	if (month == 2 
		&& ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
		return 29;
	return monthArray[month];
}

Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
	if (month < 1 || month>12 || day<1 || day>GetMonthDay(year, month))
		cout << "非法日期" << endl;
}

void Date::Print()
{
	cout << _year << "年" << _month << "月" << _day << "日" << endl;
}

bool Date::operator<(const Date& d)
{
	if (_year < d._year)
	{
		return true;
	}
	else if (_year == d._year && _month < d._month)
	{
		return true;
	}
	else if (_year == d._year && _month == d._month && _day < d._day)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool Date::operator==(const Date& d)
{
	return _year == d._year
		&& _month == d._month
		&& _day == d._day;
}

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

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

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

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

Date& Date::operator+=(int day)
{
	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_month++;
		_day -= GetMonthDay(_year, _month);

		if (_month == 13)
		{
			_year++;
			_month = 1;
		}
	}
	return *this;
}

Date Date::operator+(int day)
{
	Date tmp(*this);
	tmp += day;
	return tmp;
}
//Test.cpp

#include"Date.h"

int main()
{
	Date d1(2025, 1, 19);
	d1 += 100;
	d1.Print();

	Date d2(2025, 1, 19);
	Date ret = d2 + 100;
	ret.Print();

	return 0;
}


三、+=复用+

Date Date::operator+(int day)
{
	Date tmp = *this;
	tmp._day += day;
	while (tmp._day > GetMonthDay(tmp._year, tmp._month))
	{
		// 月进位
		tmp._day -= GetMonthDay(tmp._year, tmp._month);
		++_month;
		// 月满了
		if (tmp._month == 13)
		{
			++tmp._year;
			tmp._month = 1;
		}
	}
	return tmp;
}

Date& Date::operator+=(int day)
{
	*this = *this + day;
	return *this;
}

  += 复用 + 进行了 2 次拷贝 ,而 + 复用 += 没有进行拷贝,因此复用 += 更好。


四、- 复用 -=

//Date.h

#pragma once
#include<iostream>
using namespace std;

class Date
{
public:
	int GetMonthDay(int year, int month);
	Date(int year = 1, int month = 1, int day = 1);
	void Print();

	Date& operator-=(int day);
	Date operator-(int day);

private:
	int _year;
	int _month;
	int _day;
};
//Date.cpp

#include"Date.h"

int Date::GetMonthDay(int year, int month)
{
	static int monthArray[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
	if (month == 2 
		&& ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
		return 29;
	return monthArray[month];
}

Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;

	//检查日期是否合法
	if (month < 1 || month>12 || day<1 || day>GetMonthDay(year, month))
		cout << "非法日期" << endl;
}

void Date::Print()
{
	cout << _year << "年" << _month << "月" << _day << "日" << endl;
}

Date& Date::operator-=(int day)
{
	_day -= day;
	while (_day <= 0)
	{
		_month--;
		if (_month == 0)
		{
			_year--;
			_month = 12;
		}
		_day += GetMonthDay(_year, _month);
	}
	return *this;
}

Date Date::operator-(int day)
{
	Date tmp = *this;
	tmp -= day;
	return tmp;
}
//Test.cpp

#include"Date.h"

int main()
{
	Date d1(2025, 1, 19);
	d1 -= 101;
	d1.Print();

	return 0;
}


五、前置++和后置++

//Date.h

#pragma once
#include<iostream>
using namespace std;

class Date
{
public:
	int GetMonthDay(int year, int month);
	Date(int year = 1, int month = 1, int day = 1);
	void Print();

	Date& operator+=(int day);

	// 函数重载
	// 运算符重载
	// ++d1 -> d1.operator++()
	Date& operator++();

	// d1++ -> d1.operator++(0)
	// 加一个int参数,进行占位,跟前置++构成函数重载进行区分
	// 本质后置++调用,编译器进行特殊处理
	Date operator++(int);

private:
	int _year;
	int _month;
	int _day;
};
//Date.cpp

#include"Date.h"

int Date::GetMonthDay(int year, int month)
{
	static int monthArray[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
	if (month == 2 
		&& ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
		return 29;
	return monthArray[month];
}

Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
	//检查日期是否合法
	if (month < 1 || month>12 || day<1 || day>GetMonthDay(year, month))
		cout << "非法日期" << endl;
}

void Date::Print()
{
	cout << _year << "年" << _month << "月" << _day << "日" << endl;
}

Date& Date::operator+=(int day)
{
	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_month++;
		_day -= GetMonthDay(_year, _month);

		if (_month == 13)
		{
			_year++;
			_month = 1;
		}
	}
	return *this;
}

Date& Date::operator++()
{
	*this += 1;
	return *this;
}


Date Date::operator++(int)
{
	Date tmp = *this;
	*this += 1;
	return tmp;
}
//Test.cpp

#include"Date.h"

int main()
{
	Date d1(2025, 1, 19);
	Date ret1 = d1++;
    //显示调用Date ret1 = d1.operator++(0);
	ret1.Print();
	d1.Print();

	Date ret2 = ++d1;
	ret2.Print();
	d1.Print();

	return 0;
}


六、日期相减

//Date.h

#pragma once
#include<iostream>
using namespace std;

class Date
{
public:
	int GetMonthDay(int year, int month);
	Date(int year = 1, int month = 1, int day = 1);
	void Print();

	bool operator<(const Date& d);
	bool operator==(const Date& d);
	bool operator!=(const Date& d);
	Date& operator-=(int day);
	Date& operator++();
	Date& operator+=(int day);
	int operator-(const Date& d);

private:
	int _year;
	int _month;
	int _day;
};
//Date.cpp

#include"Date.h"

int Date::GetMonthDay(int year, int month)
{
	static int monthArray[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
	if (month == 2 
		&& ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
		return 29;
	return monthArray[month];
}

Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
	//检查日期是否合法
	if (month < 1 || month>12 || day<1 || day>GetMonthDay(year, month))
		cout << "非法日期" << endl;
}

void Date::Print()
{
	cout << _year << "年" << _month << "月" << _day << "日" << endl;
}

bool Date::operator<(const Date& d)
{
	if (_year < d._year)
	{
		return true;
	}
	else if (_year == d._year && _month < d._month)
	{
		return true;
	}
	else if (_year == d._year && _month == d._month && _day < d._day)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool Date::operator==(const Date& d)
{
	return _year == d._year
		&& _month == d._month
		&& _day == d._day;
}

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

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

Date& Date::operator+=(int day)
{
	if (day < 0)
	{
		return *this -= (-day);
	}
	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		++_month;

		if (_month == 13)
		{
			++_year;
			_month = 1;
		}
	}
	return *this;
}

Date& Date::operator++()
{
	*this += 1;
	return *this;
}

//d1-d2
int Date::operator-(const Date& d)
{
	Date max = *this;
	Date min = d;
	int flag = 1;

	if (*this < d)
	{
		max = d;
		min = *this;
		flag = -1;
	}

	int n = 0;

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

	return n * flag;
}
//Test.cpp

#include"Date.h"

int main()
{
	Date d1(2025, 12, 12);
	Date d2(2025, 1, 9);

	cout << d1 - d2 << endl;

	return 0;
}


七、const成员

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

//Test.cpp

#include"Date.h"
int main()
{
	const Date d1(2023, 7, 27);
	d1.Print();

	return 0;
}

    这里涉及权限放大的问题,这里编译器会转化成d1.Print(&d1),&d1的类型是const Date*,const要修饰被指向的内容不能被改变,void Date::Print函数的形参this类型是Date*,现在是类型const Date*的d1传递给类型Date*的this,造成了权限的放大。


//Date.h

#pragma once
#include<iostream>
using namespace std;

class Date
{
public:
	int GetMonthDay(int year, int month);
	Date(int year = 1, int month = 1, int day = 1);
	void Print() const;

private:
	int _year;
	int _month;
	int _day;
};
//Date.cpp

#include"Date.h"

int Date::GetMonthDay(int year, int month)
{
	static int monthArray[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
	if (month == 2 
		&& ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
		return 29;
	return monthArray[month];
}

Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;
	//检查日期是否合法
	if (month < 1 || month>12 || day<1 || day>GetMonthDay(year, month))
		cout << "非法日期" << endl;
}

void Date::Print()const
{
	cout << _year << "年" << _month << "月" << _day << "日" << endl;
}

//Test.cpp

#include"Date.h"

int main()
{
	const Date d1(2023, 7, 27);
	d1.Print();

	//权限可以缩小
	Date d2(2023, 7, 27);
	d2.Print();

	return 0;
}

总结

以上就是今天要讲的内容,本文仅仅简单介绍了c++的基础知识。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

jacob~

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

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

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

打赏作者

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

抵扣说明:

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

余额充值