Lesson of 23 February 2011¶
Example of use of the reference in C++
File example1.cc
1/*
2 Stupid example with reference
3*/
4
5#include <iostream>
6#include <iomanip>
7
8using namespace std ;
9
10int
11main() {
12 int aVeryLongAndStupidName = 1 ;
13 int & b = aVeryLongAndStupidName ;
14 int * pa = &aVeryLongAndStupidName ;
15 int * pb = &b ;
16
17 cout << "\npa = " << pa
18 << "\npb = " << pb
19 << "\n&b = " << &b
20 << "\n&aVeryLongAndStupidName = " << &aVeryLongAndStupidName
21 << '\n' ;
22
23 cout << " aVeryLongAndStupidName = " << aVeryLongAndStupidName
24 << " b = " << b
25 << '\n' ;
26
27 ++aVeryLongAndStupidName ;
28
29 cout << " aVeryLongAndStupidName = " << aVeryLongAndStupidName
30 << " b = " << b
31 << '\n' ;
32
33 ++b ;
34
35 cout << " aVeryLongAndStupidName = " << aVeryLongAndStupidName
36 << " b = " << b
37 << '\n' ;
38
39 long c = (long) &b ;
40
41 cout << "&b = " << &b
42 << " c = " << hex << c
43 << '\n' ;
44
45 cout << sizeof(int*) << '\n' ;
46 return 0 ;
47}
Example of use of pointers in C++
File example2.cc
1/*
2 Stupid example with pointer
3*/
4
5#include <iostream>
6#include <iomanip>
7
8using namespace std ;
9
10int
11main() {
12
13 int aStupidVector[] = { -123,1,2,3,4,5,6,7,8,9 } ;
14 int aStupidVectorSize = sizeof(aStupidVector)/sizeof(aStupidVector[0]) ;
15
16 int * pa = aStupidVector ;
17
18 cout << "*aStupidVector = " << *aStupidVector << '\n' ;
19 cout << "*pa = " << *pa << '\n' ;
20 ++pa ;
21 cout << "*pa = " << *pa << '\n' ;
22 pa += 6 ; // pa = pa + 6
23 cout << "*pa = " << *pa << '\n' ;
24 pa -= 2 ; // pa = pa - 2
25 cout << "*pa = " << *pa << '\n' ;
26
27 int * pb = aStupidVector + 1 ;
28 unsigned long diff = pb - aStupidVector ;
29 cout << "diff = " << diff << '\n' ;
30
31 unsigned long diff1 = (long) pb - (long) aStupidVector ;
32 cout << "diff1 = " << diff1 << '\n' ;
33
34 pa = aStupidVector ;
35 pb = aStupidVector + aStupidVectorSize ;
36 for ( int * pp = pa ; pp < pb ; ++pp )
37 cout << "vec[ " << pp-pa << "] = " << *pp << '\n' ;
38
39 for ( int i = 0 ; i < aStupidVectorSize ; ++i )
40 cout << "vec[ " << i << "] = " << aStupidVector[i] << '\n' ;
41
42 int * pp = pa ;
43 while ( pp < pb ) cout << "vec[ " << pp-pa << "] = " << *pp++ << '\n' ;
44
45 return 0 ;
46}
A simple class implementing polynomial algebra (incomplete)
File poly.hh
1/*
2 An example of class implementing integer polynomial
3 */
4
5#include <vector>
6#include <iostream>
7
8class poly {
9 std::vector<int> coeffs ; // data are in the private part of the class
10
11 void reduce() ;
12
13public:
14
15 // contructors
16 poly() ; // make a zero polinomial
17
18 poly( poly const & ) ; // copy constructor
19
20 poly( int coeffs[], int degree ) ; // make a polynomial from a C vector
21
22 poly( std::vector<int> const & coeffs ) ; // make a polynomial from a C vector
23
24 int degree() const { return coeffs.size()-1 ; }
25
26 // access to the coefficients
27 int const & operator [] ( int i ) const { return coeffs[i] ; }
28 int & operator [] ( int i ) { return coeffs[i] ; }
29
30 int get( int i ) const { return i < coeffs.size() ? coeffs[i] : 0 ; }
31
32 // internal operator
33 poly const & operator += ( poly const & a ) ;
34 poly const & operator -= ( poly const & a ) ;
35 poly const & operator *= ( poly const & a ) ;
36 poly const & operator /= ( poly const & a ) ;
37 poly const & operator %= ( poly const & a ) ;
38
39 // evaluation operator
40 int const & operator () ( int x ) const ;
41
42 void print( std::ostream & s ) const ;
43} ;
44
45// external operator
46
47poly operator + ( poly const & a, poly const & b ) ;
48poly operator - ( poly const & a, poly const & b ) ;
49poly operator * ( poly const & a, poly const & b ) ;
50poly operator / ( poly const & a, poly const & b ) ;
51poly operator % ( poly const & a, poly const & b ) ;
52
53inline
54std::ostream & operator << ( std::ostream & s, poly const & a )
55{ a . print(s) ; return s ; }
File poly.cc
1#include "poly.hh"
2#include <algorithm>
3
4// contructors
5poly::poly() {
6 std::cout << "call poly::poly()\n" ;
7 coeffs . clear() ;
8}
9
10poly::poly( poly const & a ) {
11 std::cout << "call poly::poly( poly const & )\n" ;
12 coeffs . resize( a . coeffs . size() ) ;
13 std::copy( a.coeffs.begin(), a.coeffs.end(), coeffs.begin() ) ;
14}
15
16poly::poly( int coeffs[], int degree ) {
17 std::cout << "call poly::poly( int coeffs[], int degree )\n" ;
18 this -> coeffs . resize( degree + 1 ) ;
19 std::copy( coeffs, coeffs + degree + 1, this -> coeffs . begin() ) ;
20 reduce() ;
21}
22
23poly::poly( std::vector<int> const & coeffs ) {
24 std::cout << "call poly::poly(std::vector<int> const & coeffs)\n" ;
25 this -> coeffs . resize( coeffs.size() ) ;
26 std::copy( coeffs . begin(), coeffs . end(), this -> coeffs . begin() ) ;
27 reduce() ;
28}
29
30void
31poly::reduce() {
32 while ( (!coeffs.empty()) && coeffs.back() == 0 )
33 this -> coeffs . pop_back() ;
34}
35
36void
37poly::print( std::ostream & s ) const {
38 if ( coeffs . size() == 0 ) {
39 s << "0" ;
40 } else if ( coeffs . size() == 1 ) {
41 s << coeffs[0] ;
42 } else {
43 if ( coeffs[0] != 0 ) s << coeffs[0] ;
44 if ( coeffs[1] != 0 ) {
45 if ( coeffs[0] != 0 ) s << " + " ;
46 if ( coeffs[1] == 1 ) s << "x" ;
47 else s << coeffs[1] << "*x" ;
48 }
49 for ( int i = 2 ; i < coeffs.size() ; ++i )
50 if ( coeffs[i] != 0 )
51 if ( coeffs[i] == 1 ) s << " + x^" << i ;
52 else s << " + " << coeffs[i] << "*x^" << i ;
53 }
54}
55
56poly const &
57poly::operator += ( poly const & a ) {
58 if ( coeffs . size() < a . coeffs . size() )
59 coeffs . resize( a . coeffs . size() ) ;
60 for ( int i = 0 ; i <= a . degree() ; ++i )
61 coeffs[i] += a[i] ;
62 reduce() ;
63 return *this ;
64}
65
66poly const &
67poly::operator -= ( poly const & a ) {
68 if ( coeffs . size() < a . coeffs . size() )
69 coeffs . resize( a . coeffs . size() ) ;
70 for ( int i = 0 ; i <= a . degree() ; ++i )
71 coeffs[i] -= a[i] ;
72 reduce() ;
73 return *this ;
74}
75
76poly operator + ( poly const & a, poly const & b ) {
77 int degree = std::max(a.degree(), b.degree()) ;
78 std::vector<int> coeffs(degree+1) ;
79 for ( int i = 0 ; i <= degree ; ++i )
80 coeffs[i] = a.get(i) + b.get(i) ;
81 return poly( coeffs ) ;
82}
83
84poly operator * ( poly const & a, poly const & b ) {
85 int degree = a . degree() + b . degree() ;
86 std::vector<int> coeffs(degree+1) ;
87 for ( int i = 0 ; i <= degree ; ++i ) {
88 coeffs[i] = 0 ;
89 for ( int k = 0 ; k <= i ; ++k )
90 coeffs[i] += a.get(k)*b.get(i-k) ;
91 }
92 return poly( coeffs ) ;
93}
94
95#if 0
96
97// internal operator
98poly const & operator += ( poly const & a ) ;
99poly const & operator -= ( poly const & a ) ;
100poly const & operator *= ( poly const & a ) ;
101poly const & operator /= ( poly const & a ) ;
102poly const & operator %= ( poly const & a ) ;
103
104// evaluation operator
105int const & operator () ( int x ) const ;
106
107// external operator
108
109poly operator + ( poly const & a, poly const & b ) ;
110poly operator - ( poly const & a, poly const & b ) ;
111poly operator * ( poly const & a, poly const & b ) ;
112poly operator / ( poly const & a, poly const & b ) ;
113poly operator % ( poly const & a, poly const & b ) ;
114
115#endif
Testing the class
File test.cc
1#include "poly.hh"
2#include <iostream>
3
4int
5main() {
6 poly a, b, c, d ;
7 poly e(a) ;
8 int coeffs[] = { 1, 0, 2, 3, 4, 0 } ;
9 int degree = sizeof(coeffs)/sizeof(coeffs[0])-1 ;
10 poly f(coeffs,degree) ;
11
12 a = f+f ;
13
14 std::cout << "POLY f = " << f << '\n' ;
15 std::cout << "POLY a = " << a << '\n' ;
16 std::cout << "POLY a*f = " << a*f << '\n' ;
17
18 return 0 ;
19}