104494
|
1 /** testpolymorph.cpp --- A sequence of polymorphism examples.
|
|
2 *
|
|
3 * Copyright (C) 2009 Eric M. Ludlam
|
|
4 *
|
|
5 * Author: Eric M. Ludlam <eric@siege-engine.com>
|
|
6 * X-RCS: $Id: testpolymorph.cpp,v 1.2 2009/09/02 13:55:46 davenar Exp $
|
|
7 *
|
|
8 * This program is free software; you can redistribute it and/or
|
|
9 * modify it under the terms of the GNU General Public License as
|
|
10 * published by the Free Software Foundation; either version 2, or (at
|
|
11 * your option) any later version.
|
|
12 *
|
|
13 * This program is distributed in the hope that it will be useful, but
|
|
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
16 * General Public License for more details.
|
|
17 *
|
|
18 * You should have received a copy of the GNU General Public License
|
|
19 * along with this program; see the file COPYING. If not, write to
|
|
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
21 * Boston, MA 02110-1301, USA.
|
|
22 */
|
|
23
|
|
24 #include <cmath>
|
|
25
|
|
26 // Test 1 - Functions w/ prototypes
|
|
27 namespace proto {
|
|
28
|
|
29 int pt_func1(int arg1);
|
|
30 int pt_func1(int arg1) {
|
|
31 return 0;
|
|
32 }
|
|
33
|
|
34 }
|
|
35
|
|
36 // Test 2 - Functions w/ different arg lists.
|
|
37 namespace fcn_poly {
|
|
38
|
|
39 int pm_func(void) {
|
|
40 return 0;
|
|
41 }
|
|
42 int pm_func(int a) {
|
|
43 return a;
|
|
44 }
|
|
45 int pm_func(char a) {
|
|
46 return int(a);
|
|
47 }
|
|
48 int pm_func(double a) {
|
|
49 return int(floor(a));
|
|
50 }
|
|
51
|
|
52 }
|
|
53
|
|
54 // Test 3 - Methods w/ differet arg lists.
|
|
55 class meth_poly {
|
|
56 public:
|
|
57 int pm_meth(void) {
|
|
58 return 0;
|
|
59 }
|
|
60 int pm_meth(int a) {
|
|
61 return a;
|
|
62 }
|
|
63 int pm_meth(char a) {
|
|
64 return int(a);
|
|
65 }
|
|
66 int pm_meth(double a) {
|
|
67 return int(floor(a));
|
|
68 }
|
|
69
|
|
70 };
|
|
71
|
|
72 // Test 4 - Templates w/ partial specifiers.
|
|
73 namespace template_partial_spec {
|
|
74 template <typename T> class test
|
|
75 {
|
|
76 public:
|
|
77 void doSomething(T t) { };
|
|
78 };
|
|
79
|
|
80 template <typename T> class test<T *>
|
|
81 {
|
|
82 public:
|
|
83 void doSomething(T* t) { };
|
|
84 };
|
|
85 }
|
|
86
|
|
87 // Test 5 - Templates w/ full specicialization which may or may not share
|
|
88 // common functions.
|
|
89 namespace template_full_spec {
|
|
90 template <typename T> class test
|
|
91 {
|
|
92 public:
|
|
93 void doSomething(T t) { };
|
|
94 void doSomethingElse(T t) { };
|
|
95 };
|
|
96
|
|
97 template <> class test<int>
|
|
98 {
|
|
99 public:
|
|
100 void doSomethingElse(int t) { };
|
|
101 void doSomethingCompletelyDifferent(int t) { };
|
|
102 };
|
|
103 }
|
|
104
|
|
105 // Test 6 - Dto., but for templates with multiple parameters.
|
|
106 namespace template_multiple_spec {
|
|
107 template <typename T1, typename T2> class test
|
|
108 {
|
|
109 public:
|
|
110 void doSomething(T1 t) { };
|
|
111 void doSomethingElse(T2 t) { };
|
|
112 };
|
|
113
|
|
114 template <typename T2> class test<int, T2>
|
|
115 {
|
|
116 public:
|
|
117 void doSomething(int t) { };
|
|
118 void doSomethingElse(T2 t) { };
|
|
119 };
|
|
120
|
|
121 template <> class test<float, int>
|
|
122 {
|
|
123 public:
|
|
124 void doSomething(float t) { };
|
|
125 void doSomethingElse(int t) { };
|
|
126 void doNothing(void) { };
|
|
127 };
|
|
128 }
|
|
129
|
|
130
|
|
131 // End of polymorphism test file.
|