四、类和对象——4.2对象特性、对象的初始化和清理


前言

本章内容主要是关于对象的初始化和清理

  • 生活中我们买的电子产品都基本会有出厂设置,在某一天我们不用时也会删除一些自己的信息数据以保证安全。
  • C++中的面向对象来源于生活,每个对象也会有初始设置以及 对象销毁前的清理数据的设置。

提示:以下是本篇文章正文内容,下面案例可供参考

一、构造函数和析构函数

简要介绍

对象的初始化和清理也是两个非常重要的安全问题

  • 一个对象或者变量没有初始状态,对其使用后果是未知。

  • 同样的使用完一个对象和变量,没有及时清理,也会造成一定的安全问题。

  • C++利用了构造函数析构函数来解决上述问题,这两个函数会被编译器自动调用,完成对象的初始化和清理工作。

  • 对象的初始化和清理工作是编译器强制要我们做的事情,因此如果我们不提供构造和析构,编译器会提供。

  • 编译器提供的构造函数和析构函数是空实现

  • 构造函数: 主要作用于创建对象时为对象的成员属性赋值,构造函数由编译器自动调用,无需手动调用。

  • 析构函数: 主要作用于对象销毁前系统自动调用,执行一些清理工作。

语法

  • 构造函数语法: 类名(){}

    1.构造函数,没有返回值,也不写void。
    2.函数名称与类名相同。
    3.构造函数可以有参数,因此可以发生重载。
    4.程序在调用对象时会自动调用构造,无需手动调用,而且只调用一次。

  • 析构函数语法: ~类名(){}

    1.析构函数,没有返回值,也不写void。
    2.函数名称与类名相同,在名称前加上符号~。
    3.析构函数不可以有参数,因此不能发生重载。
    4.程序在对象销毁前会自动调用析构,无需手动调用,而且只调用一次。

视频链接: 点击此处即可 里面有更详细的讲解

二、构造函数的分类及调用

分类方式

  • 按参数分类:有参构造和无参构造
  • 按类型分类:普通构造和拷贝构造

调用方式

  1. 括号法
  2. 显示法
  3. 隐式转换法

代码示例

代码如下(示例):

#include <iostream>
using namespace std;

class Person
{
public:
	//分类
	Person()                      //无参构造
	{
		cout << "Person 构造函数的调用!" << endl;
	}
	Person(int age)                 //有参构造
	{
		m_age = age;
		cout << "Person 构造函数的调用!" << endl;
	}
	Person(const Person &p)       //拷贝构造,加const防止数据被修改
	{
		//将传入的人的所有属性拷贝到我身上
		m_age = p.m_age; 
		cout << "Person 拷贝构造函数的调用!" << endl;
	}

	~Person()
	{
		cout << "Person 析构函数的调用" << endl;
	}

	int m_age;

};


//调用
void test01()
{
	//1、括号法!
	cout << "1、括号法" << endl;
	Person p1;           //默认无参构造函数的调用
	Person p2(10);         //有参函数的调用
	Person p3(p2);         //拷贝函数的调用
	cout << "p2的年龄为:" << p2.m_age << endl;
	cout << "p3的年龄为:" << p3.m_age << endl;
	cout << "---------------------" << endl;
	//注意事项1
	//调用默认构造函数时,不要加()。

	//2、显示法!
	cout << "2、显示法" << endl;
	Person p11;                    //默认无参构造函数的调用
	Person p22 = Person(10);       //有参函数的调用(p2是Person(10)的名字)
	Person p33 = Person(p22);       //拷贝函数的调用
	cout << endl;

	Person(10);              //匿名对象(没有名字),特点:当前行执行结束后,系统会立即收回匿名对象
	cout << "aaaaaaaaaa" << endl;
	cout << "---------------------" << endl;
	//注意事项2
	//不要利用拷贝构造函数 初始化匿名对象 因为在编译器中Person(p3) == Person p3;编译器会以为这是在声明对象
	//Person(p3);

	//3、隐式转换法
	cout << "3、隐式转换法" << endl;
	Person p4 = 10;               //有参构造的调用
	Person p5 = p4;               //拷贝构造的调用
	cout << endl;
}

int main()
{
	test01();

	system("pause");
	return 0;
}

运行结果如下:

在这里插入图片描述

三、拷贝构造函数调用时机

C++中拷贝构造函数调用时机通常有三种:

1. 使用一个已经创建完毕的对象来初始化一个新对象

代码示例

代码如下(示例):

#include <iostream>
using namespace std;

class Person
{
public:
	Person()                     
	{
		cout << "Person 无参构造函数的调用!" << endl;
	}
	Person(int age)
	{
		m_Age = age;
		cout << "Person 有参构造函数的调用!" << endl;
	}
	Person(const Person &p)
	{
		m_Age = p.m_Age;
		cout << "Person 拷贝构造函数的调用!" << endl;
	}

	~Person()
	{
		cout << "Person 析构函数的调用" << endl;
	}

	int m_Age;

};

void test01()
{
	Person p1(20);
	Person p2(p1);

	cout << "p2的年龄为: " << p2.m_Age << endl;
}

int main()
{
	test01();

	system("pause");
	return 0;
}

运行结果如下:
在这里插入图片描述

2. 值传递的方式给函数参数传值

代码如下(示例):

#include <iostream>
using namespace std;

class Person
{
public:
	Person()                     
	{
		cout << "Person 无参构造函数的调用!" << endl;
	}
	Person(int age)
	{
		m_Age = age;
		cout << "Person 有参构造函数的调用!" << endl;
	}
	Person(const Person &p)
	{
		m_Age = p.m_Age;
		cout << "Person 拷贝构造函数的调用!" << endl;
	}

	~Person()
	{
		cout << "Person 析构函数的调用" << endl;
	}

	int m_Age;

};

void doWork(Person p)    //相当于Person p = p;  隐式转换法 拷贝构造函数的调用
{                        //形参p和实参p并不一样哦
	
}                        //函数执行完后,析构函数的调用

void test02()
{
	Person p;     //无参构造函数的调用
	doWork(p);    
}                 //函数执行完后,析构函数的调用

int main()
{
	test02();

	system("pause");
	return 0;
}

运行结果如下:

在这里插入图片描述

3. 以值方式返回局部对象

代码如下(示例):

#include <iostream>
using namespace std;

class Person
{
public:
	Person()                     
	{
		cout << "Person 无参构造函数的调用!" << endl;
	}
	Person(int age)
	{
		m_Age = age;
		cout << "Person 有参构造函数的调用!" << endl;
	}
	Person(const Person &p)
	{
		m_Age = p.m_Age;
		cout << "Person 拷贝构造函数的调用!" << endl;
	}

	~Person()
	{
		cout << "Person 析构函数的调用" << endl;
	}

	int m_Age;

};

Person doWork()    
{                       
	Person p1;
	cout << "p1的地址:" << (int*)&p1 << endl;   //打印出p1的地址
	return p1;              //在内存分区那我们知道,不能返回局部变量的地址
}                        //但doWork()函数实际上返回的并不是p1,而是拷贝了一份和p1一样的对象(p')
                          //实际上返回的是p'
void test03()
{
	Person p = doWork();           //接受p',打印出p'的地址,发现与p1的地址不一样,这就说明
	cout << "p的地址:" << (int*)&p << endl;
}                             //返回的不是p1,而是又拷贝了一份和p1一样的对象p',然后返回的实际上是P'
                                 
int main()
{
	test03();

	system("pause");
	return 0;
}

运行结果如下:
在这里插入图片描述

四、构造函数调用规则

简要介绍

默认情况下,C++编译器至少给一个类添加3个函数

  1. 默认构造函数(无参,函数体为空)即空实现
  2. 默认析构函数(无参,函数体为空)即空实现
  3. 默认拷贝构造函数,对属性进行值拷贝

构造函数调用规则

  • 如果用户调用有参构造函数,C++不再提供默认无参构造,但是会提供默认拷贝构造。
  • 如果用户提供默认拷贝构造函数,C++不会再提供其他构造函数。

代码示例

代码如下(示例1):

用户提供默认拷贝构造函数

#include <iostream>
using namespace std;

class Person
{
public:
	Person()
	{
		cout << "Person 无参构造函数的调用!" << endl;
	}
	Person(int age)
	{
		m_Age = age;
		cout << "Person 有参构造函数的调用!" << endl;
	}
	Person(const Person &p)
	{
		m_Age = p.m_Age;
		cout << "Person 拷贝构造函数的调用!" << endl;
	}

	~Person()
	{
		cout << "Person 析构函数的调用" << endl;
	}

	int m_Age;

};

void test01()
{
	Person p1;
	p1.m_Age = 20;
	Person p2(p1);
	cout << "p2的年龄为:" << p2.m_Age << endl;
}

int main()
{
	test01();

	system("pause");
	return 0;
}

运行结果如下:
在这里插入图片描述

代码如下(示例2):

用户不提供默认拷贝构造函数

#include <iostream>
using namespace std;

class Person
{
public:
	Person()
	{
		cout << "Person 无参构造函数的调用!" << endl;
	}
	Person(int age)
	{
		m_Age = age;
		cout << "Person 有参构造函数的调用!" << endl;
	}
	/*Person(const Person &p)
	{
		m_Age = p.m_Age;
		cout << "Person 拷贝构造函数的调用!" << endl;
	}*/

	~Person()
	{
		cout << "Person 析构函数的调用" << endl;
	}

	int m_Age;

};

void test01()
{
	Person p1;
	p1.m_Age = 20;
	Person p2(p1);
	cout << "p2的年龄为:" << p2.m_Age << endl;
}

int main()
{
	test01();

	system("pause");
	return 0;
}

运行结果如下:
在这里插入图片描述
可以看出p2的年龄还是20,这说明,系统会自动提供拷贝构造函数。

代码如下(示例3):

#include <iostream>
using namespace std;

class Person
{
public:
	/*Person()
	{
		cout << "Person 无参构造函数的调用!" << endl;
	}*/
	Person(int age)
	{
		m_Age = age;
		cout << "Person 有参构造函数的调用!" << endl;
	}
	/*Person(const Person &p)
	{
		m_Age = p.m_Age;
		cout << "Person 拷贝构造函数的调用!" << endl;
	}*/

	~Person()
	{
		cout << "Person 析构函数的调用" << endl;
	}

	int m_Age;

};

void test02()
{
	Person p(18);
	Person p3(p);
	cout << "p3的年龄为:" << p3.m_Age << endl;

}

int main()
{
	test02();

	system("pause");
	return 0;
}

运行结果如下:
在这里插入图片描述

如图,调用有参构造函数,C++不再提供默认无参构造,但是会提供默认拷贝构造。

五、深拷贝与浅拷贝

简要介绍

深浅拷贝是面试经典问题,也是常见的一个坑。

  • 浅拷贝:简单的复制拷贝操作
  • 深拷贝:在堆区重新申请空间,进行拷贝工作

注意:

代码示例

错误代码如下(示例):

#include <iostream>
using namespace std;

class Person
{
public:
	Person()
	{
		cout << "Person 无参构造函数的调用!" << endl;
	}
	Person(int age,int height)
	{
		m_Age = age;
		m_Height = new int(height);
		cout << "Person 有参构造函数的调用!" << endl;
	}
	//Person(const Person &p)
	//{
	//	m_Age = p.m_Age;
	//	m_Height = new int(*p.m_Height);          //深拷贝
	//	cout << "Person 无参构造函数的调用!" << endl;
	//}

	~Person()
	{
		if (m_Height != NULL)
		{
			delete m_Height;
			m_Height = NULL;
		}
		cout << "Person 析构函数的调用" << endl;
	}

	int m_Age;
	int* m_Height;
};

void test01()
{
	Person p1(18,160);
	cout << "p1的年龄为:" << p1.m_Age << endl;
	cout << "p1的身高为:" << *p1.m_Height << endl;
	Person p2(p1);
	cout << "p2的年龄为:" << p2.m_Age << endl;
	cout << "p2的身高为:" << *p2.m_Height << endl;
}

int main()
{
	test01();

	system("pause");
	return 0;
}

注意: 上述代码运行系统会崩溃。如果利用编译器提供的拷贝构造函数,会做浅拷贝工作。浅拷贝带来的问题是:堆区的内存重复释放。这种问题要利用深拷贝解决
小结: 如果属性有在堆区开辟的,一定要自己提供拷贝构造函数,防止浅拷贝带来的问题。

视频链接: 点击此处详细解释在此,不看后悔系列!!!!!

正确代码如下(示例):

#include <iostream>
using namespace std;

class Person
{
public:
	Person()
	{
		cout << "Person 无参构造函数的调用!" << endl;
	}
	Person(int age,int height)
	{
		m_Age = age;
		m_Height = new int(height);
		cout << "Person 有参构造函数的调用!" << endl;
	}
	Person(const Person &p)
	{
		m_Age = p.m_Age;
		m_Height = new int(*p.m_Height);          //深拷贝
		cout << "Person 无参构造函数的调用!" << endl;
	}

	~Person()
	{
		if (m_Height != NULL)
		{
			delete m_Height;
			m_Height = NULL;
		}
		cout << "Person 析构函数的调用" << endl;
	}

	int m_Age;
	int* m_Height;
};

void test01()
{
	Person p1(18,160);
	cout << "p1的年龄为:" << p1.m_Age << endl;
	cout << "p1的身高为:" << *p1.m_Height << endl;
	Person p2(p1);
	cout << "p2的年龄为:" << p2.m_Age << endl;
	cout << "p2的身高为:" << *p2.m_Height << endl;
}

int main()
{
	test01();

	system("pause");
	return 0;
}

运行结果如下:
在这里插入图片描述

六、初始化列表

简要介绍

  • 作用: C++提供了初始化列表语法,用来初始化属性
  • 语法: 构造函数():属性1(值1),属性2(值2),…{}

代码示例

  • 传统意义上的初始化
#include <iostream>
using namespace std;

class Person
{
public:
	//传统意义上的初始化
	Person(int a, int b, int c)
	{
		m_A = a;
		m_B = b;
		m_C = c;
	}

	int m_A;
	int m_B;
	int m_C;

};

void test01()
{
	Person p(10, 20, 30);
	cout << "m_A = " << p.m_A << endl;
	cout << "m_B = " << p.m_B << endl;
	cout << "m_C = " << p.m_C << endl;
}

int main()
{
	test01();

	system("pause");
	return 0;
}

运行结果如下:
在这里插入图片描述

  • 初始化列表初始化属性
#include <iostream>
using namespace std;

class Person
{
public:
	//初始化列表初始化属性
	Person(int a, int b, int c) : m_A(a), m_B(b), m_C(c)
	{

	}


	int m_A;
	int m_B;
	int m_C;

};

void test01()
{
	Person p(30, 20, 10);
	cout << "m_A = " << p.m_A << endl;
	cout << "m_B = " << p.m_B << endl;
	cout << "m_C = " << p.m_C << endl;
}

int main()
{
	test01();

	system("pause");
	return 0;
}

运行结果如下:

在这里插入图片描述

七、类对象作为类成员

  • C++类中的成员可以是另一个类的对象,我们称该成员为对象成员
class A{};
class b
{
	A a;    //a为对象成员
};

那么当创建B对象时,A与B的构造和析构的顺序时谁先谁后?

#include <iostream>
#include <string>
using namespace std;

class Phone
{
public:
	Phone(string brand)
	{
		m_pBrand = brand;
		cout << "Phone 构造函数的调用" << endl;
	}
	~Phone()
	{
		cout << "Phone 析构函数的调用" << endl;
	}

	string m_pBrand;      
};

class Person
{
public:
	//Phone m_Phone = Brand;  隐式转换法
	Person(string Name, string Brand) : m_Name(Name), m_Phone(Brand)
	{
		cout << "Person 构造函数的调用" << endl;
	}
	~Person()
	{
		cout << "Person 析构函数的调用" << endl;
	}

	string m_Name;
	Phone m_Phone;
};

void test01()
{
	Person p("张三", "苹果MAX");
	cout << p.m_Name << "拿着" << p.m_Phone.m_pBrand << endl;
}

int main()
{
	test01();

	system("pause");
	return 0;
}

运行结果如下:

在这里插入图片描述
由上图可见,当其他类对象作为本类成员时,先构造类对象,再构造自身,析构顺序相反。

八、静态成员

静态成员就是在成员变量和成员函数前加上关键字static,称为静态成员。

静态成员分为:

静态成员变量

  1. 所有对象共享同一份数据
  2. 在编译阶段分配内存
  3. 类内声明,类外初始化
    ps: 静态成员变量也是有访问权限的

代码示例

#include <iostream>

using namespace std;

class Person
{
public:
	static int m_A;   //类内声明
};

int Person::m_A = 100;   //类外初始化

void test01()
{
	Person p1;
	cout << "p1.m_A = " << p1.m_A << endl;

	Person p2;
	p2.m_A = 200;

	cout << "p1.m_A = " << p2.m_A << endl;  
	//静态成员变量,不属于某个对象上,所有对象共享同一份数据
	//因此静态成员变量有两种访问方式
	
	//1、通过对象进行访问
	/*Person p;
	cout << p.m_A << endl;*/
	//2、通过类名进行访问
	cout << "通过类名进行访问" << endl;
	cout << Person::m_A << endl;

}

int main()
{
	test01();

	return 0;
}

运行结果如下:
在这里插入图片描述

静态成员函数

  1. 所有对象共享同一个函数
  2. 静态成员函数只能访问静态成员
    ps: 静态成员函数也是有访问权限的

代码示例

#include <iostream>

using namespace std;

class Person
{
public:
	static void func()
	{
		m_A = 100;     //静态成员函数可以访问 静态成员变量
		cout << "static func()的调用" << endl;
		cout << "m_A = " << m_A << endl;
	}

	static int m_A;    //静态成员变量

};

int Person::m_A = 0;

void test01()
{

	//1、通过对象进行访问
	cout << "第一种访问:" << endl;
	Person p;
	p.func();

	//2、通过类名进行访问
	cout << "第二种访问:" << endl;
	Person::func();
}

int main()
{
	test01();

	return 0;
}

运行结果如下:

在这里插入图片描述


总结

提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值