C++的六大“天选之子“拷贝构造与与运算符重载

2024-02-26 1480阅读

温馨提示:这篇文章已超过440天没有更新,请注意相关的内容是否还可用!

C++的六大“天选之子“拷贝构造与与运算符重载

🎈个人主页:🎈 :✨✨✨初阶牛✨✨✨

🐻推荐专栏1: 🍔🍟🌯C语言初阶

🐻推荐专栏2: 🍔🍟🌯C语言进阶

🔑个人信条: 🌵知行合一

🍉本篇简介:>:讲解C++中有关类和对象的介绍,本篇是中篇的第结尾篇文章,讲解拷贝构造,运算符重载以及取地址重载符.

金句分享:

✨别在最好的年纪,辜负了最好的自己.✨

一、“拷贝构造函数”

拷贝构造函数:

只有单个形参,该形参是对本类类型对象的引用(一般常用const修饰),在用已存在的类类型对象创建新对象时由编译器自动调用。

2.1 自动生成的"拷贝构造函数"

假设哦我们需要创建两个一模一样的对象A和B.

C++的六大“天选之子“拷贝构造与与运算符重载

那我们可以先创建一个对象A,再通过将A作为参数,传给B进行初始化,

即一个自定义类型实例化出的对象(B)用另一个该类型实例化出的对象(A)进行初始化.

class Date
{
public:
	Date(int year = 2020, int month = 1, int day = 1)//全缺省构造函数
	{
		_year = year;
		_month = month;
		_day = day;
	}
	void Print()
	{
		cout 
	Date A(2023, 7, 20);
	A.Print();
	printf("\n");
	Date B(A);//会调用系统生成的拷贝构造
	B.Print();
	return 0;
}

public:
	Date(int year = 2020, int month = 1, int day = 1)//全缺省构造函数
	{
		_year = year;
		_month = month;
		_day = day;
	}
	Date(const Date& d)//拷贝构造函数
	{
		cout 
		cout 
	Date d1(2023, 7, 20);
	d1.Print();
	printf("\n");
	Date d2(d1);
	d2.Print();
	return 0;
}

}
void test(Date d1)
{
}
int main()
{
	Date d1(2023, 7, 20);
	test(2);
	test(d1);
	return 0;
}

public:
	Stack(int capacity=5)//全缺省构造函数
	{
		cout 
			perror("malloc申请空间失败!!!");
			return;
		}
		_capacity = capacity;
		_size = 0;
	}
	void Push(DataType data)//压栈操作
	{
		CheckCapacity();
		_array[_size] = data;
		_size++;
	}
	~Stack()//析构函数
	{
		cout 
			free(_array);
			_array = NULL;
			_capacity = 0;
			_size = 0;
		}
	}
private:
	void CheckCapacity()
	{
		if (_size == _capacity)
		{
			int newcapacity = _capacity * 2;
			DataType* temp = (DataType*)realloc(_array, newcapacity *
				sizeof(DataType));
			if (temp == NULL)
			{
				perror("realloc申请空间失败!!!");
				return;
			}
			_array = temp;
			_capacity = newcapacity;
		}
	}
private:
	DataType* _array;
	int _capacity;
	int _size;
};
int main()
{
	Stack s1;
	s1.Push(1);
	s1.Push(2);
	s1.Push(3);
	s1.Push(4);
	Stack s2(s1);//这条语句会报错.
	return 0;
}

		_array = (int*)malloc(sizeof(int) * S._capacity);
		if (NULL == _array)
		{
			perror("malloc申请空间失败!!!");
			return;
		}
		memcpy(S._array,_array,sizeof(int)*S._size);
		_capacity = S._capacity;
		_size = S._size;
	}

public:
	Date(int year = 2023, int month = 10, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
private:
	int _year;
	int _month;
	int _day;
};
void test1()
{
	Date d1(2023, 7, 28);
	Date d2;
	if (d2 == d1)
	{
		cout 
		cout 
public:
	Date(int year = 2023, int month = 10, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	void print()
	{
		cout 
	Date d1(2023, 7, 28);
	Date d2;
	d1.print();
	d2.print();
	cout 
	test1();
	return 0;
}
 
public :
 Date* operator&()
 {
 	return this ;
 }
 
 const Date* operator&()const
 {
 	return this ;
 }
private :
 int _year ; // 年
 int _month ; // 月
 int _day ; // 日
};
VPS购买请点击我

免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

目录[+]