3.4.1构造函数
1.构造函数对的作用
构造函数是累的一个特殊的成员函数,构造函数的作用是在创建对象时对对象的数据成员进行初始化,看下面的例子
【例3-9】
#include "iostream"
using namespace std;
class box
{
public:
box()
{
length = 1;width = 2;height = 3;
cout << "Box's constructor is executed !" << endl;
}
void show()
{
cout << "length is " << length<<endl;
cout << "width is " << width<<endl;
cout << "height is" <<height<< endl;
}
private:
float length, height, width;
};
int main()
{
box box1;
cout << "The inforation of box1 is " << endl;
box1.show();
box box2;
cout << "The inforation of box2 is " << endl;
box2.show();
system("pause");
return 0;
}
总结:
(1)构造函数与类名相同,且没有返回值,定义构造函数时在函数名前什么也不加,(加 void 也不可以)
(2)构造函数不需要用户调用,它是由系统在创建对象时自动调用的,鉴于此,构造函数要声明为public访问属性
(3)C++系统在 创建对象时必须执行一个构造函数,否则系统无法创造对象,如果用户自己没有定义构造函数,则C++系统会自动提供一个构造函数。成为默认的构造函数,这个构造函数没有函数体,没有参数,不能进行初始化操作。
(4)特别提醒:类是一种抽象的自定义数据类型,它并不占用内存空间,所以不能在类内对数据成员进行初始化,如下面的初始化格式是错误的
class box
{
...
private:
int length =0;
...
}
2.带参数的构造函数
前面的例子是不带参数的构造函数,一般格式为 构造函数(参数表)。实参在定义对象时,一般格式为 类名 对象名 (参数表)
#include "iostream"
using namespace std;
class box
{
public:
box(float l,float w,float h) //带有三个形参的构造函数
{
length = l;width = w;height = h;
}
float volume()
{
return length*height*width;
}
void show()
{
cout << "length is " << length<<endl;
cout << "width is " << width<<endl;
cout << "height is" <<height<< endl;
}
private:
float length, height, width;
};
int main()
{
box box1(1, 2, 3);
box box2(1, 1, 1);
cout << "The volume of box1 is " << box1.volume() << endl;
cout << "The volume of box2 is " << box2.volume() << endl;
system("pause");
return 0;
}
3.构造函数与参数初始化表
class box
{
public:
//使用参数初始化表的构造函数
box(float l,float w,float h):length (l);width (w);height (h);
float volume()
{
return length*height*width;
}
private:
float length, height, width;
};
4.构造重载函数
#include <iostream>
using namespace std;
class Box
{public:
Box( ) //无参数的构造函数
{ length = 1; width = 1; height = 1; }
Box(float L, float W, float H) //带有3个形参的构造函数
{ length = L; width = W; height = H; }
float Volume( ){ return length*width*height; }
private:
float length, width, height;
};
int main( )
{
Box box1(4, 2, 3); //调用带有3个形参的构造函数创建对象
Box box2; //调用没有参数的构造函数创建对象
cout << "The volume of box1 is " << box1.Volume( ) << endl;
cout << "The volume of box2 is " << box2.Volume( ) << endl;
//system("pause");
return 0;
}
注意:使用参数实例化对象时的格式为 类名 对象名(实参表)
使用默认构造函数实例化对象时的格式为 类名 对象名 ,如果写成 Box box2()则是错误的。
3.4.2析构函数
析构函数的作用是在系统释放对象占用的内存之前进行一些清理工作。没有返回值和参数。
#include <iostream>
using namespace std;
class Box
{public:
Box( ) //无参数的构造函数
{ length = 1; width = 1; height = 1; }
Box(float L, float W, float H) //带有3个形参的构造函数
{ length = L; width = W; height = H; }
float Volume( ){ return length*width*height; }
~Box( ) //析构函数
{ //在析构函数中增加输出,当释放对象时会输出所释放对象的相关信息
cout << "Box(" << length << ", " << width << ", " << height << ")";
cout << " is destructed!" << endl;
}
private:
float length, width, height;
};
int main( )
{
Box box1(4, 2, 3); //调用带有3个参数的构造函数创建对象
Box box2; //调用没有参数的构造函数创建对象
cout << "The volume of box1 is " << box1.Volume( ) << endl;
cout << "The volume of box2 is " << box2.Volume( ) << endl;
//system("pause");
return 0;
}
3.4.3构造函数和析构函数的调用顺序