C++中的类和对象

目录

一.面向过程和面相对象的初步认识

1.1 C语言中的面相过程

1.2 C++中的面向对象

 二.类的引入

三.类的定义

3.1 类的基本结构

3.2类的两种定义方式

3.2.1 声明和定义全部放在类体中

3.2.2 类声明放在.h文件中

3.3 成员变量命名规则的建议

3.3.1 错误示例

3.3.2 方法1

3.3.3 方法2

四.类的访问限定符及封装

4.1 访问限定符

  4.1.1 访问限定符的作用

 4.1.2 访问限定符说明

4.1.3  面试题

4.2 封装

4.2.1 引例

4.2.2 封装的定义

4.2.3 封装的本质

五.类的作用域(类域)

  5.1 类域的概念

六.类的实例化

七.类对象模型

  7.1 如何计算类的大小

7.2 类对象的存储方式猜测

7.2.1 类对象中包含类的每个成员(全部成员)

7.2.2 代码只保存一份,在对象中保存存放代码的地址

 7.2.3 只保存成员变量,成员函数存放在公共的代码段

7.3 结构体对齐的规则

八.this指针

8.1 this指针的引出

8.2 this指针的特性

8.3. C语言和C++实现Stack的对比

8.3.1 C语言实现

8.3.2 C++实现

九.类的6个默认成员函数

9.1 默认成员函数的引入

9.2 构造函数

9.2.1 构造函数的概念

 9.2.2 构造函数的特性

​编辑

9.3 析构函数

9.3.1 概念

9.3.2 析构函数的特性

9.4 拷贝构造函数

9.4.1 拷贝构造函数的概念

9.4.2 拷贝构造函数的特性

 9.5 赋值运算符重载

  9.5.1 赋值运算符重载的概念

9.5.2 函数运算符重载格式

 9.5.3  前置++和后置++重载

 9.6 日期类的完全实现:

9.7 const成员

9.8 取地址及const取地址操作符重载

十.再谈构造函数

 10.1 构造函数体赋值

10.2 初始化列表

10.3 初始化列表的用途

10.3 explicit关键字

十一. static成员 

11.1 概念

 11.2 特性

十二.友元

12.1 友元的概念

12.2 友元函数

12.3 友元类

十三  内部类

十四 匿名对象

十五  拷贝对象时的一些编译器优化

 十六  再次理解类和对象


一.面向过程和面相对象的初步认识

1.1 C语言中的面相过程

  在C语言中我们最多关注的是过程,我们通过编写函数,也就是一个个过程,逐步对问题求解。

  比如说写个送外卖过程:

 我们要写出以下几个函数:

1.2 C++中的面向对象

 而在C++中,关注的是对象,C++是一门基于面对对象的语言,它是指将一个事情分配给多个对象(或将多个函数多个分配给不同的对象),通过对象之间的交互完成程序。

比如在C++中的外卖系统:

外卖系统总共有三个对象:吃饭的人,商家,骑手

整个的过程可以划分为:吃饭的人点外卖,商家做饭,骑手配送。

注意:整个过程是吃饭的人,商家,骑手之间的关系完成的,比如说吃饭的人不需要关注商家是怎么做饭的(实际上还是建议关注一下,吃坏肚子就不好了),商家也不需要关注骑手是怎么配送的。

如:

总结:对象是一个实体,一个过程中我们能看到的所有实体都可以称为对象。

而我们一般用类来描述这个对象,类是用来对实体(对象)进行描述的。(比如说:对象有什么属性,有什么功能,可以做什么)

 二.类的引入

  C语言结构体中只能定义变量,在C++中,结构体内不仅可以定义变量,也可以定义函数。

比如:
    当我们在c语言中的结构体中定义一个函数时候:

   编译器会提醒我们不允许定义函数类型。

  当我们在C++中时,sturct中可以定义函数。

#include<iostream>
using std::cout;
using std::endl;
using std::cin;
struct Date
{
	void Print()
	{
		cout << _year << "/" << _month << "/" << _day << endl;
	}
	int _year = 2022;
	int _month= 4;
	int _day = 14;

};
int main()
{
	return 0;
}

  这段代码在C++中便没有语法错误,因此C++中的struct中是可以定义函数类型的。

  为什么呢?

  因为在C++中,struct是作为一个类的。

三.类的定义

3.1 类的基本结构

class className
{
	// 类体:由成员函数和成员变量组成
}; // 一定要注意后面的分号

  class为定义类的关键字,ClassName为类的名字,{}中为类的主体,注意类定义结束时后面分
号不能省略

  类体中内容称为类的成员:类中的变量称为类的属性或成员变量; 类中的函数称为类的方法或者
成员函数。


3.2类的两种定义方式


3.2.1 声明和定义全部放在类体中

需注意:成员函数如果在类中定义,编译器可能会将其当成内联函数处理。

如:
 

//声明和定义全放在类中
class Date
{
public:
	//打印时间函数
	void Print()
	{
		cout << _year << "/" << _month << "/" << _day << endl;
	}

private:

	int _year;//年
	int _month;//月
	int _day;//日

};

3.2.2 类声明放在.h文件中

成员函数定义放在.cpp文件中,注意:成员函数名前需要加  类名::  (这个属于类域的知识,在下面展开详细解释)。

此时.h文件中的内容为


//.h文件
#pragma once
#include<iostream>
using std::cout;
using std::endl;
using std::cin;
//声明和定义全放在类中
class Date
{
public:
	//打印时间函数
	void Print();
private:

	int _year;//年
	int _month;//月
	int _day;//日

};

此时.cpp文件中的内容为:

//.cpp文件中的内容为:
#include"Date.h"
void Date::Print()
{
	cout << _year << "/" << _month << "/" << _day << endl;
}

int main()
{
	return 0;
}

注意:一般情况下在写项目时大多采用声明和定义分离的方式,即第二种方式,这种方式好处很多,但这里不展开过多描述,建议大家使用第二种方法就可以了。

3.3 成员变量命名规则的建议

3.3.1 错误示例

请大家来看以下代码

class Date
{
public:
	void Init(int year)
	{
     // 这里的year到底是成员变量,还是函数形参?
     //我到底在给哪个参数赋值呢?
		year = year;
	}

private:
	int year;
	int month;
	int day;

};

为了避免这种错误,我们一般建议采用以下两种命名方法:

3.3.2 方法1

class Date
{
public:
	void Init(int year)
	{
		_year = year;
	}
private:
	int _year;
};

3.3.3 方法2

class Date
{
public:
	void Init(int year)
	{
		mYear = year;
	}
private:
	int mYear;
};

注意:其它方法也可以,主要是看个人习惯和公司要求,但是一定要能区分类中的成员变量和函数传参的区别。

四.类的访问限定符及封装

4.1 访问限定符

  4.1.1 访问限定符的作用

  C++实现封装的方式:用类将对象的属性与方法结合在一块,让对象更加完善,通过访问限定符设置访问权限选择性的将其接口提供给外部的用户使用。

  在C++中,访问限定符一共有三个:

 

 4.1.2 访问限定符说明

1. public修饰的成员在类外可以直接被访问。
2. protected和private修饰的成员在类外不能直接被访问(此处protected和private是类似的)。
3. 访问权限作用域从该访问限定符出现的位置开始直到下一个访问限定符出现时为止。
4. 如果后面没有访问限定符,作用域就到} 即类结束。
5. class的默认访问权限为private,struct为public(因为struct要兼容C)。

注意:访问限定符只在编译时有用,当数据映射到内存后,没有任何访问限定符上的区别。

4.1.3  面试题

问题:C++中的struct和class的区别是什么

答:

解答:C++需要兼容C语言,所以C++中struct可以当成结构体使用。另外C++中struct还可以用来定义类。和class定义类是一样的,区别是struct定义的类默认访问权限是public,class定义的类默认访问权限是private。

注意:在继承和模板参数列表位置,struct和class也有区别,后序学到继承和模板再给大家介绍。

4.2 封装

4.2.1 引例

面试题:

面向对象的三大特性:封装、继承、多态。


在类和对象阶段,主要是研究类的封装特性,那什么是封装呢?

4.2.2 封装的定义

  封装:将数据和操作数据的方法进行有机结合,隐藏对象的属性和实现细节,仅对外公开接口来
 和对象进行交互。

4.2.3 封装的本质

  封装本质上是一种管理,让用户更方便使用类。

  比如:对于电脑这样一个复杂的设备,提供给用户的就只有开关机键、通过键盘输入,显示器,USB插孔等,让用户和计算机进行交互,完成日常事务。但实际上电脑真正工作的却是CPU、显卡、内存等一些硬件元件。

 

 

  对于计算机使用者而言,不用关心内部核心部件,比如主板上线路是如何布局的,CPU内部是如
何设计的等,用户只需要知道,怎么开机、怎么通过键盘和鼠标与计算机进行交互即可。

  因此计算机厂商在出厂时,在外部套上壳子,将内部实现细节隐藏起来,仅仅对外提供开关机、鼠标以及键盘插孔等,让用户可以与计算机进行交互即可。 

    一方面是因为计算机使用者用不到,另一方面也是为了保护计算机,同时提高计算机的整体利用效率。
  在C++语言中实现封装也是同样的道理,可以通过类将数据以及操作数据的方法进行有机结合,通过访问权限来隐藏对象内部实现细节,控制哪些方法可以在类外部直接被使用,在实现了数据的保护的同时,还能更好的提高程序的性能

五.类的作用域(类域)

  5.1 类域的概念

    类定义了一个新的作用域,类的所有成员都在类的作用域中。在类体外定义成员时,需要使用 ::
作用域操作符指明成员属于哪个类域。

 比如说:
 

class Date 
{
public:
	void Print();
private:
	int _year;
	int _month;
	int _day;
};


//这里便需要指明Print在哪个作用域
void Date::Print()
{

   cout << _year << "/" << _month << "/" << _day << endl;
	
}

六.类的实例化

  用类类型创建对象的过程,称为类的实例化
  1. 类是对对象进行描述的,是一个模型一样的东西,限定了类有哪些成员,定义出一个类并没
有分配实际的内存空间来存储它。

比如:入学时填写的学生信息表,表格就可以看成是一个类,来描述具体学生信息。
 
  2. 一个类可以实例化出多个对象,实例化出的对象 占用实际的物理空间,存储类成员变量。

  3. 做个比方。类实例化出对象就像现实中使用建筑设计图建造出房子,类就像是设计图,只设
计出需要什么东西,但是并没有实体的建筑存在,同样类也只是一个设计,实例化出的对象
才能实际存储数据,占用物理空间。

 

 如:

#include"Date.h"
class Date
{
public:
	void Print();

	int _year;
	int _month ;
	int _day ;

};

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

int main()
{
	//创建两个Date类的变量d1,d2
	Date d1;
	Date d2;

	//分别赋值打印
	d1._year = 2023;
	d1._month = 4;
	d1._day = 15;
	d1.Print();

	d2._year = 2023;
	d2._month = 4;
	d2._day = 15;
	d2.Print();
	return 0;
}

但如果我们这样:

 编译器就会提示我们出错。

可见:Date类是没有空间的,只是一种模板,只有Date类实例化出的对象才有具体的分配空间。

七.类对象模型

  7.1 如何计算类的大小

  比如说这段代码:

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

	int _year;
	int _month;
	int _day;

};

  问题:类中既可以有成员变量,又可以有成员函数,那么一个类的对象中包含了什么?如何计算
一个类的大小?

  这里先给大家提供一个小的知识点:用sizeof运算符对一个类型名操作,得到的是具有该类型实体的大小,但类实际上是不占用存储空间的。

7.2 类对象的存储方式猜测

7.2.1 类对象中包含类的每个成员(全部成员)

 缺陷:每个对象中成员变量是不同的,但是调用同一份函数,如果按照此种方式存储,当一
个类创建多个对象时,每个对象中都会保存一份代码,相同代码保存多次,浪费空间。那么
如何解决呢?

7.2.2 代码只保存一份,在对象中保存存放代码的地址

 7.2.3 只保存成员变量,成员函数存放在公共的代码段

 问题:对于上述三种存储方式,那计算机到底是按照那种方式来存储的?
我们再通过对下面的不同对象分别获取大小来分析看下

比如:

// 类中既有成员变量,又有成员函数
class A1 {
public:
	void f1()
	{

	}
private:
	int _a;
};


// 类中仅有成员函数
class A2 
{
public:
	void f2() 
	{
	}
};

// 类中什么都没有---空类
class A3
{

};

int main()
{
	cout<<sizeof(A1)<<endl;
	cout << sizeof(A2) << endl;
	cout << sizeof(A3) << endl;

	return 0;
}

 这段代码的输出结果为什么呢?

答案为:

 结论:一个类的大小,实际就是该类中”成员变量”之和,当然要注意内存对齐。

同时对A2的结果 我们也可以得出一个结论:类对象的存储采用第三种方式
注意空类的大小,空类比较特殊,编译器给了空类一个字节来唯一标识这个类的对象

7.3 结构体对齐的规则

1. 第一个成员在与结构体偏移量为0的地址处。
2. 其他成员变量要对齐到某个数字(对齐数)的整数倍的地址处。

注意:对齐数 = 编译器默认的一个对齐数 与 该成员大小的较小值。

3. 结构体总大小为:最大对齐数(所有变量类型最大者与默认对齐参数取最小)的整数倍。
4. 如果嵌套了结构体的情况,嵌套的结构体对齐到自己的最大对齐数的整数倍处,结构体的整体大小就是所有最大对齐数(含嵌套结构体的对齐数)的整数倍。

VS中默认的对齐数为8

默认对齐数可通过修改:

#pragma pack(num)

num即为你想修改的默认对齐数

不能随意的修改默认对齐数应该根据所需,如当修改成了#pragma pack( 1 )这样其实也就不存在内存对齐了

就会导致这时的对齐数一定都是1

所以当对齐方式不合适时,我们就可以自行修改默认对齐数

【面试题】
1. 结构体怎么对齐? 为什么要进行内存对齐?
2. 如何让结构体按照指定的对齐参数进行对齐?能否按照3、4、5即任意字节对齐?
3. 什么是大小端?如何测试某台机器是大端还是小端,有没有遇到过要考虑大小端的场景

八.this指针

8.1 this指针的引出

  我们先来定义一个日期类Date。

class Date
{
public:
	void Init(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year; // 年
	int _month; // 月
	int _day; // 日
};
int main()
{
	Date d1, d2;
	d1.Init(2023, 4, 14);
	d2.Init(2023, 4, 15);
	d1.Print();
	d2.Print();
	return 0;
}

 对于上述类,有这样的一个问题:


  Date类中有 Init 与 Print 两个成员函数,函数体中没有关于不同对象的区分,那当d1调用 Init 函
数时,该函数是如何知道应该设置d1对象,而不是设置d2对象呢?


  C++中通过引入this指针解决该问题,即:C++编译器给每个“非静态的成员函数“增加了一个隐藏
的指针参数,让该指针指向当前对象(函数运行时调用该函数的对象),在函数体中所有“成员变量”
的操作,都是通过该指针去访问。只不过所有的操作对用户是透明的,即用户不需要来传递,
译器自动完成

8.2 this指针的特性

1. this指针的类型:类类型* const,即成员函数中,不能给this指针赋值
2. 只能在“成员函数”的内部使用
3. this指针本质上是“成员函数”的形参,当对象调用成员函数时,将对象地址作为实参传递给
this形参。所以对象中不存储this指针。
4. this指针是“成员函数”第一个隐含的指针形参,一般情况由编译器通过ecx寄存器自动传
递,不需要用户传递。

 我们画图来理解一下:
  比如说Print这段代码:

 【面试题】
1. this指针存在哪里?
2. this指针可以为空吗?

我们接下来来看一下两段代码。

#include"Date.h"
// 1.下面程序编译运行结果是? A、编译报错 B、运行崩溃 C、正常运行
class A
{
public:
	void Print()
	{
		cout << "Print()" << endl;
	}
private:
	int _a;
};
int main()
{
	A* p = nullptr;
	p->Print();
	return 0;
}

 代码结果为:

 显而易见是可以正常运行的,那为什么呢?

我们先来看一下这段代码

// 1.下面程序编译运行结果是? A、编译报错 B、运行崩溃 C、正常运行
class A
{
public:
	void PrintA()
	{
		cout << _a << endl;
	}
private:
	int _a;
};
int main()
{
	A* p = nullptr;
	p->PrintA();
	return 0;
}

代码结果为:运行出错

为什么呢?

解释: 从前面对象存储方式的讲解中我们知道了成员函数是存储在公共代码段,当我们使用对象调用函数时(受到类域限制,虽然函数存在公共代码段,但需要使用 对象.成员函数 的方法才能进行调用,不能直接调用成员函数),不需要在对象里面进行查找,只需要在公共代码段中查找,反映在汇编指令上即是通过 call 指令跳转到函数所在地址处。两段代码的区别在于,第一段代码中调用的成员函数中没有用this指针访问对象中成员变量的操作,因此运行函数时,并没有用this指针进行解引用操作,也就不会有访问冲突和空指针异常了;而第二段代码中调用的成员函数中使用this指针进行了解引用来访问对象中的成员变量,而this指针又是空指针,无法进行解引用,因此发生了空指针异常,程序运行崩溃。事实上,我们通过对象指针去访问对象的成员函数时,是否有对this指针进行解引用,不是看有没有解引用符号(-> 或 *),而要看程序运行时是否需要到对象中进行查找,若是需要,则表示有解引用,反之不然。

总结:其实者属于是个指针的小知识点,当我们对空指针解引用时,才会陷入访问冲突和空指针异常 ,而我们并不对空指针解引用时,程序将不会报错。

8.3. C语言和C++实现Stack的对比

8.3.1 C语言实现

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
typedef int DataType;
typedef struct Stack
{
	DataType* array;
	int capacity;
	int size;
}Stack;
void StackInit(Stack* ps)
{
	assert(ps);
	ps->array = (DataType*)malloc(sizeof(DataType) * 3);
	if (NULL == ps->array)
	{
		assert(0);
		return;
	}ps->capacity = 3;
	ps->size = 0;
}
void StackDestroy(Stack* ps)
{
	assert(ps);
	if (ps->array)
	{
		free(ps->array);
		ps->array = NULL;
		ps->capacity = 0;
		ps->size = 0;
	}
}
void CheckCapacity(Stack* ps)
{
	if (ps->size == ps->capacity)
	{
		int newcapacity = ps->capacity * 2;
		DataType* temp = (DataType*)realloc(ps->array,
			newcapacity * sizeof(DataType));
		if (temp == NULL)
		{
			perror("realloc申请空间失败!!!");
			return;
		}
		ps->array = temp;
		ps->capacity = newcapacity;
	}
}
void StackPush(Stack* ps, DataType data)
{
	assert(ps);
	CheckCapacity(ps);
	ps->array[ps->size] = data;
	ps->size++;
}
int StackEmpty(Stack* ps)
{
	assert(ps);
	return 0 == ps->size;
}
void StackPop(Stack* ps)
{
	if (StackEmpty(ps))
		return;
	ps->size--;
}
DataType StackTop(Stack* ps)
{
	assert(!StackEmpty(ps));
	return ps->array[ps->size - 1];
}
int StackSize(Stack* ps)
{
	assert(ps);
	return ps->size;
}
int main()
{
	Stack s;
	StackInit(&s);
	StackPush(&s, 1);
	StackPush(&s, 2);
	StackPush(&s, 3);
	StackPush(&s, 4);
	printf("%d\n", StackTop(&s));
	printf("%d\n", StackSize(&s));
	StackPop(&s);
	StackPop(&s);
	printf("%d\n", StackTop(&s));
	printf("%d\n", StackSize(&s));
	StackDestroy(&s);
	return 0;
}

可以看到,在用C语言实现时,Stack相关操作函数有以下共性:


1.每个函数的第一个参数都是Stack*
2.函数中必须要对第一个参数检测,因为该参数可能会为NULL
3.函数中都是通过Stack*参数操作栈的
4.调用时必须传递Stack结构体变量的地址


结构体中只能定义存放数据的结构,操作数据的方法不能放在结构体中,即数据和操作数据
的方式是分离开的,而且实现上相当复杂一点,涉及到大量指针操作,稍不注意可能就会出
错。

8.3.2 C++实现

#include<iostream>
using std::cout;
using std::endl;
using std::cin;

typedef int DataType;
class Stack
{
public:
	void Init()
	{
		_array = (DataType*)malloc(sizeof(DataType) * 3);
		if (NULL == _array)
		{
			perror("malloc申请空间失败!!!");
			return;
		}
		_capacity = 3;
		_size = 0;
	}
	void Push(DataType data)
	{
		CheckCapacity();
		_array[_size] = data;
		_size++;
	}
	void Pop()
	{
		if (Empty())
			return;
		_size--;
	}
	DataType Top() { return _array[_size - 1]; }
	int Empty() { return 0 == _size; }
	int Size() { return _size; }
	void Destroy()
	{
		if (_array)
		{
			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 s;
	s.Init();
	s.Push(1);
	s.Push(2);
	s.Push(3);
	s.Push(4);
	printf("%d\n", s.Top());
	printf("%d\n", s.Size());
	s.Pop();
	s.Pop();
	printf("%d\n", s.Top());
	printf("%d\n", s.Size());
	s.Destroy();
	return 0;
}

  C++中通过类可以将数据 以及 操作数据的方法进行完美结合,通过访问权限可以控制那些方法在类外可以被调用,即封装,在使用时就像使用自己的成员一样,更符合人类对一件事物的认知。

  而且每个方法不需要传递Stack*的参数了,编译器编译之后该参数会自动还原,即C++中 Stack *参数是编译器维护的,C语言中需用用户自己维护。

九.类的6个默认成员函数

9.1 默认成员函数的引入

  空类:如果一个类中什么都没有,即简称为空类。

  但空类中真的什么都没有吗?并不是,当任何类在什么都不写时,默认生成六个成员函数

如:

 默认成员函数:用户没有显式实现,编译器会生成的成员函数称为默认成员函数。

9.2 构造函数

9.2.1 构造函数的概念

  对以下程序而言:

class Date
{
public:
	void Init(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year; // 年
	int _month; // 月
	int _day; // 日
};
int main()
{
	Date d1, d2;
	d1.Init(2023, 4, 14);
	d2.Init(2023, 4, 15);
	d1.Print();
	d2.Print();
	return 0;
}

对于Date类,可以通过 Init 公有方法给对象设置日期,但如果每次创建对象时都调用该方法设置信息,未免有点麻烦,那能否在对象创建时,就将信息设置进去呢?


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

 9.2.2 构造函数的特性

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

  构造函数的特性:

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

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

如:

#include<iostream>
using std::cout;
using std::cin;
using std::endl;
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();// warning C4930: “Date d3(void)”: 未调用原型函数(是否是有意用变量定义的?)
}
int main()
{
	TestDate();
	return 0;
}

Date d3();实际上是个函数声明

 关于编译器自动生成的无参的默认构造函数:

#include<iostream>
using std::endl;
using std::cout;
using std::cin;
class Date
{
public:
	/*
	// 如果用户显式定义了构造函数,编译器将不再生成
	Date(int year, int month, int day)
	{
	_year = year;
	_month = month;
	_day = day;
	}
	*/
	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	// 将Date类中构造函数屏蔽后,代码可以通过编译,因为编译器生成了一个无参的默认构造函数


	Date d1;
	d1.Print();
	return 0;
}

代码结果为:

 我们来看另一段代码

#include<iostream>
using std::endl;
using std::cout;
using std::endl;
class Date
{
public:
	
	// 如果用户显式定义了构造函数,编译器将不再生成
	Date(int year, int month, int day)
	{
	_year = year;
	_month = month;
	_day = day;
	}
	
	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	// 将Date类中构造函数放开,代码编译失败,因为一旦显式定义任何构造函数,编译器将不再生成
   //这段代码会报错,因为我们没有定义无产的构造函数
	Date d1;
	d1.Print();
	return 0;
}

 代码结果为:

  关于编译器生成的默认成员函数,很多童鞋会有疑惑:不实现构造函数的情况下,编译器会
生成默认的构造函数。但是看起来默认构造函数又没什么用?d1对象调用了编译器生成的默
认构造函数,但是d1对象_year/_month/_day,依旧是随机值。也就说在这里编译器生成的
默认构造函数并没有什么用??
  解答:C++把类型分成内置类型(基本类型)自定义类型。内置类型就是语言提供的数据类
型,如:int/char...,自定义类型就是我们使用class/struct/union等自己定义的类型.

  下面这段代码是上段文字的案例:

#include<iostream>
using std::endl;
using std::cout;
using std::cin;
class Date
{
public:

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

	Date d1;
	d1.Print();
	return 0;
}

代码结果为:

  而看看下面的程序,就会发现编译器生成默认的构造函数会对自定类型成员_t调用的它的默认成员函数。

#include<iostream>
using std::endl;
using std::cout;
using std::cin;
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;
}

代码结果为:

其中d中的 _t值为:

代码剖析:

 注意:C++11 中针对内置类型成员不初始化的缺陷,又打了补丁,即:内置类型成员变量在
类中声明时可以给默认值。

  如 :

#include<iostream>
using std::endl;
using std::cout;
using std::cin;
class Time
{
public:
	Time()
	{
		cout << "Time()" << endl;
		_hour = 0;
		_minute = 0;
		_second = 0;
	}

private:
	int _hour;
	int _minute;
	int _second;
};


class Date
{
	public:
		void Print()
		{
			cout << _year << "-" << _month << "-" << _day << endl;
		}
private:
	int _year=2023;//在声明中给默认值
	int _month=4;
	int _day=15;
	Time _t;
};
int main()
{
	Date d;
	return 0;
}

代码结果为:

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


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

如 下面这段错误的代码

#include<iostream>
class Date
{
public:
	Date()
	{
		_year = 2023;
		_month = 4;
		_day = 15;
	}
	Date(int year = 2023, int month = 4, int day = 15)
	{
		_year = year;
		_month = month;
		_day = day;
	}

private:
	int _year;
	int _month;
	int _day;

};

int main()
{
	Date d1;
	return 0;
}

 这段代码是通不过编译的,原因便是我们定义了多个构造函数:

9.3 析构函数

9.3.1 概念

  通过前面构造函数的学习,我们知道一个对象是怎么来的,那一个对象又是怎么没呢的?


析构函数:与构造函数功能相反,析构函数不是完成对对象本身的销毁,局部对象销毁工作是由编译器完成的。而对象在销毁时会自动调用析构函数,完成对象中资源的清理工作(大多数为堆区的资源)。

9.3.2 析构函数的特性

析构函数是特殊的成员函数,其特征如下:

1. 析构函数名是在类名前加上字符 ~
2. 无参数无返回值类型
3. 一个类只能有一个析构函数。若未显式定义,系统会自动生成默认的析构函数。注意:析构函数不能重载
4. 对象生命周期结束时,C++编译系统系统自动调用析构函数

下面我们来写个简短的栈的代码来举一下例子:

#include<iostream>
using std::endl;
using std::cout;
using std::endl;
typedef int DataType;
class Stack
{
public:
	Stack(size_t capacity = 3)
	{
		_array = (DataType*)malloc(sizeof(DataType) * capacity);
		if (NULL == _array)
		{
			perror("malloc申请空间失败!!!");
			return;
		}
		_capacity = capacity;
		_size = 0;
	}
	void Push(DataType data)
	{
		// CheckCapacity();
		_array[_size] = data;
		_size++;
	}
	// 其他方法...
	~Stack()//栈的析构函数
	{
		cout << "~Stack()" << endl;
		if (_array)
		{
			free(_array);
			_array = NULL;
			_capacity = 0;
			_size = 0;
		}
	}
private:
	DataType* _array;
	int _capacity;
	int _size;
};
void TestStack()
{
	Stack s;
	s.Push(1);
	s.Push(2);
}
int main()
{
	TestStack();
	return 0;
}

 代码结果为:

 可以看出:在栈的类定义中,析构函数其实就是取代了以往C语言中我们写的栈的销毁函数,也就是说,析构函数其实就是一种特定的销毁函数

 通过第九章开头我们可以得知,编译器自动也会生成析构函数。,关于编译器自动生成的析构函数,是否会完成一些事情呢?下面的程序我们会看到,编译器生成的默认析构函数,对自定类型成员调用它的析构函数。

比如说下面这段代码:

#include<iostream>
using std::cout;
using std::cin;
using std::endl;
class Time
{
public:
	~Time()
	{
		cout << "~Time()" << endl;
	}
private:
	int _hour;
	int _minute;
	int _second;
};
class Date
{
private:
	// 基本类型(内置类型)
	int _year = 1970;
	int _month = 1;
	int _day = 1;
	// 自定义类型
	Time _t;
};
int main()
{
	Date d;
	return 0;
}

输出结果为:

  在main方法中根本没有直接创建Time类的对象,为什么最后会调用Time类的析构函数?


 因为:main方法中创建了Date对象d,而d中包含4个成员变量,其中_year, _month,_day三个是内置类型成员,销毁时不需要资源清理,最后系统直接将其内存回收即可;而_t是Time类对象,所以在 d销毁时,要将其内部包含的Time类的_t对象销毁,所以要调用Time类的析构函数。

但是:main函数 中不能直接调用Time类的析构函数,实际要释放的是Date类对象,所以编译器会调用Date类的析构函数,而Date没有显式提供,则编译器会给Date类生成一个默认的析构函数,目的是在其内部调用Time类的析构函数,即当Date对象销毁时,要保证其内部每个自定义对象都可以正确销毁。

main函数中并没有直接调用Time类析构函数,而是显式调用编译器为Date类生成的默认析构函数。


 注意:创建哪个类的对象则调用该类的构造函数,销毁那个类的对象则调用该类的析构函数

注意: 如果类中没有申请资源时,析构函数可以不写,直接使用编译器生成的默认析构函数,比如Date类;有资源申请时,一定要写,使用指针将申请的空间释放,否则会造成资源泄漏,比如Stack类(中有用malloc申请的空间,必须销毁)。

9.4 拷贝构造函数

9.4.1 拷贝构造函数的概念

在现实生活中,可能存在一个与你一样的自己,我们称其为双胞胎。

 那在创建对象时,可否创建一个与已存在对象一某一样的新对象呢?


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

9.4.2 拷贝构造函数的特性

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

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

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

如下面这段错误代码:

#include<iostream>
using std::cout;
using std::endl;
using std::cin;
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;
}

为什么说他会引发无限递归呢?很简单。

 详细过程:

 正确的拷贝构造函数:

#include<iostream>
using std::cout;
using std::endl;
using std::cin;
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;
	}
	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date d1;
	Date d2(d1);
	d1.Print();
	d2.Print();
	return 0;
}

 代码结果为:

 3. 若未显式定义,编译器会生成默认的拷贝构造函数。 默认的拷贝构造函数对象按内存存储按字节序完成拷贝,这种拷贝叫做浅拷贝,或者值拷贝。

比如说下面的代码:
 

#include<iostream>
using std::cout;
using std::endl;
using std::cin;
class Time
{
public:

	Time()
	{
		_hour = 1;
		_minute = 1;
		_second = 1;
}
Time(const Time& t)
{
	_hour = t._hour;
	_minute = t._minute;
	_second = t._second;
	cout << "Time::Time(const Time&)" << endl;
}
private:
	int _hour;
	int _minute;
	int _second;
};
class Date
{
private:
	// 基本类型(内置类型)
	int _year = 1970;
	int _month = 1;
	int _day = 1;
	// 自定义类型
	Time _t;
};
int main()
{
	Date d1;
	// 用已经存在的d1拷贝构造d2,此处会调用Date类的拷贝构造函数
	// 但Date类并没有显式定义拷贝构造函数,则编译器会给Date类生成一个默认的拷贝构造函数
	Date d2(d1);//此时便会对Date类中的默认拷贝构造函数进行调用
	return 0;
}

 注意:在编译器生成的默认拷贝构造函数中,内置类型是按照字节方式直接拷贝的,而自定
义类型是调用其拷贝构造函数完成拷贝的。

 4.那么我们为什么要写拷贝构造函数呢?

我们来看下面这段代码:

#include<iostream>
using std::endl;
using std::cout;
using std::cin;
// 这里会发现下面的程序会崩溃掉?这里就需要我们以后讲的深拷贝去解决。
typedef int DataType;
class Stack
{
public:
	Stack(size_t capacity = 10)
	{
		_array = (DataType*)malloc(capacity * sizeof(DataType));
		if (nullptr == _array)
		{
			perror("malloc申请空间失败");
			return;
		}
		_size = 0;
		_capacity = capacity;
	}
	void Push(const DataType& data)
	{
		// CheckCapacity();
		_array[_size] = data;
		_size++;
	}
	~Stack()
	{
		if (_array)
		{
			free(_array);
			_array = nullptr;
			_capacity = 0;
			_size = 0;
		}
	}
private:
	DataType* _array;
	size_t _size;
	size_t _capacity;
};
int main()
{
	Stack s1;
	s1.Push(1);
	s1.Push(2);
	s1.Push(3);
	s1.Push(4);
	Stack s2(s1);
	return 0;
}

 原因如下:

 注意:类中如果没有涉及资源申请时,拷贝构造函数是否写都可以;一旦涉及到资源申请
时,则拷贝构造函数是一定要写的
,否则就是浅拷贝。

 5. 拷贝构造函数典型调用场景:

1.使用已存在对象创建新对象.
2.函数参数类型为类类型对象.
3.函数返回值类型为类类型对象.

 如:

#include<iostream>
using std::cout;
using std::endl;
using std::cin;
class Date
{
public:
	Date(int year, int minute, int day)
	{
		cout << "Date(int,int,int):" << this << endl;
	}
	Date(const Date& d)
	{
		cout << "Date(const Date& d):" << this << endl;
	}
	~Date()
	{
		cout << "~Date():" << this << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
Date Test(Date d)
{
	Date temp(d);
	return temp;
}
int main()
{
	Date d1(2022, 1, 13);
	Test(d1);
	return 0;
}

代码解析:

 为了提高程序效率,一般对象传参时,尽量使用引用类型,返回时根据实际场景,能用引用
尽量使用引用。

 9.5 赋值运算符重载

  9.5.1 赋值运算符重载的概念

C++为了增强代码的可读性引入了运算符重载,运算符重载是具有特殊函数名的函数,也具有其
返回值类型,函数名字以及参数列表,其返回值类型和参数列表与普通的函数类似。(其主要作用是使C++中内置的符号可以用于类上


函数名字为:关键字operator后面接需要重载的运算符符号。


函数原型:返回值类型 operator  操作符(参数列表)

注意:

1.不能通过连接其他符号来创建新的操作符:比如operator@重载操作符必须有一个类类型参数
2.用于内置类型的运算符,其含义不能改变,例如:内置的整型+,不 能改变其含义
3.作为类成员函数重载时,其形参看起来比操作数数目少1,因为成员函数的第一个参数为隐
藏的this
4.      .*    ::   sizeof   ?:       .        注意以上5个运算符不能重载。这个经常在笔试选择题中出
现。

 代码示例:
 

#include<iostream>
using std::cout;
using std::cin;
using std::endl;
// 全局的operator==
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;
};
// 这里会发现运算符重载成全局的就需要成员变量是公有的,那么问题来了,封装性如何保证?
// 这里其实可以用我们后面学习的友元解决,或者干脆重载成成员函数。
bool operator==(const Date& d1, const Date& d2)
{
	return d1._year == d2._year
		&& d1._month == d2._month
		&& d1._day == d2._day;
}
void Test()
{
	Date d1(2018, 9, 26);
	Date d2(2018, 9, 27);
	cout << (d1 == d2) << endl;
}
int main()
{
	Test();
	return 0;
}

 这里会发现运算符重载成全局的就需要成员变量是公有的,那么问题来了,封装性如何保证?
 这里其实可以用我们后面学习的友元解决,或者干脆重载成成员函数。

如这段代码:

#include<iostream>
using std::endl;
using std::cout;
using std::cin;
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;
	}
	// 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;
}

9.5.2 函数运算符重载格式

1. 赋值运算符重载格式

1.参数类型:const T&,传递引用可以提高传参效率。
2.返回值类型:T&,返回引用可以提高返回的效率,有返回值目的是为了支持连续赋值检测是否自己给自己赋值。
3.返回*this :要复合连续赋值的含义。

#include<iostream>
using std::cout;
using std::endl;
using std::cin;
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 Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date d1;
	Date d2(22, 1, 3);
	d2.Print();
	d2 = d1;
	d2.Print();
	return 0;
}

代码结果为:

 可以看出这段代码中 “=” 号的作用与我们C++中内置的“=”号是同一个道理, 但是只是增加了一个“=”号的作用,使符号能对 Date这个类 使用。

2. 赋值运算符只能重载成类的成员函数不能重载成全局函数

如下面这段错误代码:

#include<iostream>
using std::cout;
using std::endl;
using std::cin;
class Date
{
public:
	Date(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	int _year;
	int _month;
	int _day;
};
// 赋值运算符重载成全局函数,注意重载成全局函数时没有this指针了,需要给两个参数
Date& operator=(Date& left, const Date& right)
{
	if (&left != &right)
	{
		left._year = right._year;
		left._month = right._month;
		left._day = right._day;
	}
	return left;
}
// 编译失败:
// error C2801: “operator =”必须是非静态成员

错误信息:

 原因:赋值运算符如果不显式实现,编译器会生成一个默认的。此时用户再在类外自己实现
一个全局的赋值运算符重载,就和编译器在类中生成的默认赋值运算符重载冲突了,故赋值
运算符重载只能是类的成员函数。

#include<iostream>
using std::endl;
using std::cout;
using std::cin;
// 这里会发现下面的程序会崩溃掉?这里就需要我们以后讲的深拷贝去解决。
typedef int DataType;
class Stack
{
public:
	Stack(size_t capacity = 10)
	{
		_array = (DataType*)malloc(capacity * sizeof(DataType));
		if (nullptr == _array)
		{
			perror("malloc申请空间失败");
			return;
		}
		_size = 0;
		_capacity = capacity;
	}
	void Push(const DataType& data)
	{
		// CheckCapacity();
		_array[_size] = data;
		_size++;
	}
	~Stack()
	{
		if (_array)
		{
			free(_array);
			_array = nullptr;
			_capacity = 0;
			_size = 0;
		}
	}
private:
	DataType* _array;
	size_t _size;
	size_t _capacity;
};
int main()
{
	Stack s1;
	s1.Push(1);
	s1.Push(2);
	s1.Push(3);
	s1.Push(4);
	Stack s2;
	s2 = s1;
	return 0;
}

执行结果为:代码崩溃。

注意:如果类中未涉及到资源管理,赋值运算符是否实现都可以;一旦涉及到资源管理则必
须要实现。

代码解析:

 9.5.3  前置++和后置++重载

 前置++和后置++都是一元运算符,为了让前置++与后置++形成能正确重载
  C++规定:后置++重载时多增加一个int类型的参数,但调用函数时该参数不用传递,编译自动传递.

 如下面这串代码:
 

#include<iostream>
using std::cout;
using std::endl;
using std::cin;
class Date
{
public:
	Date(int year = 1900, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}


	// 前置++:返回+1之后的结果
	// 注意:this指向的对象函数结束后不会销毁,故以引用方式返回提高效率
	Date& operator++()
	{
		_day += 1;
		return *this;
	}


	// 后置++:
	// 注意:后置++是先使用后+1,因此需要返回+1之前的旧值,故需在实现时需要先将this保存一份,然后给this + 1
	// 而temp是临时对象,因此只能以值的方式返回,不能返回引用
	Date operator++(int)
	{
		Date temp(*this);
		_day += 1;
		return temp;
	}
	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << endl;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date d;
	Date d1(2022, 1, 13);
	d.Print();
	d = d1++; // d: 2022,1,13 d1:2022,1,14
	d.Print();
	d = ++d1; // d: 2022,1,15 d1:2022,1,15
	d.Print();
	return 0;
}

代码结果为:

 9.6 日期类的完全实现:

在这里我们只写函数接口,不在完全调用。

.h文件:(函数声明文件)

#pragma once

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


class Date
{
	// 友元函数 -- 这个函数内部可以使用Date对象访问私有保护成员  
	friend ostream& operator<<(ostream& out, const Date& d);
	friend istream& operator>>(istream& in, Date& d);
public:
	// 获取某年某月的天数
	// 会频繁调用,所以直接放在类里面定义作为inline
	int GetMonthDay(int year, int month)
	{
		static int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
		int day = days[month];
		if (month == 2
			&& ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
		{
			day += 1;
		}

		return day;
	}

	bool CheckDate()
	{
		if (_year >= 1
			&& _month > 0 && _month < 13
			&& _day > 0 && _day <= GetMonthDay(_year, _month))
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	// 构造会频繁调用,所以直接放在类里面定义作为inline
	Date(int year = 1, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;

		/*if (!CheckDate())
		{
			Print();
			cout << "刚构造的日期非法" << endl;
		}*/

		assert(CheckDate());
	}

	void Print() const;

	bool operator==(const Date& d) const;
	bool operator!=(const Date& d) const;
	bool operator>(const Date& d) const;
	bool operator>=(const Date& d) const;
	bool operator<(const Date& d) const;
	bool operator<=(const Date& d) const;

	Date operator+(int day) const;
	Date& operator+=(int day);

	// ++d1;
	// d1++;
	// 直接按特性重载,无法区分
	// 特殊处理,使用重载区分,后置++重载增加一个int参数跟前置构成函数重载进行区分
	Date& operator++(); // 前置
	Date operator++(int); // 后置

	// d1 - 100
	Date operator-(int day) const;
	Date& operator-=(int day);

	Date& operator--(); // 前置
	Date operator--(int); // 后置

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

	//void operator<<(ostream& out);
private:
	int _year;
	int _month;
	int _day;
};

// 流插入重载
inline ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << "年" << d._month << "月" << d._day << "日" << endl;
	return out;
}

// 流提取重载
inline istream& operator>>(istream& in, Date& d)
{
	in >> d._year >> d._month >> d._day;
	assert(d.CheckDate());

	return in;
}

 .cpp (函数定义文件)

#include "Date.h"


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

// 任何一个类,只需要写一个> == 或者 < ==重载 剩下比较运算符重载复用即可
bool Date::operator== (const Date& d) const
{
	return _year == d._year
		&& _month == d._month
		&& _day == d._day;
}

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

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

bool Date::operator>=(const Date& d) const
{
	return (*this > d) || (*this == d);
}

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

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

// d1 + 100
Date Date::operator+(int day) const
{
	//Date ret(*this);
	Date ret = *this;
	ret += day;

	return ret;
}

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


Date& Date::operator++() // 前置
{
	//*this += 1;
	//return *this;

	return *this += 1;
}

Date Date::operator++(int) // 后置
{
	Date tmp(*this);
	*this += 1;

	return tmp;
}

Date Date::operator-(int day) const
{
	Date ret = *this;
	ret -= day;
	return ret;
}

Date& Date::operator-=(int day)
{
	if (day < 0)
	{
		return *this += -day;
	}

	_day -= day;
	while (_day <= 0)
	{
		--_month;
		if (_month == 0)
		{
			--_year;
			_month = 12;
		}

		_day += GetMonthDay(_year, _month);
	}

	return *this;
}

Date& Date::operator--() // 前置
{
	return *this -= 1;
}

Date Date::operator--(int) // 后置
{
	Date tmp(*this);
	*this -= 1;
	return tmp;
}

// d1 - d2
int Date::operator-(const Date& d) const
{
	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;
}

9.7 const成员

将const修饰的“成员函数”称之为const成员函数,const修饰类成员函数,实际修饰该成员函数隐含的this指针,表明在该成员函数中不能对类的任何成员进行修改

 如:
 

#include<iostream>
using std::endl;
using std::cin;
using std::cout;
class Date
{
public:
	Date(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}
	void Print()
	{
		cout << "Print()" << endl;
		cout << "year:" << _year << endl;
		cout << "month:" << _month << endl;
		cout << "day:" << _day << endl << endl;
	}
	void Print() const 
	{
		cout << "Print()const" << endl;
		cout << "year:" << _year << endl;
		cout << "month:" << _month << endl;
		cout << "day:" << _day << endl << endl;
	}
private:
	int _year; // 年
	int _month; // 月
	int _day; // 日
};
void Test()
{
	Date d1(2022, 1, 13);
	d1.Print();
	const Date d2(2022, 1, 13);
	d2.Print();
}

注意:虽然上面调用的两个Print函数时一样,但当我们把const修饰的那个Print函数注释掉后,程序会报错,这是因为当我们用const去修饰d2的时候,表示d2的内容不能被修改,但是我们实际传参时,会传入一个this指针,其类型为:Date* const this 也就是说指针的内容是可以被修改的,这实际上就是一个权限的问题,权限扩大了。而当我们把const放在函数最后时,实际上是修改this指针类型为:const Date* const this 就权限一致了。

此外,当我们将不带const的Print函数注释掉,只保留对应的 const 成员函数时,非 const 修饰的对象就会去调用 const 成员函数,这里传参时则是权限缩小了,因此可以正常运行,也就是说,在有普通成员函数时,非 const 对象会优先调用非 const 成员函数,如果没有,再去调用 const 成员函数。

请思考下面的几个问题:

1. const对象可以调用非const成员函数吗?
2. 非const对象可以调用const成员函数吗?
3. const成员函数内可以调用其它的非const成员函数吗?
4. 非const成员函数内可以调用其它的const成员函数吗?

9.8 取地址及const取地址操作符重载

  这两个默认成员函数一般不用重新定义 ,编译器默认会生成。

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

这两个运算符一般不需要重载,使用编译器生成的默认取地址的重载即可,只有特殊情况,才需
要重载,比如想让别人获取到指定的内容!

十.再谈构造函数

 10.1 构造函数体赋值

    在创建对象时,编译器通过调用构造函数,给对象中各个成员变量一个合适的初始值。

如:

#include<iostream>
using std::endl;
using std::cin;
using std::cout;
class Date
{
public:
	Date(int year, int month, int day)
	{
		_year = year;
		_month = month;
		_day = day;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	return 0;
}

虽然上述构造函数调用之后,对象中已经有了一个初始值,但是不能将其称为对对象中成员变量的初始化,构造函数体中的语句只能将其称为赋初值,而不能称作初始化。因为初始化只能初始化一次,而构造函数体内可以多次赋值。

10.2 初始化列表

  初始化列表:以一个冒号开始,接着是一个以逗号分隔的数据成员列表,每个"成员变量"后面跟
一个放在括号中的初始值或表达式。

如:

#include<iostream>
using std::cout;
using std::endl;
using std::cin;
class Date
{
public:
	Date(int year, int month, int day)
		: _year(year)
		, _month(month)
		, _day(day)//通过初始化列表给成员函数赋值
	{}
private:
	int _year;
	int _month;
	int _day;
};

10.3 初始化列表的用途

1. 每个成员变量在初始化列表中只能出现一次(初始化只能初始化一次)
2. 类中包含以下成员,必须放在初始化列表位置进行初始化:

(1)引用成员变量
(2)const成员变量
  (3)   自定义类型成员(且该类没有默认构造函数时)

如:

#include<iostream>
using std::endl;
using std::cout;
using std::cin;
class A
{
public:
	A(int a)
		:_a(a)
	{}
private:
	int _a;
};
class B
{
public:
	B(int a, int ref)
		:_aobj(a)
		, _ref(ref)
		, _n(10)
	{}
private:
	A _aobj; // 没有默认构造函数
	int& _ref; // 引用
	const int _n; // const
};

3. 尽量使用初始化列表初始化,因为不管你是否使用初始化列表,对于自定义类型成员变量,一定会先使用初始化列表初始化。

比如说这段代码:

#include<iostream>
using std::cout;
using std::endl;
using std::cin;
class Time
{
public:
	Time(int hour = 0)
		:_hour(hour)
	{
		cout << "Time()" << endl;
	}
private:
	int _hour;
};
class Date
{
public:
	Date(int day)
	{
	}
private:
	int _day;
	Time _t;
};
int main()
{
	Date d(1);
}

输出结果为:

 可见自定义类型调用了构造函数,同时用了初始化列表初始化。

4. 成员变量在类中声明次序就是其在初始化列表中的初始化顺序,与其在初始化列表中的先后次序无关。

下面这段代码的结果为多少呢?

A. 输出1 1
B.程序崩溃
C.编译不通过
D.输出1 随机值

#include<iostream>
using std::endl;
using std::cout;
using std::cin;
class A
{
public:
	A(int a)
		:_a1(a)
		, _a2(_a1)
	{}
	void Print() 
	{
		cout << _a1 << " " << _a2 << endl;
	}
private:
	int _a2;
	int _a1;
};
int main() 
{
	A aa(1);
	aa.Print();
}

答案为:D

10.3 explicit关键字

  构造函数不仅可以构造与初始化对象,对于单个参数或者除第一个参数无默认值其余均有默认值
的构造函数,还具有类型转换的作用。

  如果不想让其拥有隐式转换的作用,可以用explicit关键字。

如下面两段代码进行对比:

没有explicit关键字的代码:

#include<iostream>
using std::endl;
using std::cout;
using std::cin;
class Date {
public:
	// 此时支持隐式类型转换
	Date(int year)
		:_year(year)
	{
		cout << "Date(int year)" << endl;
	}
   // 此时支持隐式类型转换
	Date(int year, int month)
	: _year(year)
	, _month(month)
	{}


	Date(const Date& a)
	: _year(a._year)
	{
		cout << "Date(const Date& a)" << endl;
	}

private:
	int _year;
	int _month;
};

int main()
 {
   Date a1=2023;

	//下面语句发生了隐式类型转换:通过创建一个临时变量将1900转换成Date类类型对象,再用临时的对象拷贝创建a2对象

	Date a2 = 1900;

	//下面语句如果不加const,则无法通过编译,而加了const则编译通过,
	
	//正是因为隐式类型转换产生了临时对象,而临时对象具有常属性,
	
	//因此需要用const修饰才能进行引用,这也侧面说明了隐式类型转换的过程中产生了临时变量
	const Date& ref = 2023;

	Date a3(1900, 2023);

	Date a4 = { 2022, 2023 };//同样发生了隐式类型转换 
	return 0;
}

通过编译。

有explicit关键字的代码:

#include<iostream>
using std::endl;
using std::cout;
using std::cin;
class Date {
public:
	//不支持隐式类型转换
	 explicit Date(int year)
		:_year(year)
	{
		cout << "Date(int year)" << endl;
	}

	//不支持隐式类型转换
    explicit Date(int year, int month)
	: _year(year)
	, _month(month)
	{}



private:
	int _year;
	int _month;
};

int main() 
{

	//此时两个隐式类型转换都不通过

	Date a2 = 1;

	Date a4 = { 1, 2 };

	return 0;
}

这段代码将通不过编译:

十一. static成员 

11.1 概念

声明为static的类成员称为类的静态成员,用static修饰的成员变量,称之为静态成员变量;用static修饰的成员函数,称之为静态成员函数。静态成员变量一定要在类外进行初始化.

面试题:实现一个类,计算程序中创建出了多少个类对象。

#include<iostream>
using std::cout;
using std::endl;
using std::cin;
class A
{
	public:
		A() {
			++_scount; 
		}
		A(const A& t) 
		{
			++_scount; 
		}
		~A() 
		{ 
			--_scount;
		}
		static int GetACount()
		{
			return _scount;
		}
private:
	static int _scount;
};
int A::_scount = 0;
void TestA()
{
	cout << A::GetACount() << endl;
	A a1, a2;
	A a3(a1);
	cout << A::GetACount() << endl;
}
int main()
{
	TestA();
	return 0;
}

答案为:

 11.2 特性

1. 静态成员为所有类对象所共享,不属于某个具体的对象,存放在静态区。
2. 静态成员变量必须在类外定义,定义时不添加static关键字,类中只是声明。
3. 类静态成员即可用 类名::静态成员 或者 对象.静态成员 来访问。
4. 静态成员函数没有隐藏的this指针,不能访问任何非静态成员。
5. 静态成员也是类的成员,受public、protected、private 访问限定符的限制。

【问题】
1. 静态成员函数可以调用非静态成员函数吗?  不可以
2. 非静态成员函数可以调用类的静态成员函数吗?  可以

十二.友元

12.1 友元的概念

  友元提供了一种突破封装的方式,有时提供了便利。但是友元会增加耦合度,破坏了封装,所以友元不宜多用。

友元分为:友元函数和友元类

12.2 友元函数

  问题:

现在尝试去重载operator<<,然后发现没办法将operator<<重载成成员函数。因为cout的输出流对象和隐含的this指针在抢占第一个参数的位置。this指针默认是第一个参数也就是左操作数了。

但是实际使用中cout需要是第一个形参对象,才能正常使用。所以要将operator<<重载成全局函数。但又会导致类外没办法访问成员,此时就需要友元来解决。operator>>同理。

 我们来看一个不用友元函数解决"<<"流插入运算符的代码:
 

#include<iostream>
using namespace std;
class Date
{
public:
	Date(int year, int month, int day)
		: _year(year)
		, _month(month)
		, _day(day)
	{}
	// d1 << cout; -> d1.operator<<(&d1, cout); 不符合常规调用
// 因为成员函数第一个参数一定是隐藏的this,所以d1必须放在<<的左侧
	ostream& operator<<(ostream& _cout)//ostream是cout的函数类型
	{
		_cout << _year << "-" << _month << "-" << _day << endl;
		return _cout;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date d1(2023, 4, 16);
	d1 << cout;//但我们此时就会变成这样
	return 0;
}

 输出结果:

   但这个格式显示不符合 cout的语法,那么我们就可以用友元函数解决"<<"流插入运算符:

友元函数可以直接访问类的私有成员,它是定义在类外部的普通函数,不属于任何类,但需要在类的内部声明,声明时需要加friend关键字

 如以下代码:

#include<iostream>
using std::cout;
using std::cin;
using std::endl;
using  namespace std;

class Date
{
	friend ostream& operator<<(ostream& _cout, const Date& d);
	friend istream& operator>>(istream& _cin, Date& d);
public:
	Date(int year = 1900, int month = 1, int day = 1)
		: _year(year)
		, _month(month)
		, _day(day)
	{}
private:
	int _year;
	int _month;
	int _day;
};
ostream& operator<<(ostream& _cout, const Date& d)
{
	_cout << d._year << "-" << d._month << "-" << d._day;
	return _cout;
}
istream& operator>>(istream& _cin, Date& d)
{
	_cin >> d._year;
	_cin >> d._month;
	_cin >> d._day;
	return _cin;
}
int main()
{
	Date d;
	cout << d << endl;
	return 0;
}

代码结果为:

 说明:

友元函数可访问类的私有和保护成员,但不是类的成员函数。
友元函数不能用const修饰。
友元函数可以在类定义的任何地方声明,不受类访问限定符限制。
一个函数可以是多个类的友元函数。
友元函数的调用与普通函数的调用原理相同。

大家可以自己尝试以下cin函数。

12.3 友元类

 友元类的特性:


1.友元类的所有成员函数都可以是另一个类的友元函数,都可以访问另一个类中的非公有成员。
2.友元关系是单向的,不具有交换性。
比如上述Time类和Date类,在Time类中声明Date类为其友元类,那么可以在Date类中直接
访问Time类的私有成员变量,但想在Time类中访问Date类中私有的成员变量则不行。
3.友元关系不能传递
4.如果C是B的友元, B是A的友元,则不能说明C时A的友元。
5.(友元关系不能继承,在继承位置再给大家详细介绍。)

如:

#include<iostream>
using namespace std;
class Time
{
	friend class Date; // 声明日期类为时间类的友元类,则在日期类中就可以直接访问Time类中的私有成员变量
public:
	Time(int hour = 0, int minute = 0, int second = 0)
		: _hour(hour)
		, _minute(minute)
		, _second(second)
	{}
private:
	int _hour;
	int _minute;
	int _second;
};
class Date
{
public:
	Date(int year = 1900, int month = 1, int day = 1)
		: _year(year)
		, _month(month)
		, _day(day)
	{}
	void SetTimeOfDate(int hour, int minute, int second)
	{
		// 直接访问时间类私有的成员变量
		_t._hour = hour;
		_t._minute = minute;
		_t._second = second;
	}
	void Print()
	{
		cout << _year << "-" << _month << "-" << _day << "-" << _t._hour << "-" << _t._minute << "-" << _t._second << endl;//这也是直接调用
	}
private:
	int _year;
	int _month;
	int _day;
	Time _t;
};
int main()
{
	Date d1;
	d1.Print();//打印结果
	return 0;
}

代码结果为:

十三  内部类

概念:如果一个类定义在另一个类的内部,这个内部类就叫做内部类。内部类是一个独立的类,它不属于外部类,更不能通过外部类的对象去访问内部类的成员。外部类对内部类没有任何优越的访问权限。
注意:内部类就是外部类的友元类,参见友元类的定义,内部类可以通过外部类的对象参数来访问外部类中的所有成员。但是外部类不是内部类的友元(其实和友元差不多,都具有单向性)。

特性:
1. 内部类可以定义在外部类的public、protected、private都是可以的。
2. 注意内部类可以直接访问外部类中的static成员,不需要外部类的对象/类名。
3. sizeof(外部类)=外部类,和内部类没有任何关系

如:

#include<iostream>
using namespace std;
class A
{
private:
	static int k;
	int h;
public:
	class B // B天生就是A的友元
	{
	public:
		void foo(const A& a)
		{
			cout << k << endl;//OK
			cout << a.h << endl;//OK B类可以调用A类的任何成员
		}
	};
};
int A::k = 1;
int main()
{
	A::B b;//注意这里有个类域的限制
	b.foo(A());
	return 0;
}

十四 匿名对象

  直接上代码:

#include<iostream>
using namespace std;
class A
{
public:
	A(int a = 0)
		:_a(a)
	{
		cout << "A(int a)" << endl;
	}
	~A()
	{
		cout << "~A()" << endl;
	}
private:
	int _a;
};
class Solution {
public:
	int Sum_Solution(int n) {
		//...
		return n;
	}
};
int main()
{
	A aa1;
	// 不能这么定义对象,因为编译器无法识别下面是一个函数声明,还是对象定义
	//A aa1();
	// 但是我们可以这么定义匿名对象,匿名对象的特点不用取名字,
	// 但是他的生命周期只有这一行,我们可以看到下一行他就会自动调用析构函数
	A();
	A aa2(2);
	// 匿名对象在这样场景下就很好用,当然还有一些其他使用场景,这个我们以后遇到了再说
	Solution().Sum_Solution(10);
	return 0;
}

十五  拷贝对象时的一些编译器优化

   在传参和传返回值的过程中,一般编译器会做一些优化,减少对象的拷贝,这个在一些场景下还
是非常有用的。

如:

#include<iostream>
using namespace std;
class A
{
public:
	A(int a = 0)
		:_a(a)
	{
		cout << "A(int a)" << endl;
	}
	A(const A& aa)
		:_a(aa._a)
	{
		cout << "A(const A& aa)" << endl;
	}
	A& operator=(const A& aa)
	{
		cout << "A& operator=(const A& aa)" << endl;
		if (this != &aa)
		{
			_a = aa._a;
		}
		return *this;
	}
	~A()
	{
		cout << "~A()" << endl;
	}
private:
	int _a;
};
void f1(A aa)
{}
A f2()
{
	A aa;
	return aa;
}
int main()
{
	// 传值传参
	A aa1;
	f1(aa1);
	cout << endl;
	// 传值返回
	f2();
	cout << endl;
	// 隐式类型,连续构造+拷贝构造->优化为直接构造
	f1(1);
	// 一个表达式中,连续构造+拷贝构造->优化为一个构造
	f1(A(2));
	cout << endl;
	// 一个表达式中,连续拷贝构造+拷贝构造->优化一个拷贝构造
	A aa2 = f2();
	cout << endl;
	// 一个表达式中,连续拷贝构造+赋值重载->无法优化
	aa1 = f2();
	cout << endl;
	return 0;
}

大家猜以下结果是多少呢?

 说明: 不同的编译器的优化程度不同,从上述结果也可以看出,本文中所使用的VS2022的编译器的优化程度比较大。这种优化一般是针对连续的调用函数,以后再做过多讲解。

 十六  再次理解类和对象

现实生活中的实体计算机并不认识,计算机只认识二进制格式的数据。如果想要让计算机认识现
实生活中的实体,用户必须通过某种面向对象的语言,对实体进行描述,然后通过编写程序,创
建对象后计算机才可以认识。比如想要让计算机送外卖,就需要:


1. 用户先要对现实中三个人物模型进行抽象---即在人为思想层面对卖家,买家,骑手进行认识,骑手要做什么,卖家要做什么,买家要做什么?
2. 经过1之后,在人的头脑中已经对送外卖有了一个清醒的认识,只不过此时计算机还不清
楚,想要让计算机去送外卖,就需要人通过某种面相对象的语言(比如:C++、Java、Python等)将三个对象模型用类来进行描述,并输入到计算机中。
3. 经过2之后,在计算机中就有了三类,但是类只是站在计算机的角度对对象进行描述的,通过三个类,可以实例化出一个个对象,此时计算机才能进行送外卖这一进程。
4. 用户就可以借助计算机中骑手,买家,卖家这三个类,来模拟现实中的送外卖实体了。在类和对象阶段,大家一定要体会到,类是对某一类实体(对象)来进行描述的,描述该对象具有那
些属性,那些方法,描述完成后就形成了一种新的自定义类型,才用该自定义类型就可以实例化
具体的对象。

 建议:去送外卖呢亲。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值