--事物的难度远远低于对事物的恐惧!
上一章,我们简单的了解了C++ 中的构造函数,那么这节我们再来分析下C++中的构造函数,在上一节中,我们通过构造函数去初始化类对象,但是细心的朋友会发现,我们定义的三个类对象,它们的初始化值都一样,那么有没有办法使得不同类对象拥有各自的初始化值?
要解决上边的疑问,我们首先来回顾先C++中构造函数
- 无返回值,函数名与类名相同
这里并没有提到构造函数的参数问题,没错,C++可以定义带有参数的构造函数
- 一个类中可以存在多个带参构造函数,这些函数可以构成重载关系
- 构造函数的重载遵循C++重载规则
有了以上两点,我们重新更新上章节的代码
#include <iostream>
using namespace std;
class testClass
{
private:
int i;
int j;
public:
testClass()
{
cout << "testClass() called" << endl;
i = 1;
j = 2;
}
testClass(int m)
{
cout << "testClass(int m) called" << endl;
i = m;
j = 0;
}
testClass(int m, int n)
{
cout << "testClass(int m, int n) called" << endl;
i = m;
j = n;
}
int getI()
{
return i;
}
int getJ()
{
return j;
}
};
int main()
{
testClass t1; //调用 testClass()
testClass t2(3); //调用 testClass(int m)
testClass t3(5, 6); //调用 testClass(int m, int n)
cout << "t1.getI() = " << t1.getI() << endl;
cout << "t1.getJ() = " << t1.getJ() << endl << endl;
cout << "t2.getI() = " << t2.getI() << endl;
cout << "t2.getJ() = " << t2.getJ() << endl << endl;
cout << "t3.getI() = " << t3.getI() << endl;
cout << "t3.getJ() = " << t3.getJ() << endl;
system("pause");
return 0;
}
编译输出如下:
由输出我们可以看到,类中是允许多个不同参数的构造函数,同时他们之间是遵循C++函数重载规则的。
目前为止,构造函数都是在对象定义时被自动调用,但是在一些特殊情况下,需要手工调用构造函数
-思考:如何创建一个对象数组,并且数组中每个元素的初始值不一样
要实现上述需求,就需要我们定义对象数组时手工调用构造函数,代码如下
#include <iostream>
using namespace std;
class testClass
{
private:
int i;
public:
testClass()
{
cout << "testClass() called" << endl;
i = 1;
}
testClass(int m)
{
cout << "testClass(int m) called" << endl;
i = m;
}
int getI()
{
return i;
}
};
int main()
{
testClass t[3] = {testClass(), testClass(1), testClass(2)};
for (int i=0; i<3; i++)
{
cout << "t[" << i << "] = " << t[i].getI() << endl;
}
system("pause");
return 0;
}
编译输出:
从输出我们看到,在数组定义时,我们手工调用了构造函数testClas()、testClas(1)、testClas(2),函数重载使得调用不同的构造函数,也就实现了数组元素初始化为不同的值。
到了这里,相信你 已经对构造函数有了一定的认识了,下边以一个小例子来感受下构造函数的妙用之处
-设计一个数组类,能获取数组长度、获取 指定位置的元素值、设置指定位置的元素值
首先来 看下头文件IntArray.h实现
//头文件 IntArray.h
#ifndef __INTARRAY_H__
#define __INTARRAY_H__
class IntArray
{
private:
int m_length;
int *m_pArray;
public:
IntArray(int len);
int length(); //获取数组长度
bool get(int index, int &value); //获取指定位置的元素
bool set(int index, const int value); //设置指定位置的元素
void free();
};
#endif //__INTARRAY_H__
源文件IntArray.cpp实现
//IntArray.cpp文件
#include "IntArray.h"
IntArray::IntArray(int len) //构造函数,申请数组空间,初始化数组长度
{
m_pArray = new int[len];
for (int i=0; i<len; i++)
{
m_pArray[i] = 0;
}
m_length = len;
}
int IntArray::length()
{
return m_length;
}
bool IntArray::get(int index, int &value)
{
bool ret = (index >=0) && (index < m_length);
if (ret)
{
value = m_pArray[index];
}
return ret;
}
bool IntArray::set(int index, const int value)
{
bool ret = (index >=0) && (index < m_length);
if (ret)
{
m_pArray[index] = value;
}
return ret;
}
void IntArray::free()
{
delete[] m_pArray;
}
main.cpp文件使用数组类
#include <iostream>
#include "IntArray.h"
using namespace std;
int main()
{
IntArray array(5); //定义数组对象,元素个数为5
for (int i=0; i<array.length(); i++)
{
array.set(i, i+1);
}
for (int i=0; i<array.length(); i++)
{
int temp = -1;
if (array.get(i, temp))
{
cout << temp << endl;
}
}
array.free();
system("pause");
return 0;
}
编译输出如下:
这个我们自定义 的数组类,对元素的操作,均有越界判定,也就是具有安全性。
总结:
- 构造函数可以根据需要定义参数
- 构造函数之间可以存在重载关系
- 构造 函数遵循C++中重载函数的规则
- 对象定义时会触发构造函数的调用
- 某些情况下可以手动调用构造函数