Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

Thinking in C++ Vol 2 - Practical Programming
Prev Home Next

Exercises

Solutions to selected exercises can be found in the electronic document The Thinking in C++ Volume 2 Annotated Solution Guide, available for a small fee from www.MindView.net.

1.      Write a unary function template that takes a single type template parameter. Create a full specialization for the type int. Also create a non-template overload for this function that takes a single int parameter. Have your main program invoke three function variations.

2.      Write a class template that uses a vector to implement a stack data structure.

3.      Modify your solution to the previous exercise so that the type of the container used to implement the stack is a template template parameter.

4.      In the following code, the class NonComparable does not have an operator=( ). Why would the presence of the class HardLogic cause a compile error, but SoftLogic would not?

//: C05:Exercise4.cpp {-xo}
class Noncomparable {};
 
struct HardLogic {
Noncomparable nc1, nc2;
void compare() {
return nc1 == nc2; // Compiler error
}
};
 
template<class T> struct SoftLogic {
Noncomparable nc1, nc2;
void noOp() {}
void compare() {
nc1 == nc2;
}
};
 
int main() {
SoftLogic<Noncomparable> l;
l.noOp();
} ///:~

5.      Write a function template that takes a single type parameter (T) and accepts four function arguments: an array of T, a start index, a stop index (inclusive), and an optional initial value. The function returns the sum of all the array elements in the specified range and the initial value. Use the default constructor of T for the default initial value.

6.      Repeat the previous exercise but use explicit instantiation to manually create specializations for int and double, following the technique explained in this chapter.

7.      Why does the following code not compile? (Hint: what do class member functions have access to?)

//: C05:Exercise7.cpp {-xo}
class Buddy {};
 
template<class T> class My {
int i;
public:
void play(My<Buddy>& s) {
s.i = 3;
}
};
 
int main() {
My<int> h;
My<Buddy> me, bud;
h.play(bud);
me.play(bud);
} ///:~

8.      Why does the following code not compile?

//: C05:Exercise8.cpp {-xo}
template<class T> double pythag(T a, T b, T c) {
return (-b + sqrt(double(b*b - 4*a*c))) / 2*a;
}
 
int main() {
pythag(1, 2, 3);
pythag(1.0, 2.0, 3.0);
pythag(1, 2.0, 3.0);
pythag<double>(1, 2.0, 3.0);
} ///:~

9.      Write templates that take non-type parameters of the following variety: an int, a pointer to an int, a pointer to a static class member of type int, and a pointer to a static member function.

10.    Write a class template that takes two type parameters. Define a partial specialization for the first parameter, and another partial specialization that specifies the second parameter. In each specialization, introduce members that are not in the primary template.

11.    Define a class template named Bob that takes a single type parameter. Make Bob a friend of all instances of a template class named Friendly, and a friend of a class template named Picky only when the type parameter of Bob and Picky are identical. Give Bob member functions that demonstrate its friendship.

 


Thinking in C++ Vol 2 - Practical Programming
Prev Home Next

 
 
   Reproduced courtesy of Bruce Eckel, MindView, Inc. Design by Interspire