src
examples
constructors.cpp
Go to the documentation of this file.
1
8
/*****************************************************************************
9
** Includes
10
*****************************************************************************/
11
12
#include <iostream>
13
18
/*****************************************************************************
19
** Classes
20
*****************************************************************************/
21
22
class
A {
23
public
:
24
A(
int
i) : var(i) { std::cout <<
"Constructor"
<< std::endl; }
25
A(
const
A& a) { std::cout <<
"Copy constructor"
<< std::endl; }
26
~A() { std::cout <<
"Destructor"
<< std::endl; }
27
void
f() {};
28
private
:
29
int
var;
30
};
31
32
/*****************************************************************************
33
** Functions
34
*****************************************************************************/
35
36
A f() {
37
return
A(3);
38
}
39
40
A g() {
41
A a(4);
42
return
a;
43
}
44
45
/*****************************************************************************
46
** Main
47
*****************************************************************************/
48
49
int
main
() {
50
51
std::cout << std::endl;
52
std::cout <<
"***********************************************************"
<< std::endl;
53
std::cout <<
" Normal Constructor : A a(3)"
<< std::endl;
54
std::cout <<
"***********************************************************"
<< std::endl;
55
std::cout << std::endl;
56
57
A a1(3); a1.f();
58
59
std::cout << std::endl;
60
std::cout <<
"***********************************************************"
<< std::endl;
61
std::cout <<
" Copy Constructor : A a2(a)"
<< std::endl;
62
std::cout <<
"***********************************************************"
<< std::endl;
63
std::cout << std::endl;
64
65
A a2(a1); a2.f();
66
67
std::cout << std::endl;
68
std::cout <<
"***********************************************************"
<< std::endl;
69
std::cout <<
" Constructor Assignment w/ Function : A a3 = f();"
<< std::endl;
70
std::cout <<
"***********************************************************"
<< std::endl;
71
std::cout << std::endl;
72
73
A a3 = f(); a3.f();
74
75
std::cout << std::endl;
76
std::cout <<
"***********************************************************"
<< std::endl;
77
std::cout <<
" Constructor Assignment w/ Non Temp Function : A a4 = g();"
<< std::endl;
78
std::cout <<
"***********************************************************"
<< std::endl;
79
std::cout << std::endl;
80
81
A a4 = g(); a4.f();
82
83
std::cout << std::endl;
84
std::cout <<
"***********************************************************"
<< std::endl;
85
std::cout <<
" Program Finished"
<< std::endl;
86
std::cout <<
"***********************************************************"
<< std::endl;
87
std::cout << std::endl;
88
89
90
return
0;
91
}
main
int main(int argc, char **argv)
ecl_utilities
Author(s): Daniel Stonier
autogenerated on Wed Mar 2 2022 00:16:32