Skip to content

Commit cad4029

Browse files
committed
implement the pairs.cpp
1 parent d886d30 commit cad4029

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

pairs.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// pairs.cpp -- defining and using a Pair template
2+
#include <iostream>
3+
#include <string>
4+
template <class T1, class T2>
5+
class Pair
6+
{
7+
private:
8+
T1 a;
9+
T2 b;
10+
public:
11+
T1 & first();
12+
T2 & second();
13+
T1 first () const { return a; };
14+
T2 second() const { return b; };
15+
Pair(const T1 & aval, const T2 & bval) : a(aval), b(bval) { }
16+
Pair() {}
17+
};
18+
19+
template<class T1, class T2>
20+
T1 & Pair<T1, T2>::first()
21+
{
22+
return a;
23+
}
24+
25+
template<class T1, class T2>
26+
T2 & Pair<T1, T2>::second()
27+
{
28+
return b;
29+
}
30+
31+
int main()
32+
{
33+
using std::cout;
34+
using std::endl;
35+
using std::string;
36+
Pair<string, int> ratings[4] =
37+
{
38+
Pair<string, int>("The Purple Duck", 5),
39+
Pair<string, int>("Jake's Frisco AI Fresco", 4),
40+
Pair<string, int>("Mont Souffle", 5),
41+
Pair<string, int>("Gertie's Eats", 3)
42+
};
43+
44+
int joints = sizeof(ratings) / sizeof (Pair<string, int>);
45+
cout << "Rating:\t Eatery\n";
46+
for (int i = 0; i < joints; i++) {
47+
cout << ratings[i].second() << ":\t"
48+
<< ratings[i].first() << endl;
49+
}
50+
cout << "Opps! Revised rating:\n";
51+
ratings[3].first() = "Gertie's Fab Eat";
52+
ratings[3].second() = 6;
53+
cout << ratings[3].second() << ":\t"
54+
<< ratings[3].first() << endl;
55+
56+
return 0;
57+
}

0 commit comments

Comments
 (0)