Skip to content

Commit ebb63e8

Browse files
pair impl finished
1 parent dd01e12 commit ebb63e8

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

cpp4j/cpp4j.pro

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ SOURCES += rationalnumber.cpp \
1313

1414
HEADERS += \
1515
rationalnumber.h \
16-
rationalnumberarray.h
16+
rationalnumberarray.h \
17+
pair.h
1718

cpp4j/pair.h

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#ifndef PAIR_H
22
#define PAIR_H
33

4-
template <T1,T2> class Pair {
4+
#include <stdio.h>
5+
6+
template <class T1, class T2> class Pair {
57

68
public :
79

8-
Pair ():
10+
Pair ():
911
m_first(),
1012
m_second()
1113
{ }
@@ -15,7 +17,7 @@ public :
1517
m_second(secondParam)
1618
{ }
1719

18-
Pair (Pair otherPair):
20+
Pair (Pair <T1,T2>& otherPair):
1921
m_first(otherPair.first()),
2022
m_second(otherPair.second())
2123
{ }
@@ -25,31 +27,31 @@ public :
2527
return Pair(right.first(), right.second());
2628
}
2729

28-
ostream& operator << (ostream &os){
29-
os << first();
30-
os << second();
30+
friend ostream& operator<< (ostream &os, const Pair<T1,T2>& pair){
31+
os << '<' << pair.first() << ',' << pair.second() << '>';
32+
return os;
3133
}
3234

3335
T1& first(void){
34-
return &m_first;
36+
return m_first;
3537
}
3638

3739
const T1& first(void) const {
38-
return &m_first;
40+
return m_first;
3941
}
4042

4143
T2& second(void){
42-
return &m_second;
44+
return m_second;
4345
}
4446

4547
const T2& second(void) const{
46-
return &m_second;
48+
return m_second;
4749
}
4850

4951
private:
5052

5153
T1 m_first ;
5254
T2 m_second;
5355

54-
}
56+
};
5557
#endif // PAIR_H

cpp4j/testTemplates.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,17 @@ using namespace std;
2020

2121
// you should define your own namespace for
2222
// the templates/classes in this project
23-
using namespace mystl;
23+
24+
//using namespace mystl;
2425

2526
// explicit template instantiation for compiling / debugging
2627
//template class Pair<int,string>;
2728
//template class Tree< int,Less<int> >;
2829
//template class Map<int, string>;
2930

3031
// list and count all nodes in a set using an iterator
32+
/*
33+
3134
template<class Container>
3235
int printAndCount(Container & c) {
3336
@@ -54,6 +57,7 @@ int printAndCountBackwards(Container & c) {
5457
return n;
5558
}
5659
60+
*/
5761

5862
int testTemplates(void)
5963
{
@@ -68,14 +72,14 @@ int testTemplates(void)
6872
Pair<string,string> s_s("Hello","World!");
6973
cout << i_f << " " << s_s << endl;
7074

71-
#if 0 // move this line down while your implementation proceeds...
7275

7376
/////////////////////////////////////////
7477
// TEST ORDER
7578
Less<int> lessInt;
7679
cout << "2<3 == " << lessInt(2,3) << endl;
7780
cout << "4<3 == " << lessInt(4,3) << endl;
7881

82+
#if 0 // move this line down while your implementation proceeds...
7983
/////////////////////////////////////////
8084
// TEST PAIR ORDER
8185
MapToFirst< int, float, Less > lessPair;

0 commit comments

Comments
 (0)