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

1. 类的默认成员函数

默认成员函数就是用户没有显示实现,编译器会自动生成的成员函数称为默认成员函数。一个类,我们不写的情况下编译器会默认生成以下6个成员函数。

1. 构造函数:主要完成初始化工作。

2. 析构函数:主要完成清理工作。

3. 拷贝构造:使用同类对象初始化创建对象。

4. 赋值重载:主要是把一个对象赋值给另一个对象。

5. 取地址重载:主要是普通对象和const对象取值(这两个很少会自己实现)。

2. 构造函数

构造函数是特殊的成员函数,构造函数的主要任务是实例化时初始化对象(不是开空间创建对象,我们使用的局部对象是栈帧创建时,空间就开好了的)。构造函数的本质就是要代替我们以前Stack和Data中写的Init函数的功能,构造函数自动调用的特点就完美的代替了Init。

构造函数的特点:

1. 函数名与类名相同。

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

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

4. 构造函数可以重载。

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

6. 无参构造函数,全缺省构造函数,我们不写构造时编译器自动生成的构造函数,都叫做默认构造函数。但这三个函数有且只有一个存在,不能同时存在。无参构造和全缺省函数虽然构成函数重载,但是调用时会产生歧义总结:不需要传实参就可以调用的构造就叫默认构造。

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

注意:C++把类型分为内置类型(基本类型)和自定义类型。内置类型就是语言提供的原生数据类型,如:int/char/float/double/指针等,自定义类型就是使用class/struct等关键字自己定义的类型。

#include<iostream>
using namespace std;

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

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

	//3.全缺省构造函数
	/*Data(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;
};

int main()
{
	//如果将第一个和第三个构造注释掉,只留下第二个带参构造,C2512错误“Data” : 没有合适的默认构造函数可用
	Data d1;//自动调用构造函数,不需要显示的调用
	Data d2(2024, 9, 16);//调用带参构造函数
	d1.Print();
	d2.Print();

	//注意:调用无参构造函数时不需要(),否则编译器无法区分是函数声明,还是实例化对象
	//Data d3(); warning C4930: “Data d3(void)”: 未调用原型函数(是否是有意用变量定义的?)
	Data d3;

	return 0;
}
#include<iostream>
#include<cassert>

using namespace std;

class Stack
{
public:
	Stack(int n = 4)
	{
		_a = (int*)malloc(sizeof(int) * n);
		if (_a == nullptr)
		{
			perror("malloc申请空间失败!\n");
		}
		_capacity = n;
		_top = 0;
	}

	~Stack()
	{
		free(_a);
		_a = nullptr;
		_top = _capacity = 0;
	}

	//...
private:
	//成员变量
	int* _a;
	int _top;
	int _capacity;
};

//两个栈实现队列
class MyQueue
{
public:
	   //我们不写,编译器自动生成的默认构造,对于自定义类型Stack,会调用这个成员变量的默认构造函数初始化,注意:但对于_size,编译器不做要求,可能会出现随机值,需要我们自己完成_size的初始化
private:
	Stack Pushst;
	Stack Popst;
	int _size;
};

int main()
{
	Stack st;
	MyQueue mq;
	return 0;
}

3. 析构函数

析构函数与构造函数功能相反,析构函数不是完成对对象本身的销毁(比如局部对象是存在栈帧的,函数结束栈帧销毁,它就释放了,不需要我们管)。C++规定对象在销毁时会自动调用析构函数,完成对对象中资源的清理释放工作。构造函数的本质就是要代替我们以前Stack中的Destroy函数的功能,但是像Data没有Destroy,其实就是没有资源需要被释放,所以Data是不需要析构函数的。

析构函数的特点:

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

2. 无参数无返回值。

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

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

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

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

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

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

#include<iostream>
#include<cassert>

using namespace std;

class Stack
{
public:
	Stack(int n=4)
	{
		_a = (int*)malloc(sizeof(int) * n);
		if (_a == nullptr)
		{
			perror("malloc申请空间失败!\n");
		}
		_capacity = n;
		_top = 0;
	}

	~Stack()
	{
		free(_a);
		_a = nullptr;
		_top = _capacity = 0;
	}

	//...
private:
	//成员变量
	int* _a;
	int _top;
	int _capacity;
};

//两个栈实现队列
class MyQueue
{
public:
	//编译器默认生成MyQueue的析构函数调用了Stack的析构,释放Stack内部的资源
	//显示写析构,也会自动调用Stack的析构
	/*
	 ~MyQueue()
	{}
	*/
private:
	Stack Pushst;
	Stack Popst;
};

int main()
{
	Stack st1;
    Stack st2;//后定义的先析构
	MyQueue mq;
	return 0;
}

4. 拷贝构造函数

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

拷贝构造的特点:

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

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

#include<iostream>
using namespace std;

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

	//Data(Data d)这里调用拷贝构造函数Data(Data d)之前需要传值传参,传值传参是一种拷贝行为,又形成了一个新的拷贝构造,就造成了无穷递归
	Data(const Data& d)//拷贝构造的第一个参数必须是引用,d是d1的引用,可以加上const修饰防止形(实)参被改变
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}

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

int main()
{
	Data d1;
	Data d2(d1);
	return 0;
}


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

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

#include<iostream>
using namespace std;

class Stack
{
public:
	Stack(int n = 4)
	{
		_a = (int*)malloc(sizeof(int) * n);
		if (_a == nullptr)
		{
			perror("malloc申请空间失败!\n");
		}
		_capacity = n;
		_top = 0;
	}

	void Push(int data)
	{
		if (_capacity == _top)
		{
			int newcapacity = _capacity == 0 ? 0 : 2 * _capacity;
			int* temp = (int*)realloc(_a, newcapacity * sizeof(int));
			if (!temp)
			{
				printf("realloc fail!\n");
				exit(0);
			}
			_a = temp;
			_capacity = newcapacity;
		}
		_a[_top++] = data;
	}

	Stack(Stack& st)//Stack的拷贝构造,深拷贝
	{
        //需要对_a指向资源创建同样大小的资源再拷贝值
		//st==st1,this==st2
		_a = (int*)malloc(sizeof(int) * st._capacity);
		if (_a == nullptr)
		{
			perror("malloc fail!");
			exit(0);
		}
		memcpy(_a, st._a, sizeof(int) * st._top);
		_capacity = st._capacity;
		_top = st._top;
	}

	~Stack()
	{
		free(_a);
		_a = nullptr;
		_top = _capacity = 0;
	}

	//...

private:
	int* _a;
	int _top;
	int _capacity;
};

//两个栈实现队列
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 st3 = st1;

	//MyQueue自动生成的拷贝构造,会自动调用Stack拷贝构造完成Pushst/Popst的拷贝,只需Stack自己完成了拷贝构造,它就没问题
	MyQueue mq1;
	MyQueue mq2(mq1);

	return 0;
}

5. 像Data这样类成员变量全是内置类型且没有指向什么资源,编译器自动生成的拷贝构造就可以完成需要的拷贝,所以不需要我们显示实现拷贝构造。像Stack这样的类,虽然也都是内置类型,但_a指向了资源,编译器自动生成的拷贝构造完成的值拷贝/浅拷贝不符合我们的需求,所以需要我们自己实现深拷贝(对指向的资源也进行拷贝)。像MyQueue这样的类型内部主要是自定义类型Stack成员,编译器自动生成的拷贝构造会自动调用Stack的拷贝构造,也不需要我们显示的实现MyQueue的拷贝构造。

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

#include<iostream>
using namespace std;

class Data
{
public:
	Data(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	//Data(Data d)每次调用拷贝构造韩式之前要先传值传参,传值传参又是一种拷贝,又要形成一个新的拷贝构造,形成无穷递归
	Data(Data& d)
	{
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}

	Data(Data* 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 Func1(Data d)
{
	cout << &d << endl;//?
	d.Print();
}

/*Data& Func2()
{
	Data temp(2024, 9, 16);
	temp.Print();
	return temp;
}*/

int main()
{
	Data d1(2024, 9, 16);

	//C++规定,对于自定义类型对象进行拷贝行为必须要调用拷贝构造,传值传参(拷贝行为)要调用拷贝构造
	Func1(d1);//这里的d1传值传参给d要调用拷贝构造完成拷贝,传引用传参可以减少这里的拷贝

	//Data d2(&d1);//这不是拷贝构造,是一种普通的构造

	//拷贝构造的两种写法,通过同类型的对象初始化构造
	Data d2(d1);//1
	d2.Print();

	Data d3 = d1;//2
	d3.Print();

	Func2返回了一个局部对象temp的引用作为返回值,当Func2函数结束,temp对象被销毁,ret相当于引用了一个野引用
	/*Data ret = Func2();
	ret.Print();*///warning C4172 : 返回局部变量或临时变量的地址: temp
	
	return 0;
}

5. 赋值运算符重载

5.1 运算符重载

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

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

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

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

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

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

7. .*   ::   sizeof   ?:   .  以上五个运算符不能重载。

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

9. 一个类需要重载哪些运算符,看哪些运算符重载后有意义,比如Data类重载operator-(日期-日期,相差的天数)有意义,但是重载operator+就没有意义。

10. 重载++运算符时,有前置++和后置++,运算符重载函数名都是operator++.C++规定,后置++重载时,增加一个int形参,跟前置++构成函数重载,方便区分。

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

#include<iostream>
using namespace std;

class Data
{
public:
	
	Data(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;
};

//重载为全局函数面临对象访问私有成员变量问题:这里使用了成员变量放公有的方法

bool operator==(const Data& d1, const Data& d2)
{
	return d1._year == d2._year
		&& d1._month && d2._month
		&& d1._day == d2._day;
}

int main()
{
	Data d1(2024, 9, 17);
	Data d2(2024, 9, 18);
	operator==(d1,d2);//运算符重载函数可以显示调用
	
	d1 == d2;//编译器会自动转换为operator==(d1,d2);
	return 0;
}
5.2 赋值运算符重载 

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

赋值运算符重载的特点:

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

2. 有返回值,建议写成当前类类型的引用,引用返回可以提高效率,有返回值得目的是为了支持连续赋值场景(例如:x = y = z =1;)。

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

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

#include<iostream>
using namespace std;

class Data
{
public:

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

	//*this=d2,d=d1
	Data& operator=(const Data& d)//传引用返回减少拷贝次数,提高效率,d2=d1表达式的返回值应为对象d2,也就是*this,可连续赋值
	{
		//检查不要出现自己给自己赋值的情况
		if (this != &d)
		{
			_year = d._year;
			_month = d._month;
			_day = d._day;
		}
		return *this;//在类中允许显示写this
	}

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

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

int main()
{
	//这里是赋值重载,用于完成两个已经存在的对象的直接拷贝赋值
	Data d1(2024, 9, 21);
	Data d2;
	Data d3;
	
	
	d2.operator=(d1);
	d3 = d1;//编译器会自动转换为d3.operator=(d1);

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

	//这里是拷贝构造,用于一个对象拷贝初始化给另一个要创建的对象,注意区分拷贝构造和赋值重载
	Data d4 = d1;
	Data d5(d1);

	return 0;
}
5.3 日期类的实现 
//Data.h
#include<iostream>
using namespace std;


class Data
{
	//友元函数声明,成员变量在函数外中也可以使用
	//利用流提取和流插入重载输入输出
	friend istream& operator>>(istream& in, Data& d);//返回值为istream&,为是的连续输入,不加const因为提取的值要放在对象里面
	friend ostream& operator<<(ostream& out, const Data& d);//istream和ostream的对象必须要传引用,不支持拷贝构造
	//cin>>d1>>d2;cin>>d1返回cin,cin>>d2

public:
	Data(int year = 1900, int month = 1, int day = 1);

	int CheckData();

	void Print();

	//定义在类中的成员函数默认是inline
	int GetMonthDay(int year, int month)//函数内容短小且频繁调用
	{
		static int arr[13] = { -1,31,28,31,30,31,30,31,31,30,31,30,31 };//static修饰局部变量,存储在静态区,生命周期是全局的,数组不会函数结束时被释放
		if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))//是二月(较容易判断,放在前面)且是润年
		{
			return arr[month] + 1;
			//数组是静态的,不要使用改变数组arr[month]++的方法,会导致下次调用时返回值错误
		}
		return arr[month];
	}

	int operator<(const Data& d);//year month day
	bool operator<=(const Data& d);
	bool operator==(const Data& d);
	bool operator>(const Data& d);
	bool operator>=(const Data& d);

	//d++
	Data operator++(int );//增加一个int 形参(不需要接收),参数名可以不写,区分++d,两者构成函数重载
	
	//++d,建议使用前置,减少拷贝次数
    Data& operator++();

	//d--
	Data operator--(int);

	//--d
	Data& operator--();

	//d+day
	Data operator+(int day);
	Data& operator+=(int day);

	//d-day
	Data operator-(int day);
	Data& operator-=(int day);

	//d1-d2
	int operator-(const Data d);//函数重载

	// <<
	// cout << a;
	//因为二元运算符的左侧运算对象传给第一个参数,右侧运算对象传给第二个参数,类成员函数中第一个参数默认为this指针,使用this>>cin,不符合习惯
	//void operator<<(ostream cin);
	
private:
	int _year;
	int _month;
	int _day;
};

//Data.cpp
#include"Data.h"

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

	if (!CheckData())
	{
		cout << "日期错误!!!" << endl;
		Print();
	}
}

int Data::CheckData()
{
	if (_month < 1 || _month > 12 || _day < 1 || _day > GetMonthDay(_year, _month))
	{
		return 0;
	}
	return 1;
}

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

int Data::operator<(const Data& d)//year month day
{
	if (_year < d._year)
	{
		return 1;
	}
	else if (_year == d._year)
	{
		if (_month < d._month)
		{
			return 1;
		}
		else if (_month == d._month)
		{
			if (_day < d._day)
			{
				return 1;
			}
		}
	}
	return 0;
}

bool Data::operator<=(const Data& d)
{
	/*if (*this == d || *this < d)
	{
		return 1;
	}
	return 0;*/
	return *this == d && *this < d;
}

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

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

bool Data::operator>=(const Data& d)
{
	if (*this == d || *this > d)
	{
		return 1;
	}
	return 0;
}

//2024 9 19
Data Data::operator+(int day)
{
	Data temp = *this;
	temp += day;
	return temp;
}

Data& Data::operator+=(int day)
{
	//还有要注意的是当day为负数时
	if (day < 0)
	{
		//+=一个负数 也就是 -=一个正数
		return *this -= (-day);
	}

	_day += day;
	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		_month++;
		if (_month > 12)
		{
			_year++;
			_month = 1;
		}
	}
	return *this;

}

//2024 9 19
//       47
//       -28
Data& Data::operator-=(int day)
{
	if (day < 0)
	{
		return *this += (- day);
	}
	_day -= day;
	while (_day < 1)
	{
		//当前月的天数已被减完,,借的是上一月的天数
		_month--;
		if (_month == 0)
		{
			_year--;
			_month = 12;
		}
		_day += GetMonthDay(_year, _month);
	}
	return *this;
}

Data Data::operator-(int day)
{
	Data temp = *this;
	temp -= day;
	return temp;
}


int Data::operator-(const Data d)
{
	Data max = *this;
	Data min = d;
	int flag = 1;
	if (max < min)
	{
		flag = -1;
		max = d;
		min = *this;
	}
	int count = 0;
	while (min < max)
	{
		++min;
		++count;
	}
	return flag * count;
}

//d++
Data Data::operator++(int )
{
	Data temp =*this;
	*this += 1;
	return temp;
}

//++d
Data& Data::operator++()
{
	*this += 1;
	return *this;
}

Data Data::operator--(int)
{
	Data temp = *this;
	*this += 1;
	return temp;
}

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

istream& operator>>(istream& in,Data& d)//cin是istream类型的对象
{
	while (1)
	{
		cout << "请依次输入年月日:" << endl;
		in >> d._year >> d._month >> d._day;
		if (!d.CheckData())
		{
			cout << "非法日期,请重新输入:" << endl;
			d.Print();
		}
		else
		{
			break;
		}
	}
	return in;
}


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

//Test.cpp

#include"Data.h"

void Test1()
{
	/*Data d1(2024, 9, 19);
	Data d2(2024, 9, 20);
	int day= 27;
	d1.Print();

	d1 += day;
	d1.Print();
	d1-=30000;
	d1.Print();
	d1 == d2;*/

	Data d1(2024, 9, 20);
	d1 -= -3000;
	d1.Print();
}

void Test2()
{
	Data d1(2024, 9, 20);
	Data d2(2024, 9, 20);
	
	Data ret1 = d1++;
	Data ret2 = ++d2;
	d1.Print();
	d2.Print();

	ret1.Print();
	ret2.Print();

}

void Test3()
{
	Data d1(2024, 9, 20);
	Data d2(2025, 1, 29);
	cout << d1 - d2;
	//Data d3(2024, 9, 32);
}

void Test4()
{
	Data d1;
	Data d2;
	/*d1 >> cin;
	operator>>(cin, d1);*/
	cin >> d1 >> d2;//流插入从左往右结合
	//d1.Print();
	//d2.Print();
	cout << d1 << d2 << endl;
}

int main()
{
	//Test1();
	//Test2();
	//Test3();
	Test4();
	return 0;
}

 

6. 取地址运算符重载

6.1 const成员函数

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

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

#include<iostream>
using namespace std;

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

	//void Print(const Data* const this) const
	void Print() const
	{
		cout << _year << " " << _month << " " << _day << endl;
	}

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

int main()
{
	//这里非const对象也可以调用const成员函数,是一种权限的缩小
	Data d(2024,9,21);
	d.Print();
	 
	const Data d2(2024, 8, 21);
	d2.Print();
	return 0;
}
 6.2 取地址运算符重载

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

#include<iostream>
using namespace std;

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

	Data* operator&()//普通对象
	{
		return nullptr;
	}

	const Data* operator&()const //const对象
	{
		return nullptr;
	}

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

int main()
{
	Data d1;
    const d2;
    //编译器会自动匹配最合适的
	cout << &d1;
    cout<<&d2;
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值