Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

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

The implementation of a c++ template list, where each element in the list is a custom class consisting of an integer and a string. The document demonstrates how to create and add items to the list, as well as displaying the list's contents.

Typology: Exercises

2011/2012

Uploaded on 07/31/2012

netu
netu 🇮🇳

4.5

(4)

55 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1c:\documentsandsettings\usman.younis\my...\TemplateList\TemplateList\TemplateList.cpp
1#include<iostream>
2#include<string>
3
4usingnamespacestd;
5
6classNumber
7{
8public:
9 intintData;
10 Number():intData(0){}
11 friendostream&operator<<(ostream&out,Number&a);
12 };
13
14 ostream&operator<<(ostream&out,Number&a)
15 {
16 out<<a.intData;
17 returnout;
18 }
19
20 template<classType1,classType2>
21 classList
22 {
23 private:
24 Type1Data1;
25 Type2Data2;
26 List*next;
27 public:
28 List():next(NULL){}
29
30 voidadd
_
item(Type1a,Type2b);
31 voidshow
_
List();
32 };
33
34 template<classType1,classType2>
35 voidList<Type1,Type2>::add
_
item(Type1a,Type2b)
36 {
37 List*newLink=newList<Type1,Type2>;
38 newLink‐>Data1 =a;
39 newLink‐>Data2 =b;
40 newLink‐>next =next;
41
42 next=newLink;
43 }
44
45 template<classType1,classType2>
46 voidList<Type1,Type2>::show
_
List()
47 {
48 List*Temp=next;
49
50 while(Temp!=NULL)
51 {
52 cout<<Temp‐>Data1<<":";
53 cout<<Temp‐>Data2<<endl;
54 Temp=Temp‐>next;
55 }
56 }
57
58 voidmain(void)
59 {
60 List<int,float>intfloatList;
61
62 intfloatList.add
_
item(3,4.5F);
63 intfloatList.add
_
item(5,2.3F);
64 intfloatList.add
_
item(7,5.7F);
65 intfloatList.add
_
item(8,8.9F);
66 intfloatList.add
_
item(2,5.3F);
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