Lesson of 22 February 2011¶
Example of ``struct`` with methods
File example1.cc
1// to compile on unix/OSX use
2// g++ example1.cc (for generation of testDecoration.o)
3// ./a.out (to run)
4//
5
6#include <iostream>
7#include <algorithm>
8
9/*
10 A stupid example of variable size vector
11 */
12
13typedef struct variableVector {
14 int size ;
15 double * values ;
16
17 // define a constructor
18 variableVector() {
19 std::cout << "create a variableVector\n" ;
20 size = 0 ;
21 values = NULL ;
22 }
23
24 // define a constructor
25 variableVector( int sz ) {
26 std::cout << "create a variableVector of size " << sz << "\n" ;
27 size = sz ;
28 values = new double[sz] ;
29 }
30
31 // define a destructor
32 ~variableVector() {
33 std::cout << "destroy a variableVector\n" ;
34 if ( values != NULL ) delete [] values ;
35 }
36
37 // ri-define default assignment operator
38 variableVector const &
39 operator = ( variableVector const & a ) {
40 std::cout << "use variableVector const & operator =\n" ;
41 size = a . size ;
42 if ( values != NULL ) delete [] values ;
43 values = new double[ size ] ;
44 for ( int i = 0 ; i < size ; ++i ) values[i] = a . values[i] ;
45 }
46
47 // declaration of a method which will add a to the struct
48 void addTo( struct variableVector const & a ) ;
49
50} variableVector ;
51
52// instance of addTo struct variableVector b, a ; b . addTo( a ) ;
53void
54variableVector::addTo( variableVector const & a ) {
55 int minSize = std::min( size, a . size ) ;
56 for ( int i = 0 ; i < minSize ; ++i ) values[i] += a . values[i] ;
57}
58
59// prototype (declaration) of a C like function to add vector a to vector b
60void
61addTo( variableVector const & a,
62 variableVector & res ) {
63 int minSize = std::min( res . size, a . size ) ;
64 for ( int i = 0 ; i < minSize ; ++i ) res . values[i] += a . values[i] ;
65}
66
67// prototype (declaration) of a C like
68void
69allocate( variableVector & res, int size ) {
70 res . size = size ;
71 if ( res . values != NULL ) delete [] res . values ;
72 res . values = new double[ size ] ;
73}
74
75// allocate resource for res
76void
77deallocate( variableVector & res ) {
78 res . size = 0;
79 delete [] res . values ;
80 res . values = NULL ;
81}
82
83// initialize vector res
84void
85initialize( variableVector & res ) {
86 res . size = 0;
87 res . values = NULL ;
88}
89
90// perform deep copy of a to res
91void
92deepCopy( variableVector const & a, variableVector & res ) {
93 allocate( res, a.size ) ;
94 for ( int i = 0 ; i < a.size ; ++i ) res . values[i] = a . values[i] ;
95}
96
97void
98copy( variableVector const & a, variableVector & res ) {
99 res . size = a . size ;
100 res . values = a . values ; // loss pointer res . values
101}
102
103int
104main() {
105 variableVector a, b(100), c(120) ;
106 initialize(a) ;
107
108 allocate( a, 100 ) ;
109
110 //copy( a, b ) ;
111 std::cout << "passa\n" ;
112 a = b ;
113 std::cout << "passa\n" ;
114
115 a . size = 0 ; // corrupt the "class" a
116
117 return 0 ;
118}
A simple class
File example2.cc
1// to compile on unix/OSX use
2// g++ example2.cc (for generation of testDecoration.o)
3// ./a.out (to run)
4//
5
6#include <iostream>
7#include <algorithm>
8
9/*
10 A stupid example of variable size vector
11 */
12
13class variableVector {
14 // private part of the class
15 int size ;
16 double * values ;
17
18 void allocate( int sz ) {
19 if ( values != NULL ) delete [] values ;
20 values = new double[ sz ] ;
21 }
22
23 void deallocate() {
24 if ( values != NULL ) delete [] values ;
25 values = NULL ;
26 }
27
28public:
29
30 // define a constructor
31 variableVector() {
32 std::cout << "create a variableVector\n" ;
33 size = 0 ;
34 values = NULL ;
35 }
36
37 // define a constructor
38 variableVector( int sz ) {
39 std::cout << "create a variableVector of size " << sz << "\n" ;
40 allocate(sz) ;
41 }
42
43 int getSize() const { return size ; }
44
45 // destructor
46 ~variableVector() {
47 std::cout << "destroy a variableVector\n" ;
48 deallocate() ;
49 }
50
51 variableVector const &
52 operator = ( variableVector const & a ) {
53 std::cout << "use variableVector const & operator =\n" ;
54 allocate( a.size ) ;
55 for ( int i = 0 ; i < size ; ++i ) values[i] = a . values[i] ;
56 }
57
58 // declaration of a method which will add a to the struct
59 void addTo( struct variableVector const & a ) ;
60
61} ;
62
63// instance of addTo struct variableVector b, a ; b . addTo( a ) ;
64void
65variableVector::addTo( variableVector const & a ) {
66 int minSize = std::min( size, a . size ) ;
67 for ( int i = 0 ; i < minSize ; ++i ) values[i] += a . values[i] ;
68}
69
70int
71main() {
72 variableVector a, b(100), c(120) ;
73
74 //copy( a, b ) ;
75 a = b ;
76 a . getSize() ;
77
78 return 0 ;
79}
A simple example of template metaprogramming
File example3.cc
1// to compile on unix/OSX use
2// g++ example3.cc (for generation of testDecoration.o)
3// ./a.out (to run)
4//
5
6
7#include <iostream>
8#include <algorithm>
9
10/*
11 Example of template metaprogram
12 */
13
14template <unsigned N>
15class Vector {
16 int values[N] ;
17} ;
18
19template <unsigned N>
20class Factorial {
21public:
22 static unsigned const RES = N*Factorial<N-1>::RES ;
23} ;
24
25template <>
26class Factorial<1> {
27public:
28 static unsigned const RES = 1 ;
29} ;
30
31int
32main() {
33 Vector<10> a, b, c ;
34 Factorial<1> f1 ;
35 Factorial<2> f2 ;
36 Factorial<6> f6 ;
37 Factorial<10> f10 ;
38
39 std::cout << f1.RES << '\n' ; // access to class variable RES
40 std::cout << f2.RES << '\n' ; // access to class variable RES
41 std::cout << f6.RES << '\n' ; // access to class variable RES
42 std::cout << f10.RES << '\n' ; // access to class variable RES
43
44 return 0 ;
45}