Lezione del 14/7/2006¶
-
Due classi per vettori 2D e matrici 2x2
File Makefile
1# 2# oggetto: elenco file dipendenze 3# (tab) comando o comandi da eseguire 4# 5# usando il comando 6# 7# gcc -MM file.c 8# 9# genero la linea delle dipendenze automaticamente 10# 11 12CC=gcc 13CFLAGS=-Wall -ansi -O 14 15CXX=g++ 16CXXFLAGS=-Wall -ansi -O 17##-DDEBUG 18 19F77=g77 20FFLAGS=-O3 21 22LIBS=-lstdc++ 23SRCS=prova.cc mat2.cc matvec2.cc 24OBJS=$(SRCS:.cc=.o) 25 26prova: $(OBJS) 27 $(CC) $(OBJS) -o prova $(LIBS) 28 29# regola di compilazione per file tipo C 30%.o: %.c 31 $(CC) $(CFLAGS) -c $< 32 33# regola di compilazione per file tipo C++ 34%.o: %.cc 35 $(CXX) $(CXXFLAGS) -c $< 36 37# regola di compilazione per file tipo Fortran 38%.o: %.f 39 $(F77) $(FFLAGS) -c $< 40 41clean: 42 rm $(OBJS) test 43 44depend: 45 makedepend $(SRCS) 46# DO NOT DELETE 47 48prova.o: vec2.hh mat2.hh 49mat2.o: mat2.hh 50matvec2.o: matvec2.hh
File vec2.hh
1/* 2 * Esempio di classe che implementa dei vettori con 2 componenti 3 * 4 * 5 */ 6 7#ifndef VEC2_HH /* solito trucco per evitare la doppia inclusione */ 8#define VEC2_HH 9 10#include <iostream> 11using namespace std ; 12 13#ifdef DEBUG 14 #define DEBUG_MESSAGE(A) cout << A << "\n" 15#else 16 #define DEBUG_MESSAGE(A) 17#endif 18 19 20 21/* 22 * Una classe puo' essere vista come una struttura con metodi 23 * alla quale vengono poste alcune restrizioni e altre piccole cose. 24 * 25 */ 26 27 28/* class nome_classe { definizioni e campi } ; 29 * 30 * e' simile a 31 * 32 * typedef struct { definizioni e campi } nome_classe ; 33 * 34 * ma con significative differenze 35 * 36 * 1) I campi non sono visibili all'esterno a meno che non lo 37 * specifichiamo (data hiding). 38 * 2) il typo della classe e' gia usabile all'interno 39 * (non occorre accedere con struct nome_classe come 40 * nel caso della struttura) 41 * 3) C'e' un importante puntatore (software) "this" che 42 * che punta alla istanza della classe, utile in situazioni 43 * particolari. 44 * 45 * In ogni punto della classe si possono inserire i seguenti 46 * qualificatori di visibilita': 47 * 48 * public: da qui in poi tutte le variabili e le dichiarazioni 49 * sono ACCESSIBILI all'esterno 50 * 51 * private: da qui in poi tutte le variabili e le dichiarazioni 52 * sono INACCESSIBILI all'esterno 53 * 54 * protected: (da spiegare piu' avanti) 55 * 56 * se niente e' specificato il default e' ``private:'' 57 */ 58 59class vec2 { 60 // campi (accesso privato) 61 double x ; // Prima coordinata del punto 62 double y ; // Seconda Coordinata del punto 63 64 // fino a qui la classe e' una struttura struct { double x, y ; } 65 66public: 67 68 // costruttori (metodi con il nome della classe) 69 vec2() { 70 DEBUG_MESSAGE("passa per il costruttore vec2()"); 71 x = y = 0 ; 72 } // corpo del costruttore 73 /* 74 * se in un pezzo di codice scrivo 75 * vec2 a, b, c ; 76 * dopo aver creato gli oggetti a,b,c viene chiamato 77 * questo metodo e di fatto vengono azzerate le coordinate x,y 78 * do ogni uno di questi oggetti. 79 */ 80 81 vec2( double const x, double const y ) { 82 DEBUG_MESSAGE("passa per il costruttore vec2(double const x, double const y)"); 83 this -> x = x ; this -> y = y ; 84 } 85 /* 86 * se in un pezzo di codice scrivo 87 * vec2 a(1,2), b(3,2), c(0.1,2.0) ; 88 * dopo aver creato gli oggetti a,b,c viene chiamato 89 * questo metodo e di fatto vengono inizializzate le coordinate x,y 90 * do ogni uno di questi oggetti. 91 */ 92 93 vec2( vec2 const & a ) { 94 DEBUG_MESSAGE("passa per il costruttore vec2(vec2 const & a)"); 95 this -> x = a.x ; this -> y = a.y ; 96 } 97 /* 98 * se in un pezzo di codice scrivo 99 * vec2 a(1,2) ; 100 * vec2 b(a) ; oppure vec2 b = a ; 101 * dopo aver creato l'oggetto b viene chiamato 102 * questo metodo che fa la copia dei valori delle 103 * coordinate dell'oggetto a. 104 */ 105 106 /* 107 * metodo di stampa di un vettore 108 */ 109 void 110 print(ostream & s) const // const indica che non modifico 111 // la classe 112 { 113 s << "[" << x << "," << y << "]" ; 114 } 115 // generato con il comando figlet -font mini -m 1 OPERAZIONI 116 // _ _ _ _ __ ___ _ ___ 117 // / \ |_) |_ |_) /\ / | / \ |\ | | 118 // \_/ | |_ | \ /--\ /_ _|_ \_/ | \| _|_ 119 // __ ___ _ ___ ___ 120 // (_ | | | |_) | | |\ | | | 121 // __) |_| _|_ | |_| | \| | _|_ 122 // 123 124 // assegnazione (operatore =) a = b ; 125 vec2 & 126 operator = (vec2 const & a) { 127 DEBUG_MESSAGE("passa per l'operatore vec2 = "); 128 x = a.x ; 129 y = a.y ; // assegna le coordinate di a a *this 130 return * this ; // ritorna un riferimento a se stesso 131 } 132 /* 133 // Perche' ritorno * this ? 134 // l'operatore = e' associativo a destra, quindi se scrivo 135 // a = b = c ====> a = (b=c) 136 // che e' come scrivere 137 // a . operator = ( b . operator(c) ) ; 138 // poiche operator = ritorna se stesso e' come fare 139 // b . operator = (c) ; 140 // a . operator = (b) ; 141 */ 142 143 // operatore somma + 144 // vec2 operator + ( vec2 const & rhs) const 145 // { return vec2(a.x + b.x, a.y + b.y) ; } 146 147 // dichiaro che questo operatore ha accesso agli elementi 148 // privati di vec2 149 friend vec2 operator + ( vec2 const & a, vec2 const & b ) ; 150 friend vec2 operator - ( vec2 const & a, vec2 const & b ) ; 151 friend vec2 operator * ( vec2 const & a, vec2 const & b ) ; 152 friend vec2 operator / ( vec2 const & a, vec2 const & b ) ; 153 friend double operator ^ ( vec2 const & a, vec2 const & b ) ; 154 155 // metodi vari 156 void 157 set( double const x, double const y) 158 { this -> x = x ; this -> y = y ; } 159 160 // metodi vari 161 double const & 162 operator () ( int const i ) const { 163 switch (i) { 164 case 1: return x ; 165 case 2: return y ; 166 default: 167 cerr << "operator vec2(" << i << ") indice fuori range!\n" ; 168 exit(1) ; 169 } 170 } 171 172 double & 173 ref( int const i ) { 174 switch (i) { 175 case 1: return x ; 176 case 2: return y ; 177 default: 178 cerr << "vec2::ref(" << i << ") indice fuori range!\n" ; 179 exit(1) ; 180 } 181 } 182 183} ; // non dimenticarsi il ; finale 184 185inline 186ostream & 187operator << (ostream & s, vec2 const & a) { 188 a . print(s) ; 189 return s ; 190} 191 192// operatore somma + 193inline 194vec2 195operator + ( vec2 const & a, vec2 const & b ) { 196 DEBUG_MESSAGE("passa per l'operatore vec2 + "); 197 vec2 tmp ; 198 tmp.x = a.x + b.x ; 199 tmp.y = a.y + b.y ; 200 return tmp ; 201} 202 203// operatore somma - 204inline 205vec2 206operator - ( vec2 const & a, vec2 const & b ) { 207 DEBUG_MESSAGE("passa per l'operatore vec2 - "); 208 return vec2(a.x - b.x, a.y - b.y) ; 209} 210 211// operatore somma * 212inline 213vec2 214operator * ( vec2 const & a, vec2 const & b ) { 215 DEBUG_MESSAGE("passa per l'operatore vec2 * "); 216 return vec2(a.x * b.x, a.y * b.y) ; 217} 218 219// operatore somma / 220inline 221vec2 222operator / ( vec2 const & a, vec2 const & b ) { 223 DEBUG_MESSAGE("passa per l'operatore vec2 / "); 224 return vec2(a.x / b.x, a.y / b.y) ; 225} 226 227// operatore somma / 228inline 229double 230operator ^ ( vec2 const & a, vec2 const & b ) { 231 DEBUG_MESSAGE("passa per l'operatore vec2 ^ "); 232 return a.x * b.y - a.y * b.x ; 233} 234 235#endif
File mat2.hh
1/* 2 * Esempio di classe che implementa matrici 2x2 3 * 4 */ 5 6#ifndef MAT2_HH /* solito trucco per evitare la doppia inclusione */ 7#define MAT2_HH 8 9#include <iostream> 10using namespace std ; 11 12#ifdef DEBUG 13 #define DEBUG_MESSAGE(A) cout << A << "\n" 14#else 15 #define DEBUG_MESSAGE(A) 16#endif 17 18class mat2 { 19 // campi (accesso privato) 20 double M[4] ; // Matrice 2x2 21 22public: 23 24 // costruttori (metodi con il nome della classe) 25 mat2(); 26 mat2( double const s ) ; 27 mat2( mat2 const & a ) ; 28 29 // accesso agli elementi di A 30 // restituisce il riferimento all'elemento i,j 31 double & 32 ref( int const i, int const j ) { 33 #ifdef DEBUG 34 if ( i < 1 || i > 2 || j < 1 || j > 2 ) { 35 cerr << "in mat2 ref(" << i << "," << j << ")" 36 << "\nindice fuori range!\n" ; 37 exit(1) ; 38 } 39 #endif 40 return M[(i-1)+2*(j-1)] ; // indirizzamento alla FORTRAN 41 } 42 43 // accesso agli elementi di A 44 // restituisce il riferimento all'elemento i,j 45 double const & 46 operator () ( int const i, int const j ) const { 47 #ifdef DEBUG 48 if ( i < 1 || i > 2 || j < 1 || j > 2 ) { 49 cerr << "in mat2(" << i << "," << j << ")" 50 << "\nindice fuori range!\n" ; 51 exit(1) ; 52 } 53 #endif 54 return M[(i-1)+2*(j-1)] ; // indirizzamento alla FORTRAN 55 } 56 57 // accesso agli elementi di A visto come vettore 58 double const & 59 operator [] ( int const i ) const { // questo operatore puo' avere 1 solo argomento 60 #ifdef DEBUG 61 if ( i < 0 || i > 3 ) { 62 cerr << "in mat2 operator [] con argomento [" << i << "]" 63 << "\nindice fuori range!\n" ; 64 exit(1) ; 65 } 66 #endif 67 return M[i] ; // indirizzamento alla FORTRAN 68 } 69 70 // questo operatore funziona in questo modo: 71 // mat2 a ; 72 // a(i,j) e' equivalente a scrivere 73 // a . operator () (i,j) 74 // che (a parte il fattio che M e' inaccessibile) 75 // in questo caso equivale a . M[(i-1)+2*(j-1)] 76 77 /* 78 * metodo di stampa di un vettore 79 */ 80 void 81 print(ostream & s) const // const indica che non modifico 82 // la classe 83 { 84 s << "[" << (*this)(1,1) << "," << (*this)(1,2) 85 << ";" << (*this)(2,1) << "," << (*this)(2,2) << "]" ; 86 } 87 // generato con il comando figlet -font mini -m 1 OPERAZIONI 88 // _ _ _ _ __ ___ _ ___ 89 // / \ |_) |_ |_) /\ / | / \ |\ | | 90 // \_/ | |_ | \ /--\ /_ _|_ \_/ | \| _|_ 91 // __ ___ _ ___ ___ 92 // (_ | | | |_) | | |\ | | | 93 // __) |_| _|_ | |_| | \| | _|_ 94 // 95 96 // assegnazione (operatore =) a = b ; 97 mat2 & 98 operator = (mat2 const & a) { 99 DEBUG_MESSAGE("passa per l'operatore mat2 = "); 100 memcpy( this -> M, a . M, 4*sizeof(double) ) ; 101 return * this ; // ritorna un riferimento a se stesso 102 } 103 104 // dichiaro che questo operatore ha accesso agli elementi 105 // privati di vec2 106 friend mat2 operator + ( mat2 const & a, mat2 const & b ) ; 107 friend mat2 operator - ( mat2 const & a, mat2 const & b ) ; 108 friend mat2 operator * ( mat2 const & a, mat2 const & b ) ; 109 110 // metodi vari 111 double det() const 112 { return (*this)(1,1)*(*this)(2,2) - (*this)(1,2)*(*this)(2,1) ; } 113 114} ; // non dimenticarsi il ; finale 115 116// costruttori (metodi con il nome della classe) 117inline 118mat2::mat2() { 119 DEBUG_MESSAGE("passa per il costruttore mat2()"); 120 M[0] = M[1] = M[2] = M[3] = 0 ; 121} // corpo del costruttore 122 123inline 124mat2::mat2( double const s ) { 125 DEBUG_MESSAGE("passa per il costruttore mat2(double const s)"); 126 ref(1,1) = ref(2,2) = s ; 127 ref(1,2) = ref(2,1) = 0 ; 128} 129 130inline 131mat2::mat2( mat2 const & a ) { 132 DEBUG_MESSAGE("passa per il costruttore mat2(mat2 const & a)"); 133 // uso la funzione per copiare pezzi di memoria della libreria C 134 // memcpy( destinazione, sorgente, quanti byte ) 135 memcpy( this -> M, a . M, 4*sizeof(double) ) ; 136} 137 138inline 139ostream & 140operator << (ostream & s, mat2 const & m) { 141 m . print(s) ; 142 return s ; 143} 144 145// operatore somma + 146mat2 operator + ( mat2 const & a, mat2 const & b ) ; 147 148// operatore somma - 149mat2 operator - ( mat2 const & a, mat2 const & b ) ; 150 151// operatore somma * 152mat2 operator * ( mat2 const & a, mat2 const & b ) ; 153 154#endif
File mat2.cc
1/* 2 * Esempio di classe che implementa matrici 2x2 3 * 4 */ 5 6#include "mat2.hh" 7 8// operatore somma + 9mat2 10operator + ( mat2 const & a, mat2 const & b ) { 11 DEBUG_MESSAGE("passa per l'operatore mat2 + "); 12 mat2 tmp ; 13 for ( int i = 1 ; i <= 2 ; ++i ) 14 for ( int j = 1 ; j <= 2 ; ++j ) 15 tmp.ref(i,j) = a(i,j) + b(i,j) ; 16 return tmp ; 17} 18 19// operatore somma - 20mat2 21operator - ( mat2 const & a, mat2 const & b ) { 22 DEBUG_MESSAGE("passa per l'operatore mat2 - "); 23 mat2 tmp ; 24 for ( int i = 0 ; i < 4 ; ++i ) // usando l'operatore [] 25 tmp.M[i] = a[i] + b[i] ; // ho un ciclo piu' efficiente 26 return tmp ; 27} 28 29// operatore somma * 30mat2 31operator * ( mat2 const & a, mat2 const & b ) { 32 DEBUG_MESSAGE("passa per l'operatore + "); 33 mat2 tmp ; 34 tmp.ref(1,1) = a(1,1)*b(1,1) + a(1,2) * b(2,1) ; 35 tmp.ref(1,2) = a(1,1)*b(1,2) + a(1,2) * b(2,2) ; 36 tmp.ref(2,1) = a(2,1)*b(1,1) + a(2,2) * b(2,1) ; 37 tmp.ref(2,2) = a(2,1)*b(1,2) + a(2,2) * b(2,2) ; 38 return tmp ; 39}
File matvec2.hh
1/* 2 * Qui raccogliamo le operazioni miste matrice vettore 3 */ 4 5#ifndef MATVEC2_HH /* solito trucco per evitare la doppia inclusione */ 6#define MATVEC2_HH 7 8#include "vec2.hh" 9#include "mat2.hh" 10 11vec2 operator * ( mat2 const & A, vec2 const & b ) ; // restituisce A*b 12vec2 solve( mat2 const & A, vec2 const & b ) ; // restituisce x = A^-1 b 13 14#endif
File matvec2.cc
1/* 2 * Qui raccogliamo le operazioni miste matrice vettore 3 */ 4 5#include "matvec2.hh" 6 7vec2 8operator * ( mat2 const & A, vec2 const & b ) { 9 return vec2( A(1,1) * b(1) + A(1,2) * b(2), 10 A(2,1) * b(1) + A(2,2) * b(2) ) ; 11} 12 13vec2 14solve( mat2 const & A, vec2 const & b ) { 15 double det = A . det() ; 16 return vec2( (b(1)*A(2,2) - b(2)*A(1,2)) / det, 17 (A(1,1)*b(2) - A(2,1)*b(1)) / det ) ; 18}
File prova.cc
1#include "vec2.hh" 2#include "mat2.hh" 3#include "matvec2.hh" 4 5int 6main() { 7 vec2 a ; // richiama il primo costruttore 8 vec2 b(1,2) ; // richiama il secondo costruttore 9 vec2 c(b) ; // richiama il terzo costruttore 10 11 cout << "TEST COSTRUTTORI" 12 << "\na = " << a 13 << "\nb = " << b 14 << "\nc = " << c 15 << '\n' ; 16 17 a . set(3,4) ; 18 // test delle varie operazioni 19 20 cout << "TEST +\n" 21 << a << " + " << b << " = " << a+b << '\n' ; 22 23 cout << "TEST -\n" 24 << a << " - " << b << " = " << a-b << '\n' ; 25 26 cout << "TEST *\n" 27 << a << " * " << b << " = " << a*b << '\n' ; 28 29 cout << "TEST /\n" 30 << a << " / " << b << " = " << a/b << '\n' ; 31 32 cout << "TEST ^\n" 33 << a << " ^ " << b << " = " << (a^b) << '\n' ; 34 35 // assegnazione 36 a = b = c ; 37 cout << "TEST ASSEGNAZIONE" 38 << "\na = " << a 39 << "\nb = " << b 40 << "\nc = " << c 41 << '\n' ; 42 43 44 mat2 A ; // richiama il primo costruttore 45 mat2 B(1) ; // richiama il secondo costruttore 46 mat2 C(B) ; // richiama il terzo costruttore 47 48 cout << "TEST COSTRUTTORI" 49 << "\nA = " << A 50 << "\nB = " << B 51 << "\nC = " << C 52 << '\n' ; 53 54 A.ref(1,2) = 2 ; 55 A.ref(2,2) = 3 ; 56 B.ref(1,2) = 1.2 ; 57 B.ref(2,1) = -3 ; 58 // test delle varie operazioni 59 60 cout << "TEST +\n" 61 << A << " + " << B << " = " << A+B << '\n' ; 62 63 cout << "TEST -\n" 64 << A << " - " << B << " = " << A-B << '\n' ; 65 66 cout << "TEST *\n" 67 << A << " * " << B << " = " << A*B << '\n' ; 68 69 // test sistema 70 a = B*b ; 71 cout << "TEST ASSEGNAZIONE" 72 << "SISTEMA " << B << " * x = " << a 73 << "\nx = " << solve(B,a) 74 << "\nmi aspetto = " << b 75 << '\n' ; 76 77 // assegnazione 78 A = B = C ; 79 cout << "TEST ASSEGNAZIONE" 80 << "\na = " << A 81 << "\nb = " << B 82 << "\nc = " << C 83 << '\n' ; 84 85 86}
-
Classi e template alcuni esempi
File ex026.cc
1/* 2 * Classi + Template 3 * 4 * 5 */ 6 7class vec2 { 8 double x, y ; 9 10 /* 11 corpo e definizioni varie 12 */ 13}; 14 15 16/* 17 * Problema: 18 * e se invecie che double voglio float ? o long double ? 19 * 20 * Soluzione: 21 * si possono parametrizzare le classi con l'uso dei template 22 */ 23 24template <typename TYPE> 25class vector2 { 26 TYPE x,y ; 27 /* 28 corpo e definizioni varie 29 */ 30} ; 31 32/* 33 * Per usare una particolare classe devo 34 * specificare il tipo 35 * 36 */ 37 38vector2<float> a ; 39vector2<double> b ; 40 41/* 42 * e se volessi una classe di vettori di lunghezza fissa, 43 * ma non fissata a priori ? 44 */ 45 46template <typename TYPE, int SIZE> 47class vector { 48 TYPE V[SIZE] ; 49 /* 50 corpo e definizioni varie 51 */ 52} ; 53 54vector<float,2> aa ; 55vector<double,6> bb ;
File ex027.cc
1/* 2 * Classi + Template 3 */ 4 5// esempio classe di vettori parametrizzata con tipo e dimensione 6 7#include <iostream> 8 9using namespace std ; 10 11template <typename TYPE, int SIZE> 12class vector { 13 TYPE V[SIZE] ; 14public: 15 vector() ; 16 vector(vector<TYPE,SIZE> const & v) ; 17 18 TYPE const & operator () (int i) const ; 19 TYPE & ref(int i) ; 20} ; 21 22template <typename TYPE, int SIZE> 23vector<TYPE,SIZE>::vector() { 24 for ( int i = 0 ; i < SIZE ; ++i ) V[i] = 0 ; 25} 26 27template <typename TYPE, int SIZE> 28vector<TYPE,SIZE>::vector( vector<TYPE,SIZE> const & v ) { 29 for ( int i = 0 ; i < SIZE ; ++i ) V[i] = v.V[i] ; 30} 31 32template <typename TYPE, int SIZE> 33TYPE const & 34vector<TYPE,SIZE>::operator () ( int i ) const 35{ return V[i-1] ; } // Accesso tipo FORTRAN all'elemento i-esimo 36 37template <typename TYPE, int SIZE> 38TYPE & 39vector<TYPE,SIZE>::ref( int i ) 40{ return V[i-1] ; } // Accesso tipo FORTRAN all'elemento i-esimo 41 42template <typename TYPE, int SIZE> 43vector<TYPE,SIZE> 44operator + ( vector<TYPE,SIZE> const & a, vector<TYPE,SIZE> const & b) { 45 vector<TYPE,SIZE> tmp ; 46 for ( int i = 1 ; i <= SIZE ; ++i ) tmp.ref(i) = a(i)+b(i) ; 47 return tmp ; 48} 49 50template <typename TYPE, int SIZE> 51vector<TYPE,SIZE> 52operator - ( vector<TYPE,SIZE> const & a, vector<TYPE,SIZE> const & b) { 53 vector<TYPE,SIZE> tmp ; 54 for ( int i = 1 ; i <= SIZE ; ++i ) tmp.ref(i) = a(i)-b(i) ; 55 return tmp ; 56} 57 58template <typename TYPE, int SIZE> 59vector<TYPE,SIZE> 60operator * ( vector<TYPE,SIZE> const & a, vector<TYPE,SIZE> const & b) { 61 vector<TYPE,SIZE> tmp ; 62 for ( int i = 1 ; i <= SIZE ; ++i ) tmp.ref(i) = a(i)*b(i) ; 63 return tmp ; 64} 65 66template <typename TYPE, int SIZE> 67vector<TYPE,SIZE> 68operator / ( vector<TYPE,SIZE> const & a, vector<TYPE,SIZE> const & b) { 69 vector<TYPE,SIZE> tmp ; 70 for ( int i = 1 ; i <= SIZE ; ++i ) tmp.ref(i) = a(i)/b(i) ; 71 return tmp ; 72} 73 74template <typename TYPE, int SIZE> 75ostream & 76operator << ( ostream & s, vector<TYPE,SIZE> const & a ) { 77 s << "[ " ; 78 for ( int i = 1 ; i < SIZE ; ++i ) s << a(i) << "," ; 79 s << a(SIZE) << "]" ; 80 return s ; 81} 82 83 84int 85main() { 86 vector<double,5> a, b, c ; 87 88 a.ref(1) = 1 ; 89 a.ref(2) = 2 ; 90 a.ref(3) = -2 ; 91 a.ref(4) = 3 ; 92 a.ref(5) = 2 ; 93 94 b.ref(1) = 1 ; 95 b.ref(2) = 22 ; 96 b.ref(3) = -2 ; 97 b.ref(4) = 3 ; 98 b.ref(5) = 4 ; 99 100 c.ref(1) = 2; 101 c.ref(2) = 4 ; 102 c.ref(3) = -2 ; 103 c.ref(4) = 3 ; 104 c.ref(5) = -1 ; 105 106 vector<double,5> d(a+b-c/a) ; 107 108 cout << "\na = " << a 109 << "\nb = " << b 110 << "\nc = " << c 111 << "\nd = " << d 112 << '\n' ; 113}