-
Notifications
You must be signed in to change notification settings - Fork 0
/
template1.cpp
113 lines (97 loc) · 1.93 KB
/
template1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <iostream>
#include <string>
using namespace std;
const char * nameArr[4] = {
"Carlos",
"Mancito",
"Manola",
"Rodomirfo"
};
class People
{
public:
People():name("Nonw"),edad(10){}
~People(){}
const char * getName(){ return name; }
int getEdad(){ return edad; }
void setNameAndAge(const char *newName, int newEdad);
bool operator>(People people);
private:
const char *name;
int edad;
};
void People::setNameAndAge(const char *newName, int newEdad)
{
name = newName;
edad = newEdad;
}
bool People::operator>(People people)
{
if (edad > people.edad)
return true;
else
return false;
}
template <class T>
T max(T x, T y) {
return (x > y) ? x : y;
};
template <class T>
class Tabla {
public:
Tabla(int nElem);
~Tabla();
T& operator[](int indice) { return pT[indice]; }
private:
T *pT;
int nElementos;
};
// Definición:
template <class T>
Tabla<T>::Tabla(int nElem) : nElementos(nElem) {
pT = new T[nElementos];
}
template <class T>
Tabla<T>::~Tabla() {
delete[] pT;
}
//Class tempalte declaration
template <class T>
class MyTable
{
public:
MyTable(int nElem);
~MyTable();
T& operator[] (int indice){ return pT[indice]; }
private:
T *pT;
int nElementos;
};
template <class T>
MyTable<T>::MyTable(int nElem) : nElementos(nElem){
pT = new T[nElementos];
}
template <class T>
MyTable<T>::~MyTable()
{
delete[] pT;
}
int main()
{
MyTable<People> peopleTable(4);
for (int i = 0; i < 4; i++)
{
peopleTable[i].setNameAndAge(nameArr[i], (i + 12));
cout << "name " << peopleTable[i].getName() << endl;
cout << "edad " << peopleTable[i].getEdad() << endl;
}
if (peopleTable[3] > peopleTable[1])
{
cout << peopleTable[3].getName() << " es mayor que " << peopleTable[1].getName() << endl;
}
else
{
cout << peopleTable[3].getName() << " es menor que " << peopleTable[1].getName() << endl;
}
return 0;
}