书P127:在介绍为什么对对象赋初值时:
(这本破书编的语序有点混乱,明明是很简单很清楚的事情总是被他莫名其妙搞得很复杂,所以对此我重新做了整理和归纳)
在定义类时
- 不能对成员变量进行初始化
- 也不能声明对象后用赋值运算对(成员变量)初始化
因为:
- 无法确定成员变量属于哪一个对象
- 成员变量一般定义为私有属性
成员变量的初始化一般是利用一个名为构造函数的成员函数来完成。
作用:创建对象时((用特定的初始值)构造对象)把对象置于某一个初始状态
构造函数在创建对象时由系统自动调用
例8-8 自定义默认构造函数。
源程序:
#include
using namespace std;
class Box
{
private:
int height; int width; int length;
public:
Box();//声明一个无参数的构造函数
Box(int h = 10, int w = 10, int len = 10);//指定默认参数值的构造函数
int Volume();
};
Box::Box()//定义一个无参数的构造函数
{
height = 10; width = 10; length = 10;
}
Box::Box(int h, int w, int len)
{
height = h;
width = w;
length = len;
}
int Box::Volume()
{
return (height * width * length);
}
int main()
{
Box box1;//建立对象 boxl,不指定实参
cout << "The volume of boxl is " << box1.Volume() << endl;
Box box4(15, 30, 25);
//建立对象 box4,指定 3个实参
cout << "The volume of box4 is " << box4.Volume() << endl;
return 0;
}
结果:
书P128:
注意:一个类只能有一个默认构造函数。
也就是说上述两种方式定义的构造函数不能同时出现。
编译时会提示指定了多个默认构造函数,调用不明的错误。
书P130:
默认构造函数(defauk conutruetor):
调用构造函数时不必给出实参的构造函数;
显然,无参的构造函数属于默认构造函数。
解释:
没有参数,也就是说:没有实参也没有形参,自然不用给出形参和实参
也就是说:
要么没有参数,要么所有的参数都指定(给定)了默认值的所有构造函数
就是我们所说的默认构造函数(调用构造函数时不必给出实参的构造函数)
然儿有意思的是:
这里我们其实不用删去整段构造函数“Box();或Box(int h = 10, int w = 10, int len = 10);”的定义和声明,
只要删掉
Box box1;//建立对象 boxl,不指定实参
cout << "The volume of boxl is " << box1.Volume() << endl;
程序就能正常运行了:
project 1:
#include
using namespace std;
class Box
{
private:
int height; int width; int length;
public:
Box();//声明一个无参数的构造函数
Box(int h = 10, int w = 10, int len = 10);//指定默认参数值的构造函数
int Volume();
};
Box::Box()//定义一个无参数的构造函数
{
height = 10; width = 10; length = 10;
}
Box::Box(int h, int w, int len)
{
height = h;
width = w;
length = len;
}
int Box::Volume()
{
return (height * width * length);
}
int main()
{
Box box4(15, 30, 25);
//建立对象 box4,指定 3个实参
cout << "The volume of box4 is " << box4.Volume() << endl;
return 0;
}
结果:
The volume of box4 is 11250
这说明:
一个类其实可以有不止一个默认构造函数,当然前提是我们指定的对象具有(指定的)实参(就可以有不止一个)
另外,根据后面我们会给出的project 3我们可以看到:
project 1可以运行成功
不是因为对象有实参一个类就可以有不止一个默认构造函数
而是因为它实际上省略了Box();构造函数,而已
如果我们一定要输出(无参数的构造函数)“box1”,删掉【Box();】的定义和声明即可:
project 2:
#include
using namespace std;
class Box
{
private:
int height; int width; int length;
public:
Box(int h = 10, int w = 10, int len = 10);//指定默认参数值的构造函数
int Volume();
};
Box::Box(int h, int w, int len)
{
height = h;
width = w;
length = len;
}
int Box::Volume()
{
return (height * width * length);
}
int main()
{
Box box1;//建立对象 boxl,不指定实参
cout << "The volume of boxl is " << box1.Volume() << endl;
Box box4(15, 30, 25);
//建立对象 box4,指定 3个实参
cout << "The volume of box4 is " << box4.Volume() << endl;
return 0;
}
结果:
另外,这里如果把box1的定义改为:
Box box1(1);
结果:
The volume of boxl is 100
根据前面运行不成功是“一个类只能有一个默认构造函数”的原因,在这里我们只要删去构造函数【Box();或Box(int h = 10, int w = 10, int len = 10);其中任一】定义和声明即可让程序运行
但是在这里,如果删掉Box(int h = 10, int w = 10, int len = 10);的定义和声明
box1能输出,但是无法输出box4:
project 3:
#include
using namespace std;
class Box
{
private:
int height; int width; int length;
public:
Box();//声明一个无参数的构造函数
int Volume();
};
Box::Box()//定义一个无参数的构造函数
{
height = 10; width = 10; length = 10;
}
int Box::Volume()
{
return (height * width * length);
}
int main()
{
Box box1;//建立对象 boxl,不指定实参
cout << "The volume of boxl is " << box1.Volume() << endl;
Box box4(15, 30, 25);
//建立对象 box4,指定 3个实参
cout << "The volume of box4 is " << box4.Volume() << endl;
return 0;
}
结果:
所以这里project 3可以证明:
前面的project 1可以运行成功
不是因为对象有实参一个类就可以有不止一个默认构造函数
而是因为它实际上省略了Box();构造函数,而已
所以我们可以总结上述三个程序,很容易得出(总的,概括性的)结论:
- 只能(可以)少于构造函数指定(定义)的默认参数个数
- 不能多于构造函数指定(定义)的默认参数个数
另外:
这里由于(int h, int w, int len)是形参,(int height; int width; int length)是私有成员不可访问
所以在这里我们根本无法访问我们建立的对象(不管叫什么名字,因为我们没有专门写用来访问这几个属性的函数)
可以参考:
C语言日记 32 类的对象,this指针_宇 -Yu的博客-CSDN博客