黑马教程学习笔记——对象特性(1)

1、构造函数和析构函数

#include <iostream>
using namespace std;
//对象的初始化和清理
class Person
{
	//1、构造函数
	//没有返回值 不用写void
	//函数名 与类名相同
	//构造函数可以有参数 可以发生重载
	//创建对象的时候吧,构造函数会自动调用,而且只调用一次
public:
	Person()
	{
		cout << "构造函数调用" << endl;//不写的话,也会打印空值
	}
	//2、析构函数 进行清理的操作
	//没有返回值 不用写void
	~Person()
	{
		cout << "析构函数调用" << endl;
	}
};
//构造和析构都是必须有的实现 如果我们自己不提供 编译器会自动提供
void test01()
{
	Person p;//在栈上的数据,test01执行完毕后,释放这个对象
}
int main()
{
	//test01();//构造和析构函数都会执行
	Person p;//只会执行构造
	system("pause");
	return 0;
}

2、构造函数分类

#include <iostream>
using namespace std;
//1、构造函数的分类及调用
//分类
//按参数分类 无参构造 和 有参构造
//按类型分类 普通构造 和 拷贝构造
class Person
{
public:
	//构造函数
	Person()
	{
		cout << "构造函数的调用" << endl;
	}
	Person(int a)
	{
		age = a;
		cout << "有参构造函数调用" << endl;
	}
	//拷贝构造函数
	Person(const Person &p)
	{
		//将传入的人身上的所有属性,拷贝到我身上
		age = p.age;
	}
	//析构函数
	~Person()
	{
		cout << "析构函数的调用" << endl;
	}
	int age;
};
void test01()
{
	//1、括号法
	//Person p;//默认构造函数调用 不要加()
	//Person p1(10);//有参构造函数调用
	//Person p2(p1);//拷贝构造函数调用
	//注意事项
	//调用默认构造函数时,不要加()否则 编译器会认为是一个函数声明

	//cout << p1.age << endl;
	//cout << p2.age << endl;

	//2、显示法
	//Person p;
	//Person p1 = Person(10);
	//Person p2 = Person(p1);

	//Person(10);//匿名对象 特点:当前执行结束后 系统会立即收掉匿名对象
	//cout << "aa" << endl;

	//注意事项:不要用拷贝函数初始化匿名对象,编译器会认为Person(p2)====Person p3;
	//3、隐式转换法
	Person p4 = 10;//想当于显示法
}
int main()
{
	test01();//调用默认函数
	system("pause");
	return 0;
}

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

#include <iostream>
using namespace std;
//c++拷贝构造函数调用时机分三种
//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;
	}
	int age;
};
//1、使用一个已经创建完毕的对象初始化一个新对象
void test01()
{
	Person p1(10);
	Person p2(p1);
	cout << p1.age << endl;
	cout << p2.age << endl;
}
//2、值传递的方式给函数参数传值
void doWork(Person p)
{

}
void test02()
{
	Person p;
	doWork(p);
}
//3、值方式返回局部对象
Person doWork2()
{
	Person p1;
	return p1;
}
void test03()
{
	Person p = doWork2();
}
int main()
{
	//test01();
	//test02();
	test03();
	system("pause");
	return 0;
}

3、构造函数的调用规则

#include <iostream>
using namespace std;
//构造函数的调用规则
//1、创建一个类 C++编译器会给每个类都添加至少3个函数
//默认构造 (空实现)
//析构函数 (空实现)
//拷贝构造 (值拷贝)

//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;
	}
	int age;
};
//1、使用一个已经创建完毕的对象初始化一个新对象
//void test01()
//{
//	//Person p;
//	p.age = 18;
//	Person p2(p);
//	cout << p2.age << endl;
//}
void test02()
{
	//Person p;
	Person p(20);
	Person p2(p);
}
int main()
{
	//test01();
	test02();
	system("pause");
	return 0;
}

4、深拷贝和浅拷贝

#include <iostream>
using namespace std;

class Person
{
public:
	//构造函数
	Person()
	{
		cout << "默认构造函数的调用" << endl;
	}
	Person(int a,int b)
	{
		age = a;
		height = new int(b);
		cout << "有参构造函数调用" << endl;
	}
	Person(const Person &p)
	{
		cout << "拷贝构造函数调用" << endl;
		age = p.age;
		//height = p.height;//编译器默认实现这行代码,有误,需要自己进行深拷贝操作

		//深拷贝操作
		height = new int(*p.height);
	}
	
	//析构函数
	~Person()
	{
		//析构代码 将堆区开辟数据做释放操作
		if (height != NULL)
		{
			delete height;
			height = NULL;
		}
		cout << "析构函数的调用" << endl;
	}
	int age;
	int *height;
};

void test01()
{
	
	Person p(18,160);
	Person p2(p);
	cout << p.age <<*p.height<< endl;
	cout << p2.age <<*p2.height<< endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

5、初始化列表

#include <iostream>
using namespace std;

class Person
{
public:
	//传统初始化操作
	/*Person(int a,int b,int c)
	{
		m_A = a;
		m_B = b;
		m_C = c;
	}*/
	//初始化列表初始化属性
	/*Person() :m_A(10), m_B(20), m_C(30)
	{

	}*/
	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;
	Person p(20,30,10);
	cout << p.m_A << p.m_B << p.m_C << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值