2024年最全【C++】类和对象(三,2024年最新2024年C C++大厂面试

img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上C C++开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以戳这里获取

运算符重载只能是类的成员函数。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5TeBxfQt-1679743273704)(C:\Users\小卢\AppData\Roaming\Typora\typora-user-images\image-20230324004623863.png)]

用户没有显式实现时,编译器会生成一个默认赋值运算符重载,以值的方式逐字节拷贝。

注意:内置类型成员变量是直接赋值的,而自定义类型成员变量需要调用对应类的赋值运算符
重载完成赋值。

前置++和后置++的运算符重载:

前置++和后置++都是一元运算符,为了让前置++与后置++形成能正确重载
C++规定:后置++重载时多增加一个int类型的参数,但调用函数时该参数不用传递,编译器自动传递

Date& Date::operator++()
{//++d1
	\*this +=  1;
	return \*this;
}
Date Date::operator++(int)
{//d1++
	Date tmp(\*this);
	\*this += 1;
	return tmp;
}

前置++:返回+1之后的结果
注意:this指向的对象函数结束后不会销毁,故以引用方式返回提高效率

注意:后置++是先使用后+1,因此需要返回+1之前的旧值,故需在实现时需要先将this保存

const成员函数:

class A
{
public:
	void Print()const
	{
		cout << _a << endl;
	}
private:
	int _a = 10;
};
int main()
{
	const A aa;//加const会报错,权限扩大了
	aa.Print();//\*this
	return 0;
}

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

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7qJQVyF7-1679743273704)(C:\Users\小卢\AppData\Roaming\Typora\typora-user-images\image-20230325190621106.png)]

请思考下面的几个问题:

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

1.不可以

2.可以

3.不可以

4.可以

注意:权限只能缩小,不能扩大

取地址及const取地址操作符重载:

这两个运算符一般不需要重载,使用编译器生成的默认取地址的重载即可,只有特殊情况,才需
要重载,比如想让别人获取到指定的内容!

class A
{
public:
	void Print()const
	{
		cout << _a << endl;
	}
	A\* operator&()
	{
		return this;
	}
	const A\* operator&()const
	{
		return this;
	}
private:
	int _a = 10;
};
int main()
{
	const A aa;//加const会报错,权限扩大了
	aa.Print();//\*this
	cout << &aa << endl;
	return 0;
}

日期类:

Date.h文件:
#pragma once
#pragma once
#include<iostream>
#include<cassert>
using namespace std;
class Date {
	friend istream& operator>>(istream& in, Date& d);
	friend ostream& operator<<(ostream& out,const Date&d);
public:
	Date(int year = 1900, int month = 1, int day = 1);
	void Print()const;
	int GetMonthDay(int year, int month)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;
	bool operator>=(const Date& d)const;
	Date& operator+=(int day);
	Date operator+(int day)const;
	Date& operator-=(int day);
	Date operator-(int day) const;
	int operator-( const Date& d) ;
	Date& operator++();//++d1
	Date operator++(int);//d1++
	Date& operator--();//--d1
	Date operator--(int);//d1--
private:
	int _year;
	int _month;
	int _day;
};
ostream& operator<<(ostream& out, const Date& d);
istream& operator>>(istream& in, Date& d);

Date.cpp文件:
#pragma once
#include"Date.h"
int Date::GetMonthDay(int year, int month)const
{
	assert(month > 0 && month < 13);

	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;
	}
	else
	{
		return monthArray[month];
	}
}
Date::Date(int year, int month, int day)
{
	//\_year = year;
	//\_month = month;
	//\_day = day;
	//检查是否合法
	if (month > 0 && month < 13 && day <= GetMonthDay(year, month))
	{
		_year = year;
		_month = month;
		_day = day;
	}
	else
	{
		cout << "日期非法" << endl;
	}
}
void Date::Print()const
{
	cout << _year << '/' << _month << '/' << _day << endl;
}
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 _year < d._year
		|| (_year == d._year && _month < d._month)
		|| (_year == d._year && _month == d._month && _day < d._day);
}
Date Date::operator+(int day)const
{
	Date tmp(\*this);

	tmp += day;

	return tmp;
}
Date& Date::operator+=(int day)
{
	if (day < 0)
	{
		\*this -= -day;
		return \*this;
	}

	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		_month++;
		if (_month == 13)
		{
			++_year;
			_month = 1;
		}
	}

	return \*this;
}
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);
}
Date& Date:: operator-=(int day)
{
	if (day < 0)
	{
		\*this += -day;
		return \*this;
	}
	_day -= day;
	while (_day <= 0)
	{
		--_month;
		if (_month == 0)
		{
			_year--;
			_month = 12;
		}
		_day += GetMonthDay(_year, _month);
	}
	return \*this;
}
Date Date:: operator-(int day)const
{
	Date tmp(\*this);
	tmp -= day;
	return tmp;
}
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;
}
bool Date:: operator!=(const Date& d)const
{
	if (\*this == d)return false;
	return true;
}
Date& Date::operator++()
{//++d1
	\*this += 1;
	return \*this;
}
Date Date::operator++(int)
{//d1++
	Date tmp(\*this);
	\*this += 1;
	return tmp;
}
Date& Date:: operator--()//--d1
{
	\*this -= 1;
	return \*this;
}
Date Date::operator--(int)//d1--
{//效率低一些
	Date tmp(\*this);
	\*this -= 1;
	return tmp;
}
ostream& operator<<(ostream& out,const Date&d)
{//全局函数不能访问私有
	cout << d._year << "年" << d._month << "月" << d._day << "日" << endl;
	return out;
}
istream& operator>>(istream& in, Date& d)
{
	in >> d._year >> d._month >> d._day;
	return in;
}


s;
}
Date Date::operator++(int)
{//d1++
	Date tmp(\*this);
	\*this += 1;
	return tmp;
}
Date& Date:: operator--()//--d1
{
	\*this -= 1;
	return \*this;
}
Date Date::operator--(int)//d1--
{//效率低一些
	Date tmp(\*this);
	\*this -= 1;
	return tmp;
}
ostream& operator<<(ostream& out,const Date&d)
{//全局函数不能访问私有
	cout << d._year << "年" << d._month << "月" << d._day << "日" << endl;
	return out;
}
istream& operator>>(istream& in, Date& d)
{
	in >> d._year >> d._month >> d._day;
	return in;
}

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

r>>(istream& in, Date& d)
{
in >> d._year >> d._month >> d._day;
return in;
}







[外链图片转存中...(img-HEKwRcK3-1715756209053)]
[外链图片转存中...(img-LdRdTDgA-1715756209054)]

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化的资料的朋友,可以添加戳这里获取](https://bbs.csdn.net/topics/618668825)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值