运行环境:VS2017
一、对象的初始化
每一个对象都应当在它建立之时就有就有确定的内容,否则就会失去对象的意义。
class Time
{
int hour = 0;
int min = 0;
int sec = 0;
};
这种是错误的,类并不是一个实体,并不占储存空间,显然无处容纳数据。
如果一个类是公用的,可以在定义对象时对数据成员进行初始化。
class Time
{
public:
int hour;
int min;
int sec;
};
Time t1 = { 13,23,45 };
二、用构造函数实现对数据成员的初始化
C++提供构造函数来处理对象的初始化。构造函数是一种特殊的成员函数,与其它成员函数不同,不需要用户来调用它,而是在建立对象时自动执行。
构造函数的名字必须与类名同名,以便编译系统能识别它并把它作为构造函数处理。它不具有任何类型,不返回任何值。
#include <iostream>
using namespace std;
class Array
{
public:
Array()
{
n = 100;
for (int i = 0; i <= 99; i++)
{
a[i] = i+10;
}
}
void set_value();
void show_value();
private:
int a[100];
int n;
};
void Array::set_value()
{
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
}
void Array::show_value()
{
for (int i = 0; i < n; i++)
{
cout << a[i] << " ";
}
cout << endl;
}
int main()
{
Array arr;
arr.set_value();
arr.show_value();
Array ar1;
ar1.show_value();
return 0;
}
下面的10-109是构造函数对对象的初始化。
上面是在类内定义构造函数的,也可以只在类内对构造函数进行声明而在类外定义构造函数。
Array_sort::Array_sort()
{
n = 100;
for (int i = 0; i <= 99; i++)
{
a[i] = i + 10;
}
}
注意:
1、构造函数没有返回值,因此也没有类型。
2、构造函数不需要用户调用,也不能被用户调用。
3、可以用一个类对象初始化另一个类对象。
Time t1;
Time t2=t1;
4、如果用户自己没有定义构造函数,则C++系统会自动生成一个构造函数,只是这个构造函数的函数体是空的,也没有参数,不执行初始化操作。
三、带参数的构造函数
可以采用带参数的构造函数,在调用不同对象的构造函数时,从外面将不同的数据传递给构造函数,以实现不同的初始化。
构造函数首部的一般形式为:
构造函数名(类型1 形参1,类型2 形参2 ,......)
实参是在定义对象时给出的。
类名 对象名(实参1,实参2,......)
#include<iostream>
using namespace std;
class box
{
public :
box(int, int, int);
int volume();
private:
int height;
int width;
int length;
};
box::box(int h, int w, int l)
{
height = h;
width = w;
length = l;
}
int box::volume()
{
return (height*width*length);
}
int main()
{
box box1(12, 25, 30);
cout << "The volume of box1 is " << box1.volume()<<endl;
box box2(15, 30, 21);
cout << "The volume of box2 is " << box2.volume() << endl;
return 0;
}
四、使用参数初始化表对参数进行初始化
这种方法不再函数体内对数据成员初始化,而是在函数首部实现。
带有参数初始化表的构造函数的一般形式如下:
类名::构造函数名([参数表])[:成员初始化表]
{
[构造函数体]
}
方括号内为可选项(可有可无)。如果数据成员是数组,则应当在构造函数的函数体中用语句对其赋值。
box::box(int h, int w, int l) :height(h), width(w), length(l) {}
或者是在类里面:
class box
{
public :
box(int h, int w, int l) :height(h), width(w), length(l) {}
int volume();
private:
int height;
int width;
int length;
};
#include<iostream>
using namespace std;
class box
{
public :
box(int , int , int );
int volume();
private:
int height;
int width;
int length;
};
box::box(int h, int w, int l) :height(h), width(w), length(l) {}
int box::volume()
{
return (height*width*length);
}
int main()
{
box box1(12, 25, 30);
cout << "The volume of box1 is " << box1.volume()<<endl;
box box2(15, 30, 21);
cout << "The volume of box2 is " << box2.volume() << endl;
return 0;
}
五、构造函数的重载
在一个类中可以定义多个构造函数,以便为对象提供不同的初始化方法。这些构造函数有相同的名字,但是参数的个数和类型不同。这成为构造函数的重载。
在建立对象时不必给出实参的构造函数,成为默认构造函数。显然,无参构造函数属于默认构造函数。
改变在:6-7行,14-19行
#include<iostream>
using namespace std;
class box
{
public :
box();
box(int h, int w, int l) :height(h), width(w), length(l) {}
int volume();
private:
int height;
int width;
int length;
};
box::box()
{
height = 10;
width = 10;
length = 10;
}
int box::volume()
{
return (height*width*length);
}
int main()
{
box box1;
cout << "The volume of box1 is " << box1.volume()<<endl;
box box2(15, 30, 21);
cout << "The volume of box2 is " << box2.volume() << endl;
return 0;
}
六、使用默认参数的构造函数
构造函数中的参数的值既可以通过实参传递,也可以指定为默认的值,即如果用户不指定实参值,编译系统就使用形参去默认值。
#include<iostream>
using namespace std;
class box
{
public :
box(int h=10, int w=10, int l=10);
int volume();
private:
int height;
int width;
int length;
};
box::box(int h,int w,int l)
{
height = h;
width = w;
length = l;
}
int box::volume()
{
return (height*width*length);
}
int main()
{
box box1;
cout << "The volume of box1 is " << box1.volume()<<endl;
box box2(15);
cout << "The volume of box2 is " << box2.volume() << endl;
box box3(15,30);
cout << "The volume of box3 is " << box3.volume() << endl;
box box4(15,30,20);
cout << "The volume of box4 is " << box4.volume() << endl;
return 0;
}
说明:
1、在第6行的声明构造函数时,形参名可以省略
box(int =10, int =10, int =10);
2、一个类只能有一个默认构造函数,也就是说,可以不用参数而调用构造函数,一个类只能有一个。
box();
box(int =10, int =10, int =10);
//“box::box”: 对重载函数的调用不明确
3、在一个类中定义了全部是默认参数的构造函数,不能再定义重载的构造函数。