3、对象的构造 - 构造函数

三、对象的构造

1.构造函数

  • 构造函数的作用是对对象进行初始化,每个类都有构造函数,如果不自己写,编译器会自动生成一个默认的无参构造函数
  1. 空间上创建对象时,成员变量初始值为随机值
#include <iostream>
#include <string>

using namespace std;

class Test
{
private:
	int i;
	int j;

public:
	int getI() { return i; }
	int getJ() { return j; }
};

Test gt;					// 全局区对象

int main()
{
	cout << "gt.i = " << gt.getI() << endl;// 0 全局数据区统一为0
	cout << "gt.j = " << gt.getJ() << endl;// 0

	Test t1;			// 栈区对象
	cout << "t1.i = " << t1.getI() << endl;// 78521 栈空间初始值不定
	cout << "t1.J = " << t1.getJ() << endl;// 55215

	//这里把Test看成自定义的数据类型,定义一个指向Test类对象的指针pt。
	Test* pt = new Test;// 堆区对象
	cout << "pt->i = " << pt->getI() << endl;// 122654 堆空间初始值不定
	cout << "pt->j = " << pt->getJ() << endl;// 232164

	delete pt;  // 释放堆空间对象

	return 0;
}
  1. 构造函数的作用是对对象自动初始化
  2. 构造函数与类名相同,没有返回值,在对象定义时自动被调用
#include <iostream>
#include <string>

using namespace std;

class Test
{
private:
	int i;
	int j;

public:
	int getI() { return i; }
	int getJ() { return j; }
	Test()
	{
		cout << "Test() Begin" << endl;
		i = 1;
		j = 2;
		cout << "Test() End" << endl;;
	}
};

Test gt;									 // Test() Begin 创建对象时调用构造函数
											 // Test() End
int main()
{
  	cout << "gt.i = " << gt.getI() << endl;	 // 1
	cout << "gt.j = " << gt.getJ() << endl;	 // 2

	Test t1;								 // Test() Begin
											 // Test() End
	cout << "t1.i = " << t1.getI() << endl;  // 1
	cout << "t1.J = " << t1.getJ() << endl;  // 2

	Test* pt = new Test;					 // Test() Begin
											 // Test() End
	cout << "pt->i = " << pt->getI() << endl;// 1
	cout << "pt->j = " << pt->getJ() << endl;// 2

	delete pt;

	return 0;
}
  1. 带有参数的构造函数,满足重载规律
  2. 初始化和赋值是不一样的
  3. 初始化调用构造函数,赋值调用拷贝构造函数
#include <iostream>
#include <string>

using namespace std;

class Test
{
public:
	Test()
	{
		cout << "Test()" << endl;
	}
	Test(int v)
	{
		cout << "Test(int v), v = " << v << endl;
	}
};

int main()
{
	Test t;  		// 调用 Test()
	Test t1(1);  	// 调用 Test(int v) 第一种对象初始化方式
	Test t2 = 2; 	// 调用 Test(int v) 第二种对象初始化方式

	int i(100);     // 初始化

	cout << "i = " << i << endl; // 100

	return 0;
}
  1. 为了灵活的初始化对象数组,需要手动调用构造函数
#include <iostream>
#include <string>

using namespace std;

class Test
{
private:
	int m_value;
public:
	Test()
	{
		cout << "Test()" << endl;

		m_value = 0;
	}
	Test(int v)
	{
		cout << "Test(int v), v = " << v << endl;

		m_value = v;
	}
	int getValue()
	{
		return m_value;
	}
};

int main()
{
	Test ta[3] = { Test(), Test(1), Test(2) };// 对象数组初始化

	for (int i = 0; i < 3; i++)
	{
		cout << "ta[i].getValue() = " <<ta[i].getValue() << endl;

	}

	Test t = Test(100);// 第三种对象初始化方式

	cout << "t.getValue() =" << t.getValue();

	return 0;
}

2. 无参构造函数

  • 当类中没有定义构造函数(包括拷贝构造函数)时,编译器默认生成一个无参构造函数,并且函数体为空
#include <iostream>
#include <string>

using namespace std;

class Test

{
private:
	int i;
	int* p;
public:
	int getI()
	{
		return i;
	}
	/*Test()				
	{

	}
	Test(const Test& t)
	{
		i = t.i;
	}*/
};

int main()
{
	Test t1;
	Test t2 = t1;

	cout << " t1.i = " << t1.getI() << endl;
	cout << " t2.i = " << t2.getI() << endl;

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值