Lesson of 24 June 2008¶
An example of overloading
File overloading.cc
:open:
1/*
2// An example of overloading
3//
4*/
5
6#include <iostream>
7
8using namespace std ;
9
10// define the function factorial with integer argument
11int
12factorial( int a ) {
13 cout << "using factorial(int) with a = " << a << "\n" ;
14 if ( a > 1 ) return a*factorial(a-1) ;
15 else return 1 ;
16}
17
18// define the function factorial with unsigned long integer argument
19unsigned long
20factorial( unsigned long a ) {
21 cout << "using factorial(unsigned long) with a = " << a << "\n" ;
22 if ( a > 1 ) return a*factorial(a-1) ;
23 else return 1 ;
24}
25
26int
27main() {
28 unsigned long ula = 10 ;
29 int ia = 10 ;
30 cout << factorial(ia) << "\n" ;
31 cout << factorial(ula) << "\n" ;
32 long b = factorial(ia) ;
33 return 0 ;
34}
An example of Inheritance
File inherith.cc
:open:
1/*
2// An example of inheritance
3//
4*/
5
6#include <iostream>
7
8using namespace std ;
9
10// define a base class
11class base_class {
12 int value ; // declare private variable
13public:
14
15 base_class()
16 : value(0) // calling the costructor for value
17 { cout << "base_class constructor\n" ; }
18 /* this is similar to
19 base_class(){ value = 0 ; }
20 the difference is on the order of the operatio.
21 in this latter case the default costrutor for
22 value is performed and next is assigned to 0.
23 using the proposed costruct the variable value
24 is initialized to 0 directly. Thus is a better
25 performing design
26 */
27
28 // a public method
29 void printName(ostream & s)
30 { s << "I am base class\n" ; }
31
32 // accessor method
33 int getValue() const { return value ; }
34} ;
35
36// define extend_class inherithing from base_class
37class extend_class : public base_class {
38public:
39 extend_class()
40 : base_class() // call the base_class constructor
41 { cout << "extend_class constructor\n" ; }
42
43 // redefine the method printName
44 void printName(ostream & s)
45 { s << "I am extend_class class\n" ; }
46} ;
47
48int
49main() {
50 base_class bc ; // instance bc with the default constructor
51 extend_class ec ; // instance ec with the default constructor
52 bc . printName( cout ) ;
53 ec . printName( cout ) ;
54 return 0 ;
55}
An example of Inheritance (bis)
File inherith2.cc
:open:
1/*
2// An example of inheritance
3//
4*/
5
6#include <iostream>
7
8using namespace std ;
9
10typedef double valueType ;
11typedef int indexType ;
12
13class point2d {
14protected:
15 valueType x, y ;
16public:
17
18 point2d() : x(0), y(0) // calling the costructor for value
19 { cout << "point2d constructor\n" ; }
20
21 point2d(valueType xx, valueType yy) : x(xx), y(yy) // calling the costructor for value
22 { cout << "point2d constructor N.2\n" ; }
23
24 ~point2d()
25 { cout << "point2d destructor\n" ; }
26
27 point2d const &
28 operator = (point2d const & a) {
29 cout << "assign operator\n" ;
30 x = a.x ;
31 y = a.y ;
32 return *this ;
33 }
34
35 valueType getX() const { return x ; }
36 valueType getY() const { return y ; }
37 void set( valueType x, valueType y ) {
38 this -> x = x ;
39 this -> y = y ;
40 }
41
42} ;
43
44class point3d : public point2d {
45 valueType z ;
46public:
47
48 point3d() : point2d(0,0), z(0) // calling the costructor for value
49 { cout << "point3d constructor\n" ; }
50
51 point3d(valueType xx, valueType yy, valueType zz)
52 : point2d(xx,yy), z(zz) // calling the costructor for value
53 { cout << "point3d constructor N.2\n" ; }
54
55 ~point3d()
56 { cout << "point3d destructor\n" ; }
57
58 point3d const &
59 operator = (point3d const & a) {
60 cout << "assign operator\n" ;
61 x = a.x ;
62 y = a.y ;
63 z = a.z ;
64 return *this ;
65 }
66
67 valueType getZ() const { return z ; }
68
69 void set( valueType x, valueType y, valueType z ) {
70 point2d::set(x,y) ;
71 this -> z = z ;
72 }
73} ;
74
75
76// define external operator for addition
77point2d
78operator + ( point2d const & a, point2d const & b)
79{ cout << "perform + operator\n" ;
80 return point2d( a.getX()+b.getX(), a.getY() + b.getY()) ; }
81/*
82
83 */
84
85int
86main() {
87 cout << "before point2d a, b ;\n" ;
88 point2d a, b, c ;
89 cout << "after point2d a, b ;\n" ;
90 c = a + b ;
91 // this is eqauivalent to
92
93 c . operator = ( operator + (a,b) ) ;
94
95 cout << "before to exit;\n" ;
96 // the same operatio without temporary class production
97 a . set( a.getX() + b.getX(), a.getY() + b.getY() ) ;
98 return 0 ;
99}
The lesson in a zip file
The zip file lesson3.zip