生活中我们买的电子产品都基本会有出厂设置,在某一天我们不用的时候也会删除一些自己信息数据保证安全。
C++中的面向对象来源于生活,每个对象也都会有初始设置以及对象销毁前的清理数据的设置。
4.2.1构造函数和析构函数
对象的初始化和清理也是两个非常重要的安全问题
一个对象或者变量没有初始状态,对其使用后果是未知
同样的使用完一个对象或变量,没有及时清理,也会造成一定的安全问题
c++利用了构造函数和析构函数解决上述问题,这两个函数将会被编译器自动调用,完成对象初始化和清理工作是编译器强制要我们做的事情,因此如果我们不提供构造和析构,编译器会提供编译器提供的构造函数和析构函数是空实现。
构造函数:主要作用在于创建对象时为对象的成员属性赋值,构造函数由编译器自动调用,无须手动调用。
析构函数:主要作用在于对象销毁前系统自动调用,执行一些清理工作。
构造函数语法:类名(){}
1.构造函数,没有返回值也不写void
2.函数名称与类名相同
3.构造函数可以有参数,因此可以发生重载
4.程序在调用对象时候会自动调用构造,无须手动调用,而且只会调用一次
析构函数语法:~类名(){}
1.析构函数,没有返回值也不写void
2.函数名称与类名相同,在名称前加上符号 ~
3.析构函数不可以有参数,因此不可以发生重载
4.程序在对象销毁前会自动调用析构,无须手动调用,而且只会调用一次
#include<stdc++.h>
using namespace std;
class Person
{
public:
Person()
{
cout << "Person构造函数的调用" << endl;
}
~Person()
{
cout << "Person的析构函数的调用" << endl;
}
};
void test01()
{
Person p;//在栈上的数据,test01执行完毕后,释放对象
}
int main()
{
Person p;
system("pause");
}
4.2.2构造函数的分类及调用
两种分类方式:
按参数分为:有参构造和无参构造
按类型分为:普通构造和拷贝构造
三种调用方式:
括号发
显示法
隐式转换法
#include<stdc++.h>
using namespace std;
class Person
{
public:
Person()//无参构造
{
cout << "Person的构造函数调用" << endl;
}
Person(int a)//有参构造
{
age = a;
cout << "Person的构造函数调用" << endl;
}
//拷贝构造函数
Person(const Person &p)
{
cout << "Person的拷贝函数调用" << endl;
//将传入的人身上所有属性,拷贝到我身上
age = p.age;
}
~Person()
{
cout << "Person的析构函数" << endl;
}
int age;
};
//调用
void test01()
{
1.括号法
//Person p1;//默认构造函数调用
//Person p2(10);//有参构造函数
//Person p3(p2);//拷贝构造函数
注意事项
调用默认构造函数的时候,不要加()
如果加了那么代码,编译器会认为是一个函数的声明,不会认为在创建对象
//
2.显示法
//Person p1;
//Person p2 = Person(10);//有参构造
//Person p3 = Person(p2);//拷贝构造
Person(10);//匿名对象 特点:当前行执行结束后,系统会立即回收掉匿名对象
//注意事项2
//不要利用拷贝构造函数 初始化匿名对象 编译器会认为Person(p3)=== Person p3;
//3.隐式转换法
Person p4 = 10;//相当于 写了 Person p4 = Person(10);
Person p5 = p4;
}
int main()
{
test01();
}
4.2.3拷贝构造函数调用时机
c++中拷贝构造函数调用时机通常有三种情况
1.使用一个已经创建完毕的对象来初始化一个新对象
2.值传递的方式给函数参数传值
3.以值方式返回局部对象
#include<stdc++.h>
using namespace std;
//拷贝构造函数调用时机
//1.使用一个已经创建完毕的对象来初始化一个新对象
//2.值传递的方式给函数参数传值
//3.值方式返回局部对象
class Person
{
public:
Person()
{
cout << "Person默认构造函数调用" << endl;
}
Person(int age)
{
cout << "Person有参构造函数调用" << endl;
m_Age = age;
}
Person(const Person& p)
{
cout << "Person拷贝构造函数调用" << endl;
m_Age = p.m_Age;
}
int m_Age;
};
void test01()
{
Person p1(20);
Person p2(p1);
cout << "P2的年龄为: " << p2.m_Age << endl;
}
void doWork(Person p)
{
}
void test02()
{
Person p;
doWork(p);
}
Person doWork2()
{
Person p1;
return p1;
}
void test03()
{
Person p = doWork2();
cout << (int*)&p << endl;
}
int main()
{
//test01();
//test02();
test03();
return 0;
}
4.2.4构造函数调用规则
默认情况下,c++编译器至少给一个类添加3个函数
1.默认构造函数(无参,函数体为空)
2.默认析构函数(无参,函数体为空)
3.默认拷贝构造函数,对属性进行值拷贝
构造函数调用规则如下:
如果用户定义有参构造函数,c++不在提供给默认无参构造,但是会提供默认拷贝构造
如果用户定义拷贝构造函数,c++不会再提供其他构造函数
#include<stdc++.h>
using namespace std;
//构造函数的调用规则
//1.创建一个类,c++编译器会给每个类都添加至少3个函数
//默认构造(空实现)
//析构函数(空实现)
//拷贝构造(值拷贝)
//2.如果我们写了有参构造函数,编译器就不再提供默认构造,依然提供拷贝构造
//如果我们写了拷贝构造函数,编译器就不再提供其他普通构造函数了
class Person
{
public:
Person()
{
cout << "Person的默认构造函数调用" << endl;
}
Person(int age)
{
cout << "Person的有参构造函数调用" << endl;
m_Age = age;
}
Person(const Person& p)
{
cout << "Person的拷贝构造函数调用" << endl;
m_Age = p.m_Age;
}
~Person()
{
cout << "Person的析构函数调用" << endl;
}
int m_Age;
};
void test01()
{
Person p;
p.m_Age = 18;
Person p2(p);
cout << "p2的年龄为:" << p2.m_Age << endl;
}
void test02()
{
Person p(18);
Person p2(p);
cout << "p2的年龄为:" << p2.m_Age << endl;
}
int main()
{
// test01();
test02();
}
4.2.5深拷贝与浅拷贝
深浅拷贝是面试经典问题,也是常见的一个问题
浅拷贝:简单的赋值拷贝操作
深拷贝:在堆区重新申请空间,进行拷贝操作
#include<stdc++.h>
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()
{
//析构代码,将堆区开辟数据做释放操作
if (m_Height != NULL)
{
delete m_Height;
m_Height = NULL;
}
cout << "Person的析构函数调用" << endl;
}
//深拷贝
Person(const Person& p)
{
cout << "Person 拷贝构造函数调用" << endl;
m_Age = p.m_Age;
m_Height = new int(*p.m_Height);
}
int m_Age;
int* m_Height;
};
void test01()
{
Person p1(18,160);
cout << "p1的年龄为:" << p1.m_Age <<"身高为:"<<*p1.m_Height<< endl;
Person p2(p1);
cout << "p2的年龄为:" << p2.m_Age << "身高为:" << *p2.m_Height<<endl;
}
int main()
{
test01();
}
4.2.6 初始化列表
作用:
C++提供了初始化列表语法,用来初始化属性
语法:构造函数():属性1(值1),属性2(值2)...{ }
#include<stdc++.h>
using namespace std;
class Person
{
public:
/*Person(int a, int b, int c)
{
m_A = a;
m_B = b;
m_C = c;
}*/
//初始化列表初始化属性
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);
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();
}
4.2.7 类对象作为类成员
C++类中的成员可以是另一个类的对象,我们称该成员为 对象成员
例如:
class A { }
class B
{
A a;
}
B类中有对象A作为成员,A为对象成员
那么当创建B对象时,A与B的构造和析构的顺序是谁先谁后?
当其他类对象作为本类成员,构造时候先构造类对象,再构造自身,析构的顺序与构造相反
4.2.8 静态成员
静态成员就是再成员变量和成员函数前加上关键字static,称为静态成员
静态成员分为:
静态成员变量
所有对象共享同一份数据
在编译阶段分配内存
类内声明,类外初始化
#include<stdc++.h>
using namespace std;
//静态成员变量
class Person
{
public:
//1.所有对象都共享同一份数据
//2.编译阶段就分配内存
//3.类内声明,类外初始化操作
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;
p2.m_A = 200;
cout << p.m_A << endl;
}
void test02()
{
//静态成员变量 不属于某个对象上,所有对象都共享同一份数据
//因此静态成员变量有两种访问方式
//1.通过对象进行访问
/*Person p;
cout << p.m_A << endl;*/
//2.通过类名进行访问
cout << Person::m_A << endl;
}
int main()
{
test01();
}
静态成员函数
所有对象共享同一个函数
静态成员函数只能访问静态成员变量
#include<stdc++.h>
using namespace std;
//静态成员函数
//所有对象共享同一个函数
//静态成员函数只能访问静态成员变量
class Person
{
public:
static void func()
{
m_A = 100;//静态成员函数可以访问 静态成员变量
//m_B = 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 = 0;
//有两种访问方式
void test01()
{
//1.通过对象访问
Person p;
p.func();
//2.通过类名访问
Person::func();
}
int main()
{
test01();
return 0;
}