C++学习(七)—类和对象(二)

构造函数和析构函数

#include<iostream>

using namespace std;

class MYclass
{
public:
	//  1、构造函数的写法
	//  没有返回值   也不写void  函数名与类名相同
	//	由编译器自动调用  不需要手动调用;而且编译器只会调用一次

	MYclass()
	{
		cout << "MYclass的构造函数调用" << endl;
	}

	//  2、析构函数的写法
	//  没有返回值  不写void 函数名与类名相同  在函数名称前加~
	//	不可以有参数,不可以发生重载
	//	由编译器自动调用,不需要手动调用,编译器也只调用一次

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

void test01()
{
	MYclass c;
}

int main()
{
	test01();
	system("pause");
	return 0;
}

构造函数的分类与调用

#include<iostream>

using namespace std;

//  分类
//  1、按照参数进行分类  有参构造函数  无参构造函数(默认构造函数)
//	2、按照类型进行分类  普通构造函数  拷贝构造函数


class Persion
{
public:
	//  默认
	Persion()
	{
		cout << "Persion的无参函数调用" << endl;
	}
	//	有参
	Persion(int age)
	{
		m_age = age;
		cout << "Persion的有参函数调用" << endl;
	}

	int m_age;
	//	拷贝
	Persion(const Persion &p)	//  Persion(const 类名 & )
	{
		m_age = p.m_age;
		cout << "Persion的拷贝函数调用" << endl;
	}

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


//	调用

void test01()
{
	//  无参构造函数
	Persion p;
	//	有参构造函数
	//括号法
	Persion p2(10);
	//	拷贝构造函数
	Persion p3(p2);

	//	显示法
	Persion p4 = Persion(10);	//	有参构造函数
	Persion P5 = Persion(p4);	//	拷贝构造函数

	//	注意
	Persion p();	//	不可以用括号法构造无参函数  原因Persion p()编译器认为是一个函数的声明;
	Persion(10);	//	匿名函数对象  特点:当前行执行完后  系统就回收 
	Persion(p5);	//  不可以用调用拷贝构造函数  来初始化匿名函数对象;编译器认为代码  Persion p5;

	//	隐式法
	Persion p6 = 10;	//  Persion p6 = 10;  相当于写了  Persion p6 = Persion(10)
	Persion p7 = p6;	//	
}


int main()
{
	test01();
	system("pause");
	return 0;
}

拷贝构造函数的调用时机

#include<iostream>

using namespace std;

class Persion
{
public:
	Persion()
	{
		cout << "无参构造函数调用" << endl;
	}
	Persion(int age)
	{
		m_age = age;
		cout << "有参构造函数调用" << endl;
	}
	Persion(const Persion &p)
	{
		cout << "拷贝构造函数调用" << endl;
		m_age = p.m_age;
	}
	~Persion()
	{
		cout << "析构函数调用" << endl;
	}
	int m_age;
};

//	1、使用一个已经创建好的对象来初始化另一个对象

void test01()
{
	Persion p1(18);

	Persion p2(p1);

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

//	2、以值传递的方式,给函数的参数传值

void f(Persion p)	//  值传递  拷贝
{
	;
}

void test02()
{
	Persion p1;
	f(p1);
}

//  3、以值的形式返回局部对象

Persion f2()
{
	Persion p1;
	return p1;
}


void test03()
{
	Persion p1 = f2();
}


int main()
{
	//	test01();
	//	test02();
		test03();
	system("pause");
	return 0;
}

构造函数的调用规则

#include<iostream>

using namespace std;


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

//  1、系统默认给一个类添加至少3个函数  默认构造函数 、析构函数 、拷贝构造函数(简单的值拷贝)


void test01()
{
	Persion p1(10);
	Persion P2(p1);

	cout << "p2.age = " << P2.m_age << endl;
}

//  2、如果我们提供了有参的构造函数,那么系统就不会提供默认的构造函数,但是仍然会提供拷贝构造函数

void test02()
{
	//  Persion p;
	Persion p1(18);
	Persion p2(p1);
	cout << "p2.age = " << p2.m_age << endl;
}

//	3、如果我们提供了拷贝构造函数,那麽系统就不会提供其他的构造函数

void test03()
{

}

int main()
{
	//	test01();
	test02();
	system("pause");
	return 0;
}
发布了31 篇原创文章 · 获赞 8 · 访问量 3670

猜你喜欢

转载自blog.csdn.net/qq_42689353/article/details/104545408
今日推荐