文章内容转载自黑马程序员C++核心编程讲义,如有侵权,请联系作者删除
4.2 对象的初始化和清理
- 生活中我们买的电子产品都基本会有出厂设置,在某一天我们不用时候也会删除一些自己信息数据保证安全
 
- C++中的面向对象来源于生活,每个对象也都会有初始设置以及对象销毁前的清理数据的设置。
 
4.2.1 构造函数和析构函数
对象的初始化和清理也是两个非常重要的安全问题
一个对象或者变量没有初始状态,对其使用后果是未知
同样的使用完一个对象或变量,没有及时清理,也会造成一定的安全问题
c++利用了构造函数和析构函数解决上述问题,这两个函数将会被编译器自动调用,完成对象初始化和清理工作。
对象的初始化和清理工作是编译器强制要我们做的事情,因此如果我们不提供构造和析构,编译器会提供
编译器提供的构造函数和析构函数是空实现(函数体内一行代码都没有)。
- 构造函数:主要作用在于创建对象时为对象的成员属性赋值,构造函数由编译器自动调用,无须手动调用。
 
- 析构函数:主要作用在于对象销毁前系统自动调用,执行一些清理工作。
 
构造函数语法:类名(){}
- 构造函数,没有返回,值也不写void
 
- 函数名称与类名相同
 
- 构造函数可以有参数,因此可以发生重载
 
- 程序在调用对象时候会自动调用构造,无须手动调用,而且只会调用一次
 
析构函数语法: ~类名(){}
- 析构函数,没有返回值也不写void
 
- 函数名称与类名相同,在名称前加上符号 ~
 
- 析构函数不可以有参数,因此不可以发生重载
 
- 程序在对象销毁前会自动调用析构,无须手动调用,而且只会调用一次
 
class Person {
  public: 	 	Person() 	{ 		cout << "Person的构造函数调用" << endl; 	} 	 	~Person() 	{ 		cout << "Person的析构函数调用" << endl; 	}
  };
 
 
 
 
 
  void test01() { 	Person p; }
  int main() { 	 	test01();          Person p;      	system("pause");
  	return 0; }
   | 
 
4.2.2 构造函数的分类及调用
两种分类方式:
 按参数分为: 有参构造和无参构造
 按类型分为: 普通构造和拷贝构造
三种调用方式:
 括号法
 显示法
 隐式转换法
示例:
 
 
  class Person { public: 	 	Person() { 		cout << "无参构造函数!" << endl; 	} 	 	Person(int a) { 		age = a; 		cout << "有参构造函数!" << endl; 	} 	 	Person(const Person& p) {          		age = p.age; 		cout << "拷贝构造函数!" << endl; 	} 	 	~Person() { 		cout << "析构函数!" << endl; 	} public: 	int age; };
 
 
  void test01() { 	Person p;  }
 
  void test02() {
  	     Person p; 	Person p1(10);     Person p2(p1) 	 	
  	 	Person p3 = Person(10);  	Person p2 = Person(p1);  	                                   
  	 	Person p4 = 10;  	Person p5 = p4; 
  	      	 }
  int main() {
  	test01(); 	
  	system("pause");
  	return 0; }
 
  | 
 
4.2.3 拷贝构造函数调用时机
C++中拷贝构造函数调用时机通常有三种情况
- 使用一个已经创建完毕的对象来初始化一个新对象
 
- 值传递的方式给函数参数传值
 
- 以值方式返回局部对象
 
示例:
class Person { public: 	Person() { 		cout << "无参构造函数!" << endl; 		mAge = 0; 	} 	Person(int age) { 		cout << "有参构造函数!" << endl; 		mAge = age; 	} 	Person(const Person& p) { 		cout << "拷贝构造函数!" << endl; 		mAge = p.mAge; 	} 	 	~Person() { 		cout << "析构函数!" << endl; 	} public: 	int mAge; };
 
  void test01() {
  	Person man(100);  	Person newman(man);      cout << newman.age << endl;                                    	Person newman2 = man; 
  	 	 }
 
 
  void doWork(Person p1) {} void test02() { 	Person p;  	doWork(p); }
 
  Person doWork2() { 	Person p1; 	cout << (int *)&p1 << endl; 	return p1; }
  void test03() { 	Person p = doWork2(); 	cout << (int *)&p << endl; }
 
  int main() {
  	 	 	     test03();                                   
       	system("pause");
  	return 0; }
  | 
 
4.2.4 构造函数调用规则
默认情况下,c++编译器至少给一个类添加3个函数
1.默认构造函数(无参,函数体为空)
2.默认析构函数(无参,函数体为空)
3.默认拷贝构造函数,对属性进行值拷贝
构造函数调用规则如下:
示例:
class Person { public: 	 	Person() { 		cout << "无参构造函数!" << endl; 	} 	 	Person(int a) { 		age = a; 		cout << "有参构造函数!" << endl; 	} 	 	Person(const Person& p) { 		age = p.age; 		cout << "拷贝构造函数!" << endl; 	} 	 	~Person() { 		cout << "析构函数!" << endl; 	} public: 	int age; };
  void test01() { 	Person p1(18); 	 	Person p2(p1);
  	cout << "p2的年龄为: " << p2.age << endl;                                              }
  void test02() { 	 	Person p1;       	     Person p2(10);  	Person p3(p2);      cout<<p3.age<<endl;                         
  	 	Person p4;  	Person p5(10);  	Person p6(p5);  }
  int main() {
  	     test02()
  	system("pause");
  	return 0; }
  | 
 
4.2.5 深拷贝与浅拷贝
深浅拷贝是面试经典问题,也是常见的一个坑
浅拷贝:简单的赋值拷贝操作,编译器提供的简单赋值操作
深拷贝:在堆区重新申请空间,进行拷贝操作
示例:
class Person { public: 	 	Person() { 		cout << "无参构造函数!" << endl; 	} 	 	Person(int age ,int height) { 		 		cout << "有参构造函数!" << endl;
  		m_age = age; 		m_height = new int(height); 		 	} 	 	Person(const Person& p) { 		cout << "拷贝构造函数!" << endl; 		 		m_age = p.m_age; 		m_height = new int(*p.m_height); 		 	}
  	 	~Person() { 		cout << "析构函数!" << endl; 		if (m_height != NULL) 		{ 			delete m_height; 		} 	} public: 	int m_age; 	int* m_height; };
  void test01() { 	Person p1(18, 180);
  	Person p2(p1);
  	cout << "p1的年龄: " << p1.m_age << " 身高: " << *p1.m_height << endl;
  	cout << "p2的年龄: " << p2.m_age << " 身高: " << *p2.m_height << endl; }
  int main() {
  	test01();
  	system("pause");
  	return 0; }
  | 
 
总结:如果属性有在堆区开辟的,一定要自己提供拷贝构造函数,防止浅拷贝带来的问题
自己的实现:
#include<iostream> using namespace std; class Person { public: 	Person() { 		cout << "默认构造函数" << endl; 	}
  	Person(int age,int height) { 		cout << "有参构造函数" << endl; 		m_age = age; 		m_height = new int(height); 	}
  	 	Person(const Person& p) { 		cout << "拷贝构造函数" << endl; 		m_age = p.m_age; 		 		 		m_height = new int(*p.m_height); 		 	} 	~Person() { 		 		if (m_height != NULL) { 			delete m_height; 			m_height = NULL; 		} 		 		 		 		cout << "析构函数调用" << endl; 	} public: 	int m_age; 	int* m_height; }; void test01() { 	Person p1(18,180); 	cout << "年龄为" << p1.m_age << "身高为"<<*p1.m_height<<endl;
  	Person p2(p1); 	cout << "年龄为" << p2.m_age << "身高为"<<*p2.m_height<<endl; } int main() { 	test01(); 	return 0; }
   | 
 
4.2.6 初始化列表
作用:
C++提供了初始化列表语法,用来初始化属性
语法:构造函数():属性1(值1),属性2(值2)... {}
示例:
class Person { public:
  	 	 	 	 	 	
  	 	Person(int a, int b, int c) :m_A(a), m_B(b), m_C(c) {} 	void PrintPerson() { 		cout << "mA:" << m_A << endl; 		cout << "mB:" << m_B << endl; 		cout << "mC:" << m_C << endl; 	} private: 	int m_A; 	int m_B; 	int m_C; };
  int main() {
  	Person p(1, 2, 3); 	p.PrintPerson();
 
  	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 << p.m_a << endl; 	cout << p.m_b << endl; 	cout << p.m_c << endl; } int main() { 	test01(); 	return 0; }
   | 
 
4.2.7 类对象作为类成员
C++类中的成员可以是另一个类的对象,我们称该成员为 对象成员
例如:
B类中有对象A作为成员,A为对象成员
那么当创建B对象时,A与B的构造和析构的顺序是谁先谁后?
示例:
class Phone { public: 	Phone(string name) 	{ 		m_PhoneName = name; 		cout << "Phone构造" << endl; 	}
  	~Phone() 	{ 		cout << "Phone析构" << endl; 	}
  	string m_PhoneName;
  };
 
  class Person { public:
  	 	Person(string name, string pName) :m_Name(name), m_Phone(pName) 	{ 		cout << "Person构造" << endl; 	}
  	~Person() 	{ 		cout << "Person析构" << endl; 	}
  	void playGame() 	{ 		cout << m_Name << " 使用" << m_Phone.m_PhoneName << " 牌手机! " << endl; 	}
  	string m_Name; 	Phone m_Phone;
  }; void test01() { 	 	 	 	Person p("张三" , "苹果X"); 	p.playGame();
  }
 
  int main() {
  	test01();
  	system("pause");
  	return 0; }
   | 
 
自己写的:
#include<iostream> #include<string> using namespace std;
  class Phone { public: 	Phone(string pName) { 		m_Pname = pName; 		cout << "Phone的构造函数" << endl; 	}
  	~Phone() { 		cout << "Phone的析构函数" << endl; 	} 	 	string m_Pname;
  }; class Person { public: 	 	Person(string name, string pName) :m_Name(name), m_Phone(pName) 	{ 		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_Pname << endl; }
  int main() { 	test01(); 	return 0; }
   | 
 
4.2.8 静态成员
静态成员就是在成员变量和成员函数前加上关键字static,称为静态成员
静态成员分为:
- 静态成员变量
- 所有对象共享同一份数据
 
- 在编译阶段分配内存,即程序还没有运行之前就分配内存了。分配在全局区
 
- 类内声明,类外初始化
 
 
- 静态成员函数
- 所有对象共享同一个函数
 
- 静态成员函数只能访问静态成员变量
 
 
示例1 :静态成员变量
class Person { 	 public:
  	static int m_A; 
  	 	 	 	
  private: 	static int m_B;  }; int Person::m_A = 10; int Person::m_B = 10;
  void test01() { 	
  	 	Person p1; 	p1.m_A = 100; 	cout << "p1.m_A = " << p1.m_A << endl;
  	Person p2; 	p2.m_A = 200; 	cout << "p1.m_A = " << p1.m_A << endl;  	cout << "p2.m_A = " << p2.m_A << endl;
  	 	cout << "m_A = " << Person::m_A << endl;
 
  	 }
  int main() {
  	test01();
  	system("pause");
  	return 0; }
   | 
 
自己写的:
#include<iostream> using namespace std; class Person { public: 	static int m_a;
  	 private: 	static int m_b; }; int Person::m_a = 100; int Person::m_b = 200; void test01() { 	Person p; 	cout << p.m_a << endl;
  	Person p2; 	p.m_a = 200; 	cout << p.m_a << endl; }
  void test02() { 	 	 	 	 	Person p; 	cout << p.m_a << endl; 	 	 	cout << Person::m_a << endl;
  	 } int main() { 	 	test02(); 	return 0; }
 
   | 
 
示例2:静态成员函数
class Person {
  public:
  	 	 	 	 	static void func() 	{ 		cout << "func调用" << endl; 		m_A = 100; 		 	}
  	static int m_A;  	int m_B;  private:
  	 	static void func2() 	{ 		cout << "func2调用" << endl; 	} }; int Person::m_A = 10;
 
  void test01() { 	
  	 	Person p1; 	p1.func();
  	 	Person::func();
 
  	 }
  int main() {
  	test01();
  	system("pause");
  	return 0; }
   | 
 
自己写的:
#include<iostream> using namespace std;
  class Person { public: 	static void func() { 		m_a = 200; 		 		cout << "static void func函数" << endl; 	}
  	static int m_a; 	int m_b;
  	 private: 	static void func2() { 		cout << "static void func2函数" << endl; 	} }; int Person::m_a = 100;
  void test01() { 	 	Person p; 	p.func();
  	 	cout << Person::func << endl; 	Person::func();
  	 	 } int main() { 	test01(); 	return 0; }
   |