构造函数,析构,拷贝构造

今天主要介绍构造函数,拷贝构造函数,析构函数,const,赋值运算符的重载。难度由易到难,每个概念下都有相应的例子(代码段)。

统一包含的头文件:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<iostream>
using namespace std;

完成对象的初始化工作:构造函数
构造函数名字与类名相同;编译器自动调用;在对象声明周期内只调用一次。

构造函数是一个特殊的成员函数,名字与类名相同,创建类类型对象时由编译器自动调用,保证每个数据成员都有 一个合适的初始值,并且在对象的生命周期内只调用一次。

构造函数特征如下:

  1. 函数名与类名相同。
  2. 无返回值。
  3. 对象实例化时编译器自动调用对应的构造函数。
  4. 构造函数可以重载。

普通类模板:

class Date {
public:
	void SetDate(int year, int month, int day) {
		_year = year;
		_month = month;
		_day = day;
	}
	void PrintDate() {
		cout << _year << "-" << _month << "-" << _day << endl;
	}

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

int main() {
	Date d;
	d.SetDate(2019,7,30);
	d.PrintDate(); 
	system("pause");
	return 0;
}

转化为构造函数形式:

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

		cout << "Date::Date():" << this << endl;
	}

	void SetDate(int year, int month, int day) {
		_year = year;
		_month = month;
		_day = day;
	}

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

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

void TestDate() {
	Date d1(2019, 7, 30);
	d1.PrintDate();
}

int main() {
	int a;
	int b = 10;
	int c(20);

	TestDate();
	system("pause");
	return 0;
}  

运行结果如下:
在这里插入图片描述
注意:如果通过无参构造函数创建对象时,对象后面不用跟括号,否则就成了函数声明

class Date
{ 
比特科技
public :
// 1.无参构造函数
Date ()
 {}
// 2.带参构造函数
Date (int year, int month , int day )
 {
_year = year ;
_month = month ;
_day = day ;
 }
private :
int _year ;
int _month ;
int _day ;
};
void TestDate()
{
Date d1; // 调用无参构造函数
Date d2 (2015, 1 1); // 调用带参的构造函数
// 注意:如果通过无参构造函数创建对象时,对象后面不用跟括号,否则就成了函数声明
// 以下代码的函数:声明了d3函数,该函数无参,返回一个日期类型的对象
Date d3(); 
}

完成对象中资源清理工作:析构函数
~析构函数中没有参数 原因:死不带去

~ 相反符号
eg:Seqlist构造函数
~Seqlist析构函数

析构函数特征如下:

  1. 析构函数名是在类名前加上字符 ~。
  2. 无参数无返回值。
  3. 一个类有且只有一个析构函数。若未显式定义,系统会自动生成默认的析构函数。
  4. 对象生命周期结束时,C++编译系统系统自动调用析构函数。
class A {
public:
	A(int a,int b,int c) {
	}
	void PrintB() {}
	~A {     //析构函数
	}
};

private: 
};

void C() {}
	A d(w,y,z);
	d.PrintB();

int main() {
	int a;
	int b;
	int c;
	C();
	system("pause");
	return 0;
}

完成对象拷贝:拷贝构造函数:也是构造函数的一种重载的形式
默认拷贝构造函数:浅拷贝—将一个对象中内容原封不动的拷贝到另一个对象中
带&是拷贝构造函数 Date(const Date& d)
单参数(const类类型&)–>如果没有&,编译器编译失败,导致无限递归

**加粗样式
**

拷贝构造函数特征: 拷贝构造函数也是特殊的成员函数,其特征如下:

  1. 拷贝构造函数是构造函数的一个重载形式。
  2. 拷贝构造函数的参数只有一个且必须使用引用传参,使用传值方式会引发无穷递归调用。
  3. 若未显示定义,系统生成默认的拷贝构造函数。 默认的拷贝构造函数对象按内存存储按字节序完成拷贝,这种拷贝我们叫做浅拷贝,或者值拷贝。(只有默认值拷贝,如Date,没有值的无法完成默认拷贝构造,如String)

class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
Date(const Date& d)
{
_year = d._year;
_month = d._month;
_day = d._day;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1;
Date d2(d1);
return 0; }

在这里插入图片描述

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

	//拷贝构造函数
	Date(const Date& d) {
		_year = d._year;
		_month = d._month;
		_day = d._day;

		cout << "Date(Date&):" << endl;
	}

	//构造函数
	Date(Date* p) {
	}

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

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

void TestDate() {
	Date d3;
	Date d1(2019, 7, 30);
	Date d2(d1);  //调用拷贝构造函数
	d3 = d1;
}

int main() {
	
	TestDate();

	system("pause");
	return 0;
}

浅拷贝:两个对象共用同一份地址
若未显示定义,系统生成默认的拷贝构造函数。 默认的拷贝构造函数对象按内存存储按字节序完成拷
贝,这种拷贝我们叫做浅拷贝,或者值拷贝。

class Date
{
public:
Date(int year = 1900, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
Date d1;
// 这里d2调用的默认拷贝构造完成拷贝,d2和d1的值也是一样的。
Date d2(d1);
return 0; }

下面这个例子包含:全缺省构造函数;拷贝构造函数;运算符的重载。

//构造函数重载
class Date {
public:
	//全缺省的构造函数
	Date(int year = 1900, int month = 1, int day = 1) {
		_year = year;
		_month = month;
		_day = day;
	}

	//拷贝构造函数
	Date(const Date& d) {
		_year = d._year;
		_month = d._month;
		_day = d._day;

		cout << "Date(Date&):" << endl;
	}

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

	/*bool IsSame(const Date& d) {
	return _year == d._year &&
	_month == d._month &&
	_day == d._day;
	}*/

	//bool operator==(const Date& left, const Date& right) {
	//	return left_year == right._year &&
	//		left_month == right._month &&
	//		left_day == right._day;
	//}


//运算符的重载
	bool operator==(const Date& d){
		return _year == d._year &&
		_month == d._month &&
		_day == d._day;
	}

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

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

int main() {
	int a = 10;
	int b = 10;

	Date d1(2019, 6, 4);
	Date d2(2019, 6, 4);

	if (d1 == d2) {
	//*if (d1.IsSame(d2)) {
		cout << "同年同月同日生"<<endl;
	}
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值