1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
声明数据结构:
struct
Time{
int
hour;
int
minute;
int
second;
void
set_time(
int
h,
int
m,
int
s);
void
tick();
void
show();
void
run();
};
定义函数:
void
Time::set_time(
int
h,
int
m,
int
s){}
void
Time::tick(){}
void
Time::show(){}
void
Time::run(){}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
声明数据结构:
class
Time{
int
hour;
int
minute;
int
second;
void
set_time(
int
h,
int
m,
int
s);
void
tick();
void
show();
void
run();
};
定义函数:
void
Time::set_time(
int
h,
int
m,
int
s){}
void
Time::tick(){}
void
Time::show(){}
void
Time::run(){}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
声明数据结构:
class
Time{
private
:
int
hour;
int
minute;
int
second;
public
:
void
set_time(
int
h,
int
m,
int
s);
void
tick();
void
show();
void
run();
};
定义函数:
void
Time::set_time(
int
h,
int
m,
int
s){}
void
Time::tick(){}
void
Time::show(){}
void
Time::run(){}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
class
A{
int
n;
double
d;
public
: A():n(0),d(0.0){
// constructor_init1
}
A(
int
n){
// constructor_init2
this
->n = n;
d = 0.0;
}
void
show(){
cout <<
"n = "
<< n <<
", d = "
<< d << endl;
}
~A(){
cout << “~A()” << endl;
}
};
int
main(
int
ac,
char
*av[])
{
A a1;
// use constructor_init1
a1.show();
A a2(100);
// use constructor_init2
(*
this
).show();
return
0;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
class
A{
int
n;
public
:
A(
int
n) : n(n){
cout <<
"A("
<< n <<
')'
<< endl;
}
~A(){
cout <<
"~A("
<< n <<
')'
<< endl;
}
};
void
func(){
A a2(2);
static
A a3(3);
}
int
main(
int
ac,
char
*av[])
{
A a4(4);
cout <<
"first call func : \n"
;
func();
cout <<
"second call func : \n"
;
func();
return
0;
}
A a1(1);
//==========================
// 1:全局对象
// 2:函数func中的自动对象
// 3:函数func中的静态全局对象
//4:函数main中的自动对象
//===============================
结果显示:
[root@anna-laptop construtor]# ./call_queue
A(1)
A(4)
first call func :
A(2)
A(3)
~A(2)
second call func :
A(2)
~A(2)
~A(4)
~A(3)
~A(1)
|