forked from ShujiaHuang/Cpp-Primer-Plus-6th
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
56eeae8
commit 5536026
Showing
25 changed files
with
1,444 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
//arraytp.h -- Array Template | ||
#ifndef ARRAYTP_H_ | ||
#define ARRAYTP_H_ | ||
|
||
#include <iostream> | ||
#include <cstdlib> | ||
|
||
template <class T, int n> | ||
class ArrayTP | ||
{ | ||
private: | ||
T ar[n]; | ||
public: | ||
ArrayTP() {}; | ||
explicit ArrayTP(const T & v); | ||
virtual T & operator[](int i); | ||
virtual T operator[](int i) const; | ||
}; | ||
|
||
template <class T, int n> | ||
ArrayTP<T,n>::ArrayTP(const T & v) | ||
{ | ||
for (int i = 0; i < n; i++) | ||
ar[i] = v; | ||
} | ||
|
||
template <class T, int n> | ||
T & ArrayTP<T,n>::operator[](int i) | ||
{ | ||
if (i < 0 || i >= n) | ||
{ | ||
std::cerr << "Error in array limits: " << i | ||
<< " is out of range\n"; | ||
std::exit(EXIT_FAILURE); | ||
} | ||
return ar[i]; | ||
} | ||
|
||
template <class T, int n> | ||
T ArrayTP<T,n>::operator[](int i) const | ||
{ | ||
if (i < 0 || i >= n) | ||
{ | ||
std::cerr << "Error in array limits: " << i | ||
<< " is out of range\n"; | ||
std::exit(EXIT_FAILURE); | ||
} | ||
return ar[i]; | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// frnd2tmp.cpp -- template class with non-template friends | ||
#include <iostream> | ||
using std::cout; | ||
using std::endl; | ||
|
||
template <typename T> | ||
class HasFriend | ||
{ | ||
private: | ||
T item; | ||
static int ct; | ||
public: | ||
HasFriend(const T & i) : item(i) {ct++;} | ||
~HasFriend() {ct--; } | ||
friend void counts(); | ||
friend void reports(HasFriend<T> &); // template parameter | ||
}; | ||
|
||
// each specialization has its own static data member | ||
template <typename T> | ||
int HasFriend<T>::ct = 0; | ||
|
||
// non-template friend to all HasFriend<T> classes | ||
void counts() | ||
{ | ||
cout << "int count: " << HasFriend<int>::ct << "; "; | ||
cout << "double count: " << HasFriend<double>::ct << endl; | ||
} | ||
|
||
// non-template friend to the HasFriend<int> class | ||
void reports(HasFriend<int> & hf) | ||
{ | ||
cout <<"HasFriend<int>: " << hf.item << endl; | ||
} | ||
|
||
// non-template friend to the HasFriend<double> class | ||
void reports(HasFriend<double> & hf) | ||
{ | ||
cout <<"HasFriend<double>: " << hf.item << endl; | ||
} | ||
|
||
int main() | ||
{ | ||
cout << "No objects declared: "; | ||
counts(); | ||
HasFriend<int> hfi1(10); | ||
cout << "After hfi1 declared: "; | ||
counts(); | ||
HasFriend<int> hfi2(20); | ||
cout << "After hfi2 declared: "; | ||
counts(); | ||
HasFriend<double> hfdb(10.5); | ||
cout << "After hfdb declared: "; | ||
counts(); | ||
reports(hfi1); | ||
reports(hfi2); | ||
reports(hfdb); | ||
// std::cin.get(); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// manyfrnd.cpp -- unbound template friend to a template class | ||
#include <iostream> | ||
using std::cout; | ||
using std::endl; | ||
|
||
template <typename T> | ||
class ManyFriend | ||
{ | ||
private: | ||
T item; | ||
public: | ||
ManyFriend(const T & i) : item(i) {} | ||
template <typename C, typename D> friend void show2(C &, D &); | ||
}; | ||
|
||
template <typename C, typename D> void show2(C & c, D & d) | ||
{ | ||
cout << c.item << ", " << d.item << endl; | ||
} | ||
|
||
int main() | ||
{ | ||
ManyFriend<int> hfi1(10); | ||
ManyFriend<int> hfi2(20); | ||
ManyFriend<double> hfdb(10.5); | ||
cout << "hfi1, hfi2: "; | ||
show2(hfi1, hfi2); | ||
cout << "hfdb, hfi2: "; | ||
show2(hfdb, hfi2); | ||
// std::cin.get(); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// pairs.cpp -- defining and using a Pair template | ||
#include <iostream> | ||
#include <string> | ||
template <class T1, class T2> | ||
class Pair | ||
{ | ||
private: | ||
T1 a; | ||
T2 b; | ||
public: | ||
T1 & first(); | ||
T2 & second(); | ||
T1 first() const { return a; } | ||
T2 second() const { return b; } | ||
Pair(const T1 & aval, const T2 & bval) : a(aval), b(bval) { } | ||
Pair() {} | ||
}; | ||
|
||
template<class T1, class T2> | ||
T1 & Pair<T1,T2>::first() | ||
{ | ||
return a; | ||
} | ||
template<class T1, class T2> | ||
T2 & Pair<T1,T2>::second() | ||
{ | ||
return b; | ||
} | ||
|
||
int main() | ||
{ | ||
using std::cout; | ||
using std::endl; | ||
using std::string; | ||
Pair<string, int> ratings[4] = | ||
{ | ||
Pair<string, int>("The Purpled Duck", 5), | ||
Pair<string, int>("Jaquie's Frisco Al Fresco", 4), | ||
Pair<string, int>("Cafe Souffle", 5), | ||
Pair<string, int>("Bertie's Eats", 3) | ||
}; | ||
|
||
int joints = sizeof(ratings) / sizeof (Pair<string, int>); | ||
cout << "Rating:\t Eatery\n"; | ||
for (int i = 0; i < joints; i++) | ||
cout << ratings[i].second() << ":\t " | ||
<< ratings[i].first() << endl; | ||
cout << "Oops! Revised rating:\n"; | ||
ratings[3].first() = "Bertie's Fab Eats"; | ||
ratings[3].second() = 6; | ||
cout << ratings[3].second() << ":\t " | ||
<< ratings[3].first() << endl; | ||
// std::cin.get(); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// stacktem.cpp -- testing the template stack class | ||
#include <iostream> | ||
#include <string> | ||
#include <cctype> | ||
#include "stacktp.h" | ||
using std::cin; | ||
using std::cout; | ||
|
||
int main() | ||
{ | ||
Stack<std::string> st; // create an empty stack | ||
char ch; | ||
std::string po; | ||
cout << "Please enter A to add a purchase order,\n" | ||
<< "P to process a PO, or Q to quit.\n"; | ||
while (cin >> ch && std::toupper(ch) != 'Q') | ||
{ | ||
while (cin.get() != '\n') | ||
continue; | ||
if (!std::isalpha(ch)) | ||
{ | ||
cout << '\a'; | ||
continue; | ||
} | ||
switch(ch) | ||
{ | ||
case 'A': | ||
case 'a': cout << "Enter a PO number to add: "; | ||
cin >> po; | ||
if (st.isfull()) | ||
cout << "stack already full\n"; | ||
else | ||
st.push(po); | ||
break; | ||
case 'P': | ||
case 'p': if (st.isempty()) | ||
cout << "stack already empty\n"; | ||
else { | ||
st.pop(po); | ||
cout << "PO #" << po << " popped\n"; | ||
break; | ||
} | ||
} | ||
cout << "Please enter A to add a purchase order,\n" | ||
<< "P to process a PO, or Q to quit.\n"; | ||
} | ||
cout << "Bye\n"; | ||
// cin.get(); | ||
// cin.get(); | ||
return 0; | ||
} |
Oops, something went wrong.