Docsity

C++ Template List: Creating & Displaying Custom Number-String Pairs, Exercises of Object Oriented Programming

Toggle sidebar
bg1
1c:\documents๎˜ƒand๎˜ƒsettings\usman.younis\my๎˜ƒ...\TemplateList\TemplateList\TemplateList.cpp
1๎˜ƒ#๎˜ƒinclude๎˜ƒ๎˜ƒ<๎˜ƒiostream๎˜ƒ>
2๎˜ƒ#๎˜ƒinclude๎˜ƒ๎˜ƒ<๎˜ƒstring๎˜ƒ>
3
4๎˜ƒusing๎˜ƒnamespace๎˜ƒstd๎˜ƒ;
5
6๎˜ƒclass๎˜ƒNumber
7๎˜ƒ{
8๎˜ƒpublic๎˜ƒ:
9๎˜ƒ๎˜ƒ ๎˜ƒint๎˜ƒintData๎˜ƒ;
10 ๎˜ƒ๎˜ƒ ๎˜ƒNumber๎˜ƒ():๎˜ƒ๎˜ƒintData๎˜ƒ(๎˜ƒ0๎˜ƒ)๎˜ƒ๎˜ƒ{}
11 ๎˜ƒ๎˜ƒ ๎˜ƒfriend๎˜ƒostream๎˜ƒ&๎˜ƒ๎˜ƒoperator๎˜ƒ<<๎˜ƒ(๎˜ƒostream๎˜ƒ&๎˜ƒ๎˜ƒout๎˜ƒ,๎˜ƒ๎˜ƒNumber๎˜ƒ&๎˜ƒ๎˜ƒa๎˜ƒ)๎˜ƒ;
12 ๎˜ƒ};
13
14 ๎˜ƒostream๎˜ƒ&๎˜ƒ๎˜ƒoperator๎˜ƒ<<๎˜ƒ(๎˜ƒostream๎˜ƒ&๎˜ƒ๎˜ƒout๎˜ƒ,๎˜ƒ๎˜ƒNumber๎˜ƒ&๎˜ƒ๎˜ƒa๎˜ƒ)
15 ๎˜ƒ{
16 ๎˜ƒ๎˜ƒ ๎˜ƒout๎˜ƒ<<๎˜ƒa๎˜ƒ.๎˜ƒintData๎˜ƒ;
17 ๎˜ƒ๎˜ƒ ๎˜ƒreturn๎˜ƒout๎˜ƒ;
18 ๎˜ƒ}
19
20 ๎˜ƒtemplate๎˜ƒ๎˜ƒ<๎˜ƒclass๎˜ƒType๎˜ƒ1๎˜ƒ,๎˜ƒ๎˜ƒclass๎˜ƒType๎˜ƒ2๎˜ƒ>
21 ๎˜ƒclass๎˜ƒList
22 ๎˜ƒ{
23 ๎˜ƒprivate๎˜ƒ:
24 ๎˜ƒ๎˜ƒ ๎˜ƒType๎˜ƒ1๎˜ƒ๎˜ƒData๎˜ƒ1๎˜ƒ;
25 ๎˜ƒ๎˜ƒ ๎˜ƒType๎˜ƒ2๎˜ƒ๎˜ƒData๎˜ƒ2๎˜ƒ;
26 ๎˜ƒ๎˜ƒ ๎˜ƒList๎˜ƒ*๎˜ƒ๎˜ƒnext๎˜ƒ;
27 ๎˜ƒpublic๎˜ƒ:
28 ๎˜ƒ๎˜ƒ ๎˜ƒList๎˜ƒ():๎˜ƒ๎˜ƒnext๎˜ƒ(๎˜ƒNULL๎˜ƒ)๎˜ƒ๎˜ƒ{}๎˜ƒ
29 ๎˜ƒ๎˜ƒ
30 ๎˜ƒ๎˜ƒ ๎˜ƒvoid๎˜ƒadd๎˜ƒ
_
๎˜ƒitem๎˜ƒ(๎˜ƒType๎˜ƒ1๎˜ƒ๎˜ƒa๎˜ƒ,๎˜ƒ๎˜ƒType๎˜ƒ2๎˜ƒ๎˜ƒb๎˜ƒ)๎˜ƒ;
31 ๎˜ƒ๎˜ƒ ๎˜ƒvoid๎˜ƒshow๎˜ƒ
_
๎˜ƒList๎˜ƒ()๎˜ƒ;๎˜ƒ
32 ๎˜ƒ};
33
34 ๎˜ƒtemplate๎˜ƒ๎˜ƒ<๎˜ƒclass๎˜ƒType๎˜ƒ1๎˜ƒ,๎˜ƒ๎˜ƒclass๎˜ƒType๎˜ƒ2๎˜ƒ>
35 ๎˜ƒvoid๎˜ƒList๎˜ƒ<๎˜ƒType๎˜ƒ1๎˜ƒ,๎˜ƒ๎˜ƒType๎˜ƒ2๎˜ƒ>๎˜ƒ::๎˜ƒadd๎˜ƒ
_
๎˜ƒitem๎˜ƒ(๎˜ƒType๎˜ƒ1๎˜ƒ๎˜ƒa๎˜ƒ,๎˜ƒ๎˜ƒType๎˜ƒ2๎˜ƒ๎˜ƒb๎˜ƒ)
36 ๎˜ƒ{
37 ๎˜ƒ๎˜ƒ ๎˜ƒList๎˜ƒ*๎˜ƒ๎˜ƒnewLink๎˜ƒ๎˜ƒ=๎˜ƒ๎˜ƒnew๎˜ƒList๎˜ƒ<๎˜ƒType๎˜ƒ1๎˜ƒ,๎˜ƒ๎˜ƒType๎˜ƒ2๎˜ƒ>;
38 ๎˜ƒ๎˜ƒ ๎˜ƒnewLink๎˜ƒโ€๎˜ƒ>๎˜ƒData๎˜ƒ1๎˜ƒ ๎˜ƒ=๎˜ƒ๎˜ƒa๎˜ƒ;
39 ๎˜ƒ๎˜ƒ ๎˜ƒnewLink๎˜ƒโ€๎˜ƒ>๎˜ƒData๎˜ƒ2๎˜ƒ ๎˜ƒ=๎˜ƒ๎˜ƒb๎˜ƒ;
40 ๎˜ƒ๎˜ƒ ๎˜ƒnewLink๎˜ƒโ€๎˜ƒ>๎˜ƒnext๎˜ƒ ๎˜ƒ=๎˜ƒ๎˜ƒnext๎˜ƒ;
41 ๎˜ƒ๎˜ƒ
42 ๎˜ƒ๎˜ƒ ๎˜ƒnext๎˜ƒ๎˜ƒ=๎˜ƒ๎˜ƒnewLink๎˜ƒ;
43 ๎˜ƒ}
44
45 ๎˜ƒtemplate๎˜ƒ๎˜ƒ<๎˜ƒclass๎˜ƒType๎˜ƒ1๎˜ƒ,๎˜ƒ๎˜ƒclass๎˜ƒType๎˜ƒ2๎˜ƒ>
46 ๎˜ƒvoid๎˜ƒList๎˜ƒ<๎˜ƒType๎˜ƒ1๎˜ƒ,๎˜ƒ๎˜ƒType๎˜ƒ2๎˜ƒ>๎˜ƒ::๎˜ƒshow๎˜ƒ
_
๎˜ƒList๎˜ƒ()
47 ๎˜ƒ{
48 ๎˜ƒ๎˜ƒ ๎˜ƒList๎˜ƒ*๎˜ƒ๎˜ƒTemp๎˜ƒ๎˜ƒ=๎˜ƒ๎˜ƒnext๎˜ƒ;
49
50 ๎˜ƒ๎˜ƒ ๎˜ƒwhile๎˜ƒ(๎˜ƒTemp๎˜ƒ๎˜ƒ!=๎˜ƒ๎˜ƒNULL๎˜ƒ)
51 ๎˜ƒ๎˜ƒ ๎˜ƒ{
52 ๎˜ƒ๎˜ƒ๎˜ƒ๎˜ƒcout๎˜ƒ<<๎˜ƒTemp๎˜ƒโ€๎˜ƒ>๎˜ƒData๎˜ƒ1๎˜ƒ<<๎˜ƒ"๎˜ƒ:๎˜ƒ"๎˜ƒ;
53 ๎˜ƒ๎˜ƒ๎˜ƒ๎˜ƒcout๎˜ƒ<<๎˜ƒTemp๎˜ƒโ€๎˜ƒ>๎˜ƒData๎˜ƒ2๎˜ƒ<<๎˜ƒendl๎˜ƒ;
54 ๎˜ƒ๎˜ƒ๎˜ƒ๎˜ƒTemp๎˜ƒ๎˜ƒ=๎˜ƒ๎˜ƒTemp๎˜ƒโ€๎˜ƒ>๎˜ƒnext๎˜ƒ;
55 ๎˜ƒ๎˜ƒ ๎˜ƒ}
56 ๎˜ƒ}
57
58 ๎˜ƒvoid๎˜ƒmain๎˜ƒ(๎˜ƒvoid๎˜ƒ)
59 ๎˜ƒ{
60 ๎˜ƒ๎˜ƒ ๎˜ƒList๎˜ƒ<๎˜ƒint๎˜ƒ,๎˜ƒ๎˜ƒfloat๎˜ƒ>๎˜ƒ๎˜ƒintfloatList๎˜ƒ;
61
62 ๎˜ƒ๎˜ƒ ๎˜ƒintfloatList๎˜ƒ.๎˜ƒadd๎˜ƒ
_
๎˜ƒitem๎˜ƒ(๎˜ƒ3๎˜ƒ,๎˜ƒ๎˜ƒ4๎˜ƒ.๎˜ƒ5๎˜ƒF๎˜ƒ)๎˜ƒ;
63 ๎˜ƒ๎˜ƒ ๎˜ƒintfloatList๎˜ƒ.๎˜ƒadd๎˜ƒ
_
๎˜ƒitem๎˜ƒ(๎˜ƒ5๎˜ƒ,๎˜ƒ๎˜ƒ2๎˜ƒ.๎˜ƒ3๎˜ƒF๎˜ƒ)๎˜ƒ;
64 ๎˜ƒ๎˜ƒ ๎˜ƒintfloatList๎˜ƒ.๎˜ƒadd๎˜ƒ
_
๎˜ƒitem๎˜ƒ(๎˜ƒ7๎˜ƒ,๎˜ƒ๎˜ƒ5๎˜ƒ.๎˜ƒ7๎˜ƒF๎˜ƒ)๎˜ƒ;
65 ๎˜ƒ๎˜ƒ ๎˜ƒintfloatList๎˜ƒ.๎˜ƒadd๎˜ƒ
_
๎˜ƒitem๎˜ƒ(๎˜ƒ8๎˜ƒ,๎˜ƒ๎˜ƒ8๎˜ƒ.๎˜ƒ9๎˜ƒF๎˜ƒ)๎˜ƒ;
66 ๎˜ƒ๎˜ƒ ๎˜ƒintfloatList๎˜ƒ.๎˜ƒadd๎˜ƒ
_
๎˜ƒitem๎˜ƒ(๎˜ƒ2๎˜ƒ,๎˜ƒ๎˜ƒ5๎˜ƒ.๎˜ƒ3๎˜ƒF๎˜ƒ)๎˜ƒ;
docsity.com
pf2

Partial preview of the text

Download C++ Template List: Creating & Displaying Custom Number-String Pairs and more Exercises Object Oriented Programming in PDF only on Docsity!

c:\documents and settings\usman.younis\my ...\TemplateList\TemplateList\TemplateList.cpp 1

1 #include 2 #include 3 4 using namespace std; 5 6 class Number 7 { 8 public: 9 int intData; 10 Number(): intData( 0 ) {} 11 friend ostream& operator<<(ostream& out, Number& a); 12 }; 13 14 ostream& operator<<(ostream& out, Number& a) 15 { 16 out<<a.intData; 17 return out; 18 } 19 20 template <class Type 1 , class Type 2 > 21 class List 22 { 23 private: 24 Type 1 Data 1 ; 25 Type 2 Data 2 ; 26 List* next; 27 public: 28 List(): next(NULL) {} 29 30 void add_item(Type 1 a, Type 2 b); 31 void show_List(); 32 }; 33 34 template <class Type 1 , class Type 2 > 35 void List<Type 1 , Type 2 >::add_item(Type 1 a, Type 2 b) 36 { 37 List* newLink = new List<Type 1 , Type 2 >; 38 newLinkโ€>Data 1 = a; 39 newLinkโ€>Data 2 = b; 40 newLinkโ€>next = next; 41 42 next = newLink; 43 } 44 45 template <class Type 1 , class Type 2 > 46 void List<Type 1 , Type 2 >::show_List() 47 { 48 List* Temp = next; 49 50 while(Temp != NULL) 51 { 52 cout<<Tempโ€>Data 1 <<" : "; 53 cout<<Tempโ€>Data 2 <<endl; 54 Temp = Tempโ€>next; 55 } 56 } 57 58 void main(void) 59 { 60 List<int, float> intfloatList; 61 62 intfloatList.add_item( 3 , 4. 5 F); 63 intfloatList.add_item( 5 , 2. 3 F); 64 intfloatList.add_item( 7 , 5. 7 F); 65 intfloatList.add_item( 8 , 8. 9 F); 66 intfloatList.add_item( 2 , 5. 3 F);

docsity.com

c:\documents and settings\usman.younis\my ...\TemplateList\TemplateList\TemplateList.cpp 2

67 68 intfloatList.show_List(); 69 70 71 Number numArg; 72 string strArg; 73 74 List<Number, string> numberstringList; 75 76 numArg.intData = 23 ; 77 strArg = "twenty three"; 78 numberstringList.add_item(numArg, strArg); 79 80 numArg.intData = 1 ; 81 strArg = "one"; 82 numberstringList.add_item(numArg, strArg); 83 84 numArg.intData = 65 ; 85 strArg = "sixty five"; 86 numberstringList.add_item(numArg, strArg); 87 88 numberstringList.show_List(); 89 }

docsity.com