C++类与对象(中)


构造函数

1.构造函数是特殊的成员函数

需要注意的是,构造函数的虽然名称叫构造,但是需要注意的是构造函数的主要任务并不是开空间创建对象,而是初始化对象

​
class Date
{
public:
	// 1.无参构造函数
	Date()
	{}

	// 2.带参构造函数
	Date(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	void Display()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
void TestDate()
{
	Date d1; // 调用无参构造函数,日期是随机值
	Date d2(2022, 4, 25); // 调用带参的构造函数
	d1.Display();
	d2.Display();
	// 注意:如果通过无参构造函数创建对象时,对象后面不用跟括号,否则就成了函数声明
	// 以下代码的函数:声明了d3函数,该函数无参,返回一个日期类型的对象
	//Date d3();
}
int main()
{
	TestDate();
	return 0;
}

​

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

​
class Date
{
public:

	void Display()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
void TestDate()
{
	Date d1; 
	//没有定义构造函数,对象也可以构造成功。因此调用的是默认构造函数
}
int main()
{
	TestDate();
	return 0;
}

​

3.无参的构造函数和全缺省的构造函数都称为默认构造函数,并且默认构造函数只能有一个

注意:无参构造函数、全缺省构造函数、我们没写编译器默认生成的构造函数,都可以认为是默认成员函数。

class Date
{
public:
	Date()
	{
		_year = 1900;
		_month = 1;
		_day = 1;
	}

	Date(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
private:
	int _year;
	int _month;
	int _day;
};
// 以下测试函数能通过编译吗?不能,只能有一个默认构造函数。
void Test()
{
	Date d1;
}
int main()
{
	Test();
	return 0;
}

4.C++把类型分成内置类型(基本类型)和自定义类型

内置类型就是语法已经定义好的类型:如int/char...,自定义类型就是我们使用class/struct/union自己定义的类型,看看下面的程序,就会发现编译器生成默认的构造函数会对自定类型成员_t调用的它的默认成员函数。

class Time
{
public:
	Time()
	{
		cout << "Time()" << endl;
		_hour = 0;
		_minute = 0;
		_second = 0;
 }
private:
	int _hour;
	int _minute;
	int _second;
};
class Date
{
private:
	// 基本类型(内置类型)
	int _year;
	int _month;
	int _day;
	// 自定义类型
	Time _t;
};
int main()
{
	Date d;
	return 0;
}

析构函数

1.特性:是特殊的成员函数

typedef int DataType;
class SeqList
{
public:
	SeqList(int capacity = 10)
	{
		_pData = (DataType*)malloc(capacity * sizeof(DataType));
		assert(_pData);
		_size = 0;
		_capacity = capacity;
 }

 ~SeqList()
 {
	 if (_pData)
	 {
		 free(_pData); // 释放堆上的空间
		 _pData = NULL; // 将指针置为空
		 _capacity = 0;
		 _size = 0;
	 }
 }

private:
	int* _pData;
	size_t _size;
	size_t _capacity;
};

2.编译器生成的默认析构函数,会对自定类型成员调用它的析构函数

{
public:
	String(const char* str = "kkkkkkkkkkkkkkkk")
	{
		_str = (char*)malloc(strlen(str) + 1);
		strcpy(_str, str);
	}
	~String()
	{
		cout << "~String()" << endl;
		free(_str);
	}
private:
	char* _str;
};
class Person
{
private:
	String _name;
	int _age;
};
int main()
{
	Person p;
	return 0;
}

拷贝构造函数

1.拷贝构造函数也是特殊的成员函数

class Date
{
public:
	Date(int year = 2000, 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;
 }
	void Display()
	{
		cout << _year << " " << _month << " " << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date d1;
	Date d2(d1);
	d1.Display();//Display后面必须要有(),否则会编译不通过
	d2.Display();
	return 0;
}

 2.若未显示定义,系统生成默认的拷贝构造函数。 

默认的拷贝构造函数对象按内存存储按字节序完成拷贝,这种拷贝我们叫做浅拷贝,或者值拷贝。那么编译器生成的默认拷贝构造函数已经可以完成字节序的值拷贝了,我们还需要自己实现吗?

答案是显然的!

class String
{
public:
	String(const char* str = "kkkkk")
	{
		_str = (char*)malloc(strlen(str) + 1);
		strcpy(_str, str);
	}
	~String()
	{
		cout << "~String()" << endl;
		free(_str);
	}
private:
	char* _str;
};
int main()
{
	String s1("hello");
	String s2(s1);
}

可以证明↓

赋值操作符重载

1.注意

1.不能通过连接其他符号来创建新的操作符:比如 operator@
2.重载操作符必须有一个类类型或者枚举类型的操作数
3.用于内置类型的操作符,其含义不能改变,例如:内置的整型 + ,不 能改变其含义
4. 作为类成员的重载函数时,其形参看起来比操作数数目少 1成员函数的操作符有一个默认的形参 this,限定为第一个形参
5..* :: sizeof ?: . 注意以上 5 个运算符不能重载

2.==重载

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

	// bool operator==(Date* this, const Date& d2)
	// 这里需要注意的是,左操作数是this指向的调用函数的对象
	bool operator==(const Date& d)
	{
		return _year == d._year
		&& _month == d._month
			&& _day == d._day;
	}
private:
	int _year;
	int _month;
	int _day;
};
void Test()
{
	Date d1(2018, 9, 26);
	Date d2(2018, 9, 27);
	cout << (d1 == d2) << endl;
}
int main()
{
	Test();
	return 0;
}

不相等↑ 

3.赋值运算符重载

1.注意

1. 参数类型
2. 返回值
3. 检测是否自己给自己赋值
4. 返回 *this
5. 一个类如果没有显式定义赋值运算符重载,编译器也会生成一个,完成对象按字节序的值拷贝

2.代码实现

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

	Date& operator=(const Date& d)
	{
		if (this != &d)
		{
			_year = d._year;
			_month = d._month;
			_day = d._day;
		}
		return *this;
	}
	void Display()
	{
		cout << _year << " " << _month << " " << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date d1(2022, 4, 22);
	Date d2;
	d2 = d1;
	d1.Display();
	d2.Display();
	return 0;
}

默认拷贝构造与赋值运算符重载的问题

1. const 对象可以调用非 const 成员函数吗?
答:不可以!
2. const 对象可以调用 const 成员函数吗?
答:可以!
3. const 成员函数内可以调用其它的非 const 成员函数吗?
答:不可以!
4. const 成员函数内可以调用其它的 const 成员函数吗?
答:可以!
综上:只要不需要修改成员变量,则都加const。

const成员函数

class Date
{ 
public :
 Date* operator&()
 {
 return this ;
 }
 
 const Date* operator&()const
 {
 return this ;
 }
private :
 int _year ; // 年
 int _month ; // 月
 int _day ; // 日
};

一般无需定义,编译器默认生成!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

**K

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值