对象数组
【Example 1】
#include <iostream>
using namespace std;
class exam
{
private:
int x;
public:
void set_x(int n) { x = n; }
int get_x() { return x; }
};
int main()
{
exam ob[4];
int i;
for (i = 0; i < 4;i++)
{
cout << ob[i].get_x() << endl;
}
return 0;
}
/*
output:
6422248
1979477805
4200944
6422400
*/
默认构造函数,随机赋值
【Example 2】
#include <iostream>
using namespace std;
class exam
{
private:
int x;
public:
exam(){}
void set_x(int n) { x = n; }
int get_x() { return x; }
};
int main()
{
exam ob[4];
int i;
for (i = 0; i < 4;i++)
{
cout << ob[i].get_x() << endl;
}
return 0;
}
/*
6422476
1979501760
688181507
-2
*/
优先调用显式定义的无参数构造函数
【Example 3】
// Definition 1
class exam
{
private:
int x;
public:
//exam(){}
exam(int n) { x = n; }
// 定义对象数组必须赋初值
// exam ob[4]; 会提示错误,不存在默认构造函数
void set_x(int n) { x = n; }
int get_x() { return x; }
};
// Definition 2
class exam
{
private:
int x;
public:
exam(){ x = 123; }
exam(int n) { x = n; }
// exam ob[4]; 可以
void set_x(int n) { x = n; }
int get_x() { return x; }
};
/*
output:
123
123
123
123
*/
当定义了含参的构造函数后,隐式的默认构造函数不会自动生成
如果用户定义的类中没有显式的定义任何构造函数,编译器就会自动为该类型生成默认构造函数,称为合成的构造函数(synthesized default constructor)
如果类包含内置或复合类型的成员,则该类就不应该依赖于合成的默认构造函数,它应该定义自己的构造函数来初始化这些成员。
多数情况下,编译器为类生成一个公有的默认构造函数,只有下面两种情况例外:
1.一个类显式地声明了任何构造函数,编译器不生成公有的默认构造函数。在这种情况下,如果程序需要一个默认构造函数,需要由类的设计者提供。
2.一个类声明了一个非公有的默认构造函数,编译器不会生成公有的默认构造函数。
C++编译器必须为未声明构造函数之class合成一个默认构造函数
- 第一,class 有member object(该member object有default constructor), class需要default constructor调用member object default constructor
- 第二,如果一个没有任何constructor的class 派生自一个"带有default constructor"的base class.那么它的default constructor会被合成出来。
- 第三,如果一个class申明了virtual function,,如果该类没有任何constructor,
编译器也会为它加default constructor.
(如果用户有constructor,编译器会在他的constructor中添加一些code,用来初始化vptr) - 第四,一个class(没有申明任何constructor)派生自一个继承串链,其中有一个或多个virtual base classes,编译器也会合成出一个default constructor,在其中放入每一个virtual base class的执行期存取操作的的码,(如果用户申明了constructors,编译器会在constructor中安插virtual base class执行期存取操作的的码)
上面四种分析合成出的default constructor都是nontrivial default constructors,
不在此情况之内的都trivial default constructors,它们实际上并不会被编译器合成出来。
在合成的default constructors,只有base class subobjects和member class object会被初始化,所有其它的nonstatic data member,如整数、整数指针、整数数组等等都不会被编译器初始化。
【Example 4】
#include <iostream>
using namespace std;
class exam
{
private:
int x;
public:
exam() { x = 123; }
exam(int n) { x = n; }
void set_x(int n) { x = n; }
int get_x() { return x; }
};
int main()
{
exam ob1[4];
exam ob2[4] = {1, 2, 3, 4};
exam ob3[4] = {5, 6};
exam ob4[4] = {exam(11), exam(22), exam(33), exam(44)};
int i;
cout << "ob1\tob2\tob3\tob4" << endl;
for (i = 0; i < 4;i++)
{
cout << ob1[i].get_x() << "\t"
<< ob2[i].get_x() << "\t"
<< ob3[i].get_x() << "\t"
<< ob4[i].get_x() << endl;
}
return 0;
}
/*
output:
ob1 ob2 ob3 ob4
123 1 5 11
123 2 6 22
123 3 123 33
123 4 123 44
*/
【Example 5】
二维对象数组的初始化
#include <iostream>
using namespace std;
class example
{
private:
int x;
int y;
public:
example() { x = 0, y = 0; }
example(int n, int m) {
x = n;
y = m;
}
~example(){
cout << "Destructor called" << endl;
}
int get_x() { return x; }
int get_y() { return y; }
};
int main()
{
example op1[3][2] = {};
example op2[3][2] = {
example(1, 2), example(3, 4),
example(4, 5), example(6, 7),
example(8, 9), example(10, 11)
};
example op3[3][2] = {
example(11, 22), example(33, 44)};
example op4[3][2];
cout << "op1\top2\top3\top4" << endl;
for (int i = 0; i < 3; i++)
{
cout << op1[i][0].get_x() << "," << op1[i][0].get_y() << " " << op1[i][1].get_x() << "," << op1[i][1].get_y()<< " "
<< op2[i][0].get_x() << "," << op2[i][0].get_y() << " " << op2[i][1].get_x() << "," << op2[i][1].get_y()<< " "
<< op3[i][0].get_x() << "," << op3[i][0].get_y() << " " << op3[i][1].get_x() << "," << op3[i][1].get_y()<< " "
<< op4[i][0].get_x() << "," << op4[i][0].get_y() << " " << op4[i][1].get_x() << "," << op4[i][1].get_y() << endl;
}
return 0;
}
/*
output:
op1 op2 op3 op4
0,0 0,0 1,2 3,4 11,22 33,44 0,0 0,0
0,0 0,0 4,5 6,7 0,0 0,0 0,0 0,0
0,0 0,0 8,9 10,11 0,0 0,0 0,0 0,0
Destructor called
Destructor called
Destructor called
Destructor called
Destructor called
Destructor called
Destructor called
Destructor called
Destructor called
Destructor called
Destructor called
Destructor called
Destructor called
Destructor called
Destructor called
Destructor called
Destructor called
Destructor called
Destructor called
Destructor called
Destructor called
Destructor called
Destructor called
Destructor called
*/