首页 > 编程语言 >【C++】类和对象(中篇)

【C++】类和对象(中篇)

时间:2024-07-21 10:25:52浏览次数:9  
标签:中篇 对象 C++ month int Date 拷贝 day 构造函数

类和对象

一.类的默认成员函数

  默认成员函数就是用户不用实现的函数,编译器会自动生成的成员函数。类中编译器会默认生成6个默认成员函数。C++11之后还增加两个默认成员函数,移动构造移动赋值

从两个方面去学习:

  1. 我们不写时,编译器默认生成的函数行为是什么,是否满足我们的需求。

  2. 编译器默认生成的函数不满足我们的需求,我们需要自己实现,那么如何自己实现?

在这里插入图片描述

二.构造函数

  构造函数是特殊的成员函数,构造函数的主要任务并不是开空间创建对象(我们常使用的局部对象在栈帧创建时,空间就开好了),而是对象实例化时初始化对象。构造函数自动调用的特点就完美的替代的了初始化操作(Init函数),比如顺序表的初始化。

构造函数的特点:

  1. 函数名与类名相同。

  2. 无返回值,也不需要写void。

  3. 对象实例化时系统会自动调用对应的构造函数。

  4. 构造函数可以重载。

重点:

  1. 如果类中没有显式定义构造函数,则C++编译器会自动生成一个无参的默认构造函数,一但用户显式定义编译器将不再生成。

  2. 默认构造(不传实参就可以调用的构造函数):自己写的无参构造函数、全缺省构造函数、编译器提供的无参构造函数。这三个函数有且只有一个存在,不能同时存在。无参构造函数全缺省构造函数虽然构成函数重载,但是调用时会存在歧义

  3. 全缺省构造函数有参构造函数不能共存,否则重定义

#include<iostream>
using namespace std;

class Date
{
public:
	//1.无参构造函数
	Date()
	{
		_year = 2024;
		_month = 7;
		_day = 16;
	}

	//2.有参构造函数
	Date(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}

	//3.全缺省构造函数
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	void Print()
	{
		cout << this->_year << "/" << this->_month << "/" << this->_day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	//Date d1;
	//d1.Print();
	//注意:调用无参构造函数不能加括号 ————> Date d1();与函数声明无法区分

	//Date d2(2024, 7, 16);
	//d2.Print();

	//Date d3(2024);
	//d3.Print();

	return 0;
}
  1. 不写构造函数,编译器默认生成的构造函数,对内置类型成员变量的初始化没有要求,也就是说是否初始化是不确定的,看编译器。对于自定义类型成员变量(例如:类的嵌套),要求调用这个成员变量的默认构造函数初始化。如果这个成员变量,没有默认构造(自己写的默认构造),那么就会报错,我们要初始化这个成员变量,需要用初始化列表才能解决,初始化列表。

说明:C++把类型分成内置类型(基本类型)和自定义类型。内置类型:int/char/double/指针等;自定义类型:class/struct等关键字自己定义的类型。

#include<iostream>
using namespace std;

typedef int STDataType;
class Stack
{
public:
	//全缺省(也算默认构造)
	Stack(int n = 4)
	{
		_a = (STDataType*)malloc(sizeof(STDataType) * n);
		if (nullptr == _a)
		{
			perror("malloc申请空间失败");
			return;
		}
		_capacity = n;
		_top = 0;
	}
private:
	STDataType* _a;
	size_t _capacity;
	size_t _top;
};
//两个Stack实现队列
class MyQueue
{
public:
	
private:
	Stack pushst;
	Stack popst;
};
int main()
{
	//编译器默认生成MyQueue的构造函数调用了Stack的构造函数,完成了两个Stack成员类的初始化
	MyQueue mq;

	return 0;
}

三.析构函数

  析构函数与构造函数功能相反,析构函数不是完成对对象本身的销毁,比如局部对象是存在栈帧的中的,函数结束栈帧销毁,就释放了,不需要我们管。C++规定对象在销毁时会自动调用析构函数,完成对象中资源的清理释放工作(堆区:动态开辟的空间)。析构函数的功能类比我们之前Stack实现的Destroy功能,而像Date没有Destroy,其实就是没有资源需要释放,所以严格说Date是不需要析构函数的。

析构函数的特点:

  1. 析构函数名是在类名前加上字符~

  2. 无参数无返回值,也不需要加void。

  3. 一个类只能有一个析构函数。若未显式定义,系统会自动生成默认的析构函数。

  4. 对象生命周期结束时,系统会自动调用析构函数。

  5. 与构造函数类似,不写析构函数,编译器自动生成的析构函数对内置类型成员不做处理,自定类型成员会调用他的析构函数。

  6. 还需要注意的是我们写析构函数,对于自定义类型成员也会调用他的析构,也就是说自定义类型成员无论什么情况都会自动调用析构函数。

#include<iostream>
using namespace std;

typedef int STDataType;
class Stack
{
public:
	//全缺省(也算默认构造)
	Stack(int n = 4)
	{
		_a = (STDataType*)malloc(sizeof(STDataType) * n);
		if (nullptr == _a)
		{
			perror("malloc申请空间失败");
			return;
		}
		_capacity = n;
		_top = 0;
	}
	~Stack()
	{
		//程序结束前自动调用析构函数,释放堆区动态开辟的空间
		free(_a);
		_a = nullptr;
		_capacity = _top = 0;
	}
private:
	STDataType* _a;
	size_t _capacity;
	size_t _top;
};
// 两个Stack实现队列 
class MyQueue
{
public:
	//即使写了MyQueue的析构函数,程序结束时调用MyQueue析构函数的同时也会调用Stack的析构函数
	/*~MyQueue()
	{
		cout << "~MyQueue()" << endl;
	}*/
private:
	Stack pushst;
	Stack popst;
};
int main()
{
	//编译器默认生成MyQueue的构造函数调用了Stack的构造函数,完成了两个Stack成员类的初始化
	//编译器默认生成MyQueue的析构函数调用了Stack的析构函数,释放了两个Stack成员类内部的资源
	MyQueue mq;

	return 0;
}
  1. 如果类中没有申请资源时,析构函数可以不写,直接使用编译器生成的默认析构函数,如Date;如果默认生成的析构就可以用,也就不需要显示写析构,如MyQueue;但是有资源申请时,一定要自己写析构,否则会造成资源泄漏,如Stack。

  2. 一个局部域的多个对象,C++规定后定义的先析构。

注意:先实例化的对象先构造后实例化的对象先析构,原因是:栈的先进后出

四.拷贝构造函数

  如果一个构造函数的第一个参数是自身类类型的引用,且任何额外的参数都有默认值,则此构造函数也叫做拷贝构造函数,也就是说拷贝构造是一个特殊的构造函数。

拷贝构造的特点:

  1. 拷贝构造函数是构造函数的一个重载。

  2. 拷贝构造函数的第一个参数必须是类类型对象的引用,使用传值方式编译器直接报错,因为语法逻辑上会引发无穷递归调用。

  3. C++规定自定义类型对象进行拷贝行为必须调用拷贝构造,所以这里自定义类型传值传参和传值返回都会调用拷贝构造完成。

  4. 若未显式定义拷贝构造,编译器会自动生成拷贝构造函数。自动生成的拷贝构造对内置类型成员变量会完成值拷贝/浅拷贝(一个字节一个字节的拷贝),对自定义类型成员变量会调用他的拷贝构造。

#include<iostream>
using namespace std;

class Date
{
public:
	Date(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	//拷贝构造函数
	Date(const Date& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	void Print()
	{
		cout << _year << "/" << _month << "/" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
void Func(Date d)
{
	cout << &d << endl;
	d.Print();
}
int main()
{
	Date d1(2024, 7, 18);
	Date d2(d1);

	//C++规定,传值传参会调用拷贝构造
	Func(d1);//先调用拷贝构造,再调用Func函数

	return 0;
}

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

  1. 像Date这样的类成员变量全是内置类型且没有指向什么资源,编译器自动生成的拷贝构造就可以完成需要的拷贝,所以不需要我们显示实现拷贝构造。但是像Stack这样的类,虽然也都是内置类型,但是_a指向了资源,编译器自动生成的拷贝构造完成的值拷贝/浅拷贝不符合我们的需求,所以需要我们自己实现深拷贝(对指向的资源也进行拷贝)。像MyQueue这样的类型内部主要是自定义类型Stack成员,编译器自动生成的拷贝构造会调用Stack的拷贝构造,也不需要我们显示实现MyQueue的拷贝构造。这里还有一个小技巧,如果一个类显示实现了析构并释放资源,那么他就需要显示写拷贝构造,否则就不需要。
#include<iostream>
using namespace std;

typedef int STDataType;
class Stack
{
public:
	Stack(int n = 4)
	{
		_a = (STDataType*)malloc(sizeof(STDataType) * n);
		if (nullptr == _a)
		{
			perror("malloc申请空间失败");
			return;
		}
		_capacity = n;
		_top = 0;
	}
	Stack(const Stack& st)
	{
		//需要对_a指向资源创建同样大的资源再拷贝值————>深拷贝
		_a = (STDataType*)malloc(sizeof(STDataType) * st._capacity);
		if (nullptr == _a)
		{
			perror("malloc申请空间失败!!!");
			return;
		}
		memcpy(_a, st._a, sizeof(STDataType) * st._top);
		_top = st._top;
		_capacity = st._capacity;
	}
	void Push(STDataType x)
	{
		if (_top == _capacity)
		{
			int newcapacity = _capacity * 2;
			STDataType* tmp = (STDataType*)realloc(_a, newcapacity *

				sizeof(STDataType));
			if (tmp == NULL)
			{
				perror("realloc fail");
				return;
			}
			_a = tmp;
			_capacity = newcapacity;
		}
		_a[_top++] = x;
	}
	~Stack()
	{
		cout << "~Stack()" << endl;
		free(_a);
		_a = nullptr;
		_top = _capacity = 0;
	}
private:
	STDataType* _a;
	size_t _capacity;
	size_t _top;
};
//两个Stack实现队列 
class MyQueue
{
public:
	//......
private:
	Stack pushst;
	Stack popst;
};
int main()
{
	Stack st1;
	st1.Push(1);
	st1.Push(2);
	//Stack不显示实现拷贝构造,用自动生成的拷贝构造完成浅拷贝 
	//会导致st1和st2里面的_a指针指向同一块资源,析构时会析构两次,程序崩溃 
	Stack st2 = st1;//Stack st2(st1);

	MyQueue mq1;
	//MyQueue自动生成的拷贝构造,会自动调用Stack拷贝构造完成pushst/popst的拷贝,
	//只要Stack拷贝构造自己实现了深拷贝,他就没问题 
	MyQueue mq2 = mq1;

	return 0;
}

在这里插入图片描述
在这里插入图片描述

  1. 传值返回会产生一个临时对象调用拷贝构造,传值引用返回,返回的是返回对象的别名(引用),没有产生拷贝。但是如果返回对象是一个当前函数局部域的局部对象,函数结束就销毁了,那么使用引用返回是有问题的,这时的引用相当于野引用,类似野指针。传引用返回可以减少拷贝,但是一定要确保返回对象,在当前函数结束后还在,才能用引用返回。

在这里插入图片描述

五.运算符重载

  1. 当运算符被用于类类型的对象时,C++语言允许我们通过运算符重载的形式指定新的含义。C++规定类类型对象使用运算符时,必须转换成调用对应运算符重载,若没有对应的运算符重载则会编译报错。

  2. 运算符重载是具有特名字的函数,他的名字是由operator和后面要定义的运算符共同构成。和其他函数⼀样,它也具有其返回类型和参数列表以及函数体。

  3. 重载运算符函数的参数个数和该运算符作用的运算对象数量一样多。一元运算符有一个参数,⼆元运算符有两个参数,⼆元运算符的左侧运算对象传给第⼀个参数,右侧运算对象传给第⼆个参数。

  4. 如果一个重载运算符函数是成员函数,则它的第一个运算对象默认传给隐式的this指针,因此运算符重载作为成员函数时,参数比运算对象少一个。

  5. 运算符重载以后,其优先级和结合性与对应的内置类型运算符保持一致。

  6. 不能通过连接语法中没有的符号来创建新的操作符:比如operator@。

  7. .* :: sizeof ?: . 注意以上5个运算符不能重载。(重点:面试常考可以背一下)

  8. 重载操作符至少有一个类类型参数,不能通过运算符重载改变内置类型对象的含义,如: int
    operator+(int x, int y)。

  9. 一个类需要重载哪些运算符,是看哪些运算符重载后有意义,比如Date类重载operator-就有意
    义,但是重载operator+就没有意义。

#include<iostream>
using namespace std;

class Date
{
	//friend bool operator==(Date d1, Date d2);全局函数做友元

public:
	Date(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	void Print()
	{
		cout << _year << "/" << _month << "/" << _day << endl;
	}
	int GetYear()
	{
		return _year;
	}
	bool operator==(Date d1)
	{
		return _year == d1._year
		&& _month == d1._month
		&& _day == d1._day;
	}
	
	//日期+天数————>返回日期
	Date operator+(int day);
	
	//日期-天数————>返回日期
	Date operator-(int day);
	
	//日期+日期————>返回天数
	int operator-(const Date& d);
	
private:
	int _year;
	int _month;
	int _day;
};

//重载为全局的面临对象访问私有成员变量的问题 
//bool operator==(Date d1, Date d2)
//{
//	return d1._year == d2._year
//		&& d1._month == d2._month
//		&& d1._day == d2._day;
//}

//有4中解决方案:
//1、成员变量设置为公有 
//2、Date提供getxxx函数
//3、友元函数 
//4、重载为成员函数 

int main()
{
	Date x1(2024, 7, 18);
	Date x2(2024, 7, 18);

	x1 == x2;//全局函数的本质:operator==(x1, x2);
	x1 == x2;//成员函数的本质:x1.operator==(x2);

	return 0;
}
  1. 重载++运算符时,有前置++和后置++,运算符重载函数名都是operator++,无法很好的区分。
    C++规定,后置++重载时,增加一个int形参,跟前置++构成函数重载,方便区分。

  2. 重载 << 和 >> 时,需要重载为全局函数,因为重载为成员函数,this指针默认抢占了第一个形参位置,第一个形参位置是左侧运算对象,调用时就变成了对象 << cout,不符合使用习惯和可读性。重载为全局函数把ostream/istream放到第一个形参位置就可以了,第⼆个形参位置当类类型对象。

六.赋值运算符重载

  赋值运算符重载是一个默认成员函数,用于完成两个已经存在的对象直接的拷贝赋值,这里要注意跟拷贝构造区分,拷贝构造用于一个对象拷贝初始化给另一个要创建的对象。

赋值运算符重载的特点:

  1. 赋值运算符重载是一个运算符重载,规定必须重载为成员函数。赋值运算重载的参数建议写成
    const当前类类型引用,否则会传值传参会有拷贝。

  2. 有返回值,且建议写成当前类类型引用,引用返回可以提高效率,有返回值目的是为了支持连续赋值场景。

  3. 没有显式实现时,编译器会自动生成一个默认赋值运算符重载,默认赋值运算符重载行为跟默认构造函数类似,对内置类型成员变量会完成值拷贝/浅拷贝(一个字节一个字节的拷贝),对自定义类型成员变量会调用他的拷贝构造。

  4. 像Date这样的类成员变量全是内置类型且没有指向什么资源,编译器自动生成的赋值运算符重载就可以完成需要的拷贝,所以不需要我们显示实现赋值运算符重载。像Stack这样的类,虽然也都是内置类型,但是_a指向了资源,编译器自动生成的赋值运算符重载完成的值拷贝/浅拷贝不符合我们的需求,所以需要自己实现深拷贝(对指向的资源也进行拷贝)。像MyQueue这样的类型内部主要是自定义类型Stack成员,编译器自动生成的赋值运算符重载会调用Stack的赋值运算符重载,也不需要我们显示实现MyQueue的赋值运算符重载。一个小技巧,如果一个类显示实现了析构并释放资源,那么他就需要显示写赋值运算符重载,否则就不需要。

七.日期类的实现

1.Date.h

#pragma once

#include<iostream>
using namespace std;
#include<assert.h>

class Date
{
	//友元函数声明
	friend ostream& operator<<(ostream& out, const Date& d);
	friend istream& operator>>(istream& in, Date& d);

public:
	bool CheckDate();
	Date();
	Date(int year, int month, int day);
	void Print();

	//定义在类里面的成员函数默认是inline,多次调用可以提高效率
	int GetMonthDay(int year, int month)
	{
		assert(month > 0 && month < 13);

		static int monthDayArray[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;
		}
		return monthDayArray[month];
	}

	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);

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

	//后置++:与前置++作区分,加上了一个占位参数int
	Date operator++(int);

	//前置++:效率高,更常用,默认是前置++
	Date& operator++();

	// d1 - d2
	int operator-(const Date& d);

	//成员函数重载<<:存在问题————> d1 << cout
	//void operator<<(ostream& out);

private:
	int _year;
	int _month;
	int _day;
};

//全局函数重载<<
ostream& operator<<(ostream& out, const Date& d);

//全局函数重载>>
istream& operator>>(istream& in, Date& d);

2.Date.cpp

#define _CRT_SECURE_NO_WARNINGS 1

#include"Date.h"

Date::Date()
{
	;
}

bool Date::CheckDate()
{
	if (_month < 0 || _month>12 || _day<1 || _day>GetMonthDay(_year, _month))
	{
		return false;
	}
	return true;
}

//构造函数
Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;

	if (!CheckDate())
	{
		cout << "非法日期:";
		Print();
		cout << endl;
	}
}

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

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

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

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

// d1 == d2
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);
}

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

// d1 += 100
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;
}

// d1 + 100
Date Date::operator+(int day) //若返回引用,则造成野引用,只能返回值
{
	Date temp = *this; //调用拷贝构造
	temp += day;

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

	return temp; //调用拷贝构造
}

// d1 - 100
Date Date::operator-(int day) //调用拷贝两个构造函数
{
	Date temp = *this; //调用拷贝构造函数:1次
	temp._day -= day;
	while (temp._day <= 0)
	{
		--temp._month;
		if (temp._month == 0)
		{
			temp._month = 12;
			--temp._year;
		}
		temp._day += GetMonthDay(temp._year, temp._month);
	}

	return temp; //调用拷贝构造函数:1次
}

// d1 -= 100
Date& Date::operator-=(int day) //调用三个拷贝构造函数
{
	*this = *this - day; //调用operator-时:2个
	//调用默认赋值运算符重载时:1个
	return *this;
}

//注意:
// - 调用 -= 可以减少3次拷贝构造函数
// + 调用 += 可以减少3次拷贝构造函数

// d1++
Date Date::operator++(int) //后置++有两次拷贝构造
{
	Date temp = *this; //调用拷贝构造函数:1次
	*this += 1;
	return temp; //调用拷贝构造函数:1次
}

// ++d1
Date& Date::operator++() //前置++无需调用拷贝构造
{
	*this += 1;
	return *this;
}

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

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

	int n = 0;
	while (min != max)
	{
		++min;
		++n;
	}
	return n * flag;
}

// d1 << cout或者d1.operator<<(cout)
//void Date::operator<<(ostream& out)// 第一个参数隐藏了this指针,只能用d1<<cout调用,无法cout<<d1
//{
//	out << _year << "年" << _month << "月" << _day << "日" << endl;
//}

ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
	return out;
}

istream& operator>>(istream& in, Date& d)
{
	while (1)
	{
		cout << "请依次输入年月日:";
		in >> d._year >> d._month >> d._day;

		if (!d.CheckDate())
		{
			cout << "输入非法日期:";
			d.Print();
			cout << "请重新输入!" << endl;
		}
		else
		{
			break;
		}
	}
	
	return in;
}

2.test.cpp

#define _CRT_SECURE_NO_WARNINGS 1

#include"Date.h"

void TestDate1()
{
	Date d1(2024, 7, 18);
	Date d2 = d1 + 100;

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

	d1 += 100;
	d1.Print();
}

void TestDate2()
{
	Date d1(2024, 7, 18);
	Date d2 = d1 - 100;

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

	d1 -= 100;
	d1.Print();
}

void TestDate3()
{
	Date d1(2024, 7, 18);
	Date ret1 = d1++;
	d1.Print();
	ret1.Print();

	Date d2(2024, 7, 18);
	Date ret2 = ++d2;
	d2.Print();
	ret2.Print();
}

void TestDate4()
{
	Date d1(2025, 3, 1);
	Date d2(2024, 7, 18);

	cout << d1 - d2 << endl;
}

void TestDate5()
{
	Date d1(2024, 7, 18);
	Date d2(2024, 7, 19);

	//调用成员函数重载<<
	//void operator<<(ostream& out)
	//d1 << cout;

	//调用全局函数重载<<
	//operator<<(cout, d1);
	//cout << d1;

	//连续调用
	//cout << d1 << d2;

	cin >> d1 >> d2;
	cout << d1 << d2;

	cout << d1 - d2 << endl;
}

int main()
{
	//TestDate1();
	//TestDate2();
	//TestDate3();
	//TestDate4();
	TestDate5();

	return 0;
}

八.取地址运算符重载

1.const成员函数

  1. 将const修饰的成员函数称之为const成员函数,const修饰成员函数放到成员函数参数列表的后
    面。

  2. const实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改。const修饰Date类的Print成员函数,Print隐含的this指针由 Date* const this 变为 const
    Date* const this

#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(const Date* const this)
	void Print() const
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};

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

	Date d2(2024, 8, 5);
	d2.Print();

	return 0;
}

权限放大:错误
在这里插入图片描述

权限平移:正确

在这里插入图片描述

权限缩小:正确

在这里插入图片描述

Date类中的成员函数一些可以加上const。例如:可以——>日期+天数,不可以——>日期+=天数

2.取地址运算符重载

  取地址运算符重载分为普通取地址运算符重载和const取地址运算符重载,一般这两个函数编译器自动生成的就可以够我们用了,不需要去显示实现。除非一些很特殊的场景,比如我们不想让别取到当前类对象的地址,就可以自己实现一份,胡乱返回一个地址。

#include<iostream>
using namespace std;

class Date
{
public:
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	Date* operator&()
	{
		return this;
		//return nullptr; 恶作剧
		//return (Date*)0x0012ff40 恶作剧
	}
	const Date* operator&()const
	{
		return this;
		//return nullptr;
		//return (Date*)0x0012ff48
	}
private:
	int _year; // 年 
	int _month; // 月
	int _day; // 日
};
int main()
{
	const Date d1(2024, 7, 5);
	Date d2(2024, 8, 5);

	cout << &d1 << endl;
	cout << &d2 << endl;

	return 0;
}

标签:中篇,对象,C++,month,int,Date,拷贝,day,构造函数
From: https://blog.csdn.net/2203_76003626/article/details/140477555

相关文章

  • C++自学笔记1(c++原理)
    Why学习C++?因为C++直接控制硬件。C++的工作原理是:C++代码,代码交给编译器来编译,编译器将代码转换成目标平台的机器码。(机器码:你操作的设备上CPU实际执行的二进制命令)所以我们使用C++可以控制CPU上每条进程的指令。C++可以运用在哪些平台上?几乎任何平台,只要你找到对应的编译器......
  • C++自学笔记2(变量与函数)
    变量(数据)编程的本质实际上就是操纵数据,任何种类的数据。我们想对数据进行修改、读取、写入,我们将数据放在一个叫做变量的东西,他可以允许我们对数据命名。我们需要利用数据、存放数据。当我们创造一个变量时,他被存放在两个地方“堆”和“栈”(再挖个坑)在物理意义上我们的数据......
  • C++自学笔记3(头文件)
    头文件就是各个函数申明存放地(也可以放结构体)现在你有main.c文件、A.c、B.c文件A.c包含A()函数B.c包含B()函数如果想要在main.c中使用A()B()两个函数,那么我们必须在intmain()之上进行申明_________________________________________________________________________//mai......
  • 使用面向对象思想实现乐器弹奏
    题目乐手可以弹奏不同的乐器从而发出不同的声音。可以弹奏的乐器包括二胡、钢琴和琵琶。定义乐器类Instrument,包括方法make_sound()。定义乐器类的子类:二胡Erhu、钢琴Piano和小提琴Violin,定义一个函数可以弹奏各种乐器play(instrument),测试给乐手不同的乐器让他弹奏。代码c......
  • Standard Template Libary or C++ Standard Library
    C++提供一套标准的library称为C++standardlibrary完全以template完成,所以又被称为StandardTemplateLibrary。这套library专门有于实现常用的据结构(例如arry、list......)以及常用的算法(例如push,pop,insert,delete,query,retrieval......)。一般来说,STL包含六个主要的组件......
  • 导入创建对对象的意外引用,具体取决于模块是否是文件,或者是否是包的一部分
    我有一个微妙的导入问题,它创建的变量引用的对象与我期望的不同,这完全取决于我正在编写的模块是“按原样”导入还是作为包的一部分导入。假设我有一个定义Config类和设置它的函数的简单模块:classConfig:def__init__(self,**kwargs):ifkwargs.get......
  • 如何忽略或绕过导致 NoneType 不可迭代对象的实例
    我正在尝试解析Edgar数据库中10K的部分,当我运行以下代码时,#pipinstalledgartoolsimportpandasaspdfromedgarimport*#TelltheSECwhoyouareset_identity("[email protected]")filings2=get_filings(form='10-K',amendments=False,......
  • 【Java面向对象】异常处理
    文章目录1.异常1.1异常类1.2异常类型1.3免检异常和必检异常2.异常的声明、抛出与捕获2.1声明异常2.2抛出与捕获异常2.3从异常中获取信息3.链式异常3.1重新抛出异常3.2链式异常4.创建自定义异常类1.异常1.1异常类异常是对象,而对象都采用类来定义。异常的......
  • C++面向对象三大特性:继承
     前言:众所周知,C++作为一个面向对象的编程语言,有着三大特性:封装,继承,和多态。在前面的C++学习中,我们大部分所接触的只有封装,现在,就让我们来认识一下继承吧。一、继承的概念继承是指一个类(子类)从另一个类(父类)获得属性和行为的过程,它是使得代码得到复用的重要手段,允许程序员......
  • C++分组背包问题_动态规划dp_背包_算法竞赛
    OIWiki上的详细讲解模型总结分组背包的问题模板:有nnn件物品和一个大小为mm......