类和对象(3)

博客IDLanFuRen

C系列专栏 C语言重点部分 C语言注意点 C++基础 Linux 数据结构 C++注意点
声明等级:黑色->蓝色->红色

欢迎新粉加入,会一直努力提供更优质的编程博客,希望大家三连支持一下啦

目录

1.拷贝构造

2.传值返回和传引用返回用法

3.运算符重载&赋值重载

前置++与后置++问题

[const 修饰的成员函数](#const 修饰的成员函数)

4.手撕日期类


1.拷贝构造

在了解拷贝构造之前,我们先来把普通构造函数总结一下:

构造函数是对对象(对象的成员变量)进行初始化;

自定义类型会自动调用默认构造函数(在我们没有写出构造函数的情况下),内置类型不一定会调用构造函数,这得看编译器的处理了,所以我们可以给内置类型成员变量给缺省值;

默认构造函数包括:无参构造函数、全缺省构造函数、我们没写编译器默认生成的构造函数。

拷贝构造也是构造函数的一种,只不过是将已经初始化的对象拷贝给未初始化的对象,达到拷贝(初始化)的效果。

cpp 复制代码
//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;
	}

	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month ;
	int _day ;
};
cpp 复制代码
//Date.c
#include"C++5.h"
int main()
{
	//缺省+
	Date d1(2024);
	//拷贝构造
	//用同类型的对象拷贝初始化
	//1
	Date d2(d1);
    //Date d2=d1;这是拷贝构造调用第二种形式
	d1.Print();
	d2.Print();
	return 0;
}

运行结果:

浅(值)拷贝与深拷贝:

在使用拷贝构造时,我们需要鉴别是否需要显示写出拷贝构造,显示拷贝构造就是深拷贝。如果出现malloc的空间,我们默认拷贝的参数是直接传值,把值拷贝过去;如果我们只传入指针的值(存放数组的地址),那我们新对象d2和旧对象d1就会共用同一个数组空间(因为都是同一个数组的地址),那样修改d1就会影响d2,反之亦然。

我们举个例子写出深拷贝的写法:

cpp 复制代码
class MyClass {
private:
    int* data;
public:
    MyClass(int value) {
        data = new int(value);
    }
    ~MyClass() {
        delete data;
    }
    MyClass(const MyClass& other) {
        data = new int(*other.data);
    }
};

2.传值返回和传引用返回用法

第一个函数func的返回值是Date,返回d时会拷贝构造生成一个临时变量,可以理解成Date tmp=d;因为临时变量具有常性,接收的时候需要用const修饰,防止权限的放大。接收的时候相当于const Date&ref=tmp;如果写成const Date ref=tmp那也是一次拷贝构造,只不过用&之后是tmp的别名,不会调用拷贝构造。

第二个人就是引用返回,会减少这第一个箭头的拷贝构造。

但是传值传引用各有利弊,如果return返回的不是指针,malloc,new等开的空间,那么就相当于野指针的出现(野引用)

两者的使用主要看对象的生命周期是否出了该函数作用域之后会不会析构,析构就选择传值返回,不析构就选择传引用返回(减少拷贝构造)。

3.****运算符重载&赋值重载

自定义类型无法通过编译器直接运算,所以搞出了一个运算符重载,增加了代码的可读性。

运算符重载大概就是:返回值类型 operator操作符(参数);别把operator写错了qwq

但是不对劲的是,为什么这里会报错?那是因为我们给这个重载函数是外部函数,不是类的成员函数,所以说我们不能调用类封装的成员变量。那我们应该怎么办呢?C++一般使用友元函数或者直接把封装到类的成员函数里面,我们这里采取后者的方法。

cpp 复制代码
Date.h
#include<iostream>
using namespace std;
//拷贝构造
//d1(d2)
// 
//赋值重载
//d1=d2
//
class Date
{
public:
	
	Date(int year = 1, int month = 1, int day = 1);
	void Print();
	//成员函数都有隐含的this指针
	bool operator==(const Date& d1);
private:
    int _year;
    int _month;
    int _day;
}
cpp 复制代码
Date.cpp
bool Date::operator==(const Date& d1)
{
	return _year == d1._year
		&& _month == d1._month
		&& _day == d1._day;
}

赋值重载跟拷贝构造有着相似的地方,如果自己没有写赋值重载函数,编译器会自己生成一个赋值重载函数,但是对于只能深拷贝的代码,复制重载也是得自己写的,例如栈。这里给出自己想实现赋值重载函数的例子:

cpp 复制代码
Date& operator=(const Date& d)
{
	if (this != &d)
	{
		this->_year = d._year;
	}
	return *this;
}

前置++与后置++问题

那么前置和后置++的运算符重载函数该怎么写呢?operator++()这样就分辨不出来了,这时候老本可是犯了难,冥思苦想,逻辑闭环的方法真给他想到了------给后置++的参数硬塞一个int,operator++(int),这样就让前置与后置有了区别。

const 修饰的成员函数

这个const修饰成员函数是修饰函数里面的this指针指向的内容------const Date&this;

4.手撕日期类

大致给一个日期类的代码,大家可以下去自己去尝试:

cpp 复制代码
//Date.h
#pragma once
#include<iostream>
using namespace std;
class Date
{
public:
	Date(int year = 1, int month = 1, int day = 1);
	void Print() const;//成员函数都有this指针
	//const修饰this指针->const Date&const this
	int GetMonthDay(int year, int month)
	{
		int GetMonthDayArray[13] = { -1,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 GetMonthDayArray[month];
		}
	}
	//bool CheckDate();

	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;

	// d1 += 100
	Date& operator+=(int day);
	Date operator+(int day) const;

	// d1 -= 100
	//Date& operator-=(int day);

	// d1 - 100;
	//Date operator-(int day) const;

	// d1 - d2
	//int operator-(const Date& d) const;
private:
	int _year;
	int _month;
	int _day;
};

// 重载
//ostream& operator<<(ostream& out, const Date& d);
//istream& operator>>(istream& in, Date& d);
cpp 复制代码
//Date.cpp
#include"C++7.h"
Date::Date(int year,int month,int day)
{
	_year = year;
	_month = month;
	_day = day;
	cout << "Date" << endl;
}

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

bool Date::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;
			}
		}
	}
	else
		return false;
}

bool Date::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;
			}
		}
	}
	else
		return false;
}

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 *this > d || *this == d;
}

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

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

// d1 += 100
Date& Date::operator+=(int day)
{
	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);//调用opertor-=
		++_month;
		if (_month == 13)
		{
			++_year;
			_month = 1;
		}
	}
	return *this;
}

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

//d1 -= 100
//Date& Date::operator-=(int day)
//{
//
//}

// d1 - 100;
//Date Date::operator-(int day) const
//{
//
//}

// d1 - d2
//int Date::operator-(const Date& d) const
//{
//
//}
cpp 复制代码
//test.cpp
#include"C++7.h"
int main()
{
	Date d1(2024, 9, 1);
	d1 += 5;//+=
	Date d2;
	d2 = d1 + 5;//+
	d1.Print();
	d2.Print();
}
相关推荐
BaiZhuYuan5 分钟前
io流(学习笔记01)--File知识点
java·开发语言
小黑031 小时前
Scala第二天
开发语言·后端·scala
AVICCI1 小时前
C++ 基础
开发语言·c++
AutoAutoJack1 小时前
C# 委托(Delegate)二
开发语言·数据结构·算法·架构·c#
用手码出世界1 小时前
C/C++内存管理
c语言·c++
Chuyaaaaaaa1 小时前
python 加入语音功能的RW8模拟登录过程
开发语言·python·microsoft
悲伤小伞1 小时前
C/C++—有关日期类的OJ题
c语言·数据结构·c++·笔记·算法
ACALJJ321 小时前
智能指针学习笔记
开发语言·c++·算法
得鹿梦鱼、2 小时前
Qt/C++ 了解NTFS文件系统,解析0x80 $Data属性,获取Run Lists数据列表
c++·qt·ntfs mft