



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Problem set solutions for various topics in c++ programming, including templated functions and classes, friend functions, min function implementation, casting, and stack and graph data structures. It covers the use of templates for creating generic functions and classes, the syntax for declaring friend functions with templated classes, implementing a min function, casting between classes, and creating a templated stack class using stl vectors and a templated graph class using adjacency lists.
Typology: Exercises
1 / 5
This page cannot be seen from the preview
Don't miss anything!




The compiler does not compile a templated function until it encounters a use of it – until that function is used with a particular type parameter, at which point it is compiled for that type parameter. Because of this, if you define a templated class in a header file and implement its (templated) functions in a .cpp file, the code in the .cpp file will never get compiled unless you use the templated functions within that .cpp file. To solve this problem, templated classes and functions are generally just implemented in the header file, with no .cpp file.
There are some syntax oddities when using friend functions with templated classes. In order to declare the function as a friend of the class, you need a function prototype (or the full function definition) to appear before the class definition. This is a problem, because you’ll often need to have the class already defined in order for the function prototype to make sense. To get around this issue, when writing friend functions with templated classes, you should include declarations in the following order:
2 Multi-Type min
Using templates, implement a min function which returns the minimum of two elements of any comparable type (i.e., it takes two arguments of some type T, and works as long as values of type T can be compared with the < operator). (To test this function, you may need to omit your usual using namespace std; line, since there is already an std::min function.)
Implement the min functionality from part 1 using only preprocessor macros. (Hint: You will probably need the ternary operator – the ?: syntax.)
3 Casting
Assume you implemented Problem 5 from Problem Set 3 correctly. This would mean you would have a working Polygon class, and inheriting from that a Triangle class and a Rectangle class. Now imagine you have a pointer declared as Rectangle *rect; that has been properly initialized.
Write a line of code showing how you would cast rect to a Triangle * without checking for type correctness (i.e., without checking whether it actually points to a Triangle). Do not use C-style casts.
Now write a line of code that does the same thing, but checks for type correctness and throws an exception or returns a null pointer if rect does not actually point to a Triangle.
4 Templated Stack
A stack data structure stores a set of items, and allows accessing them via the following operations:
5 Graph Representation (Optional)
A graph is a mathematical data structure consisting of nodes and edges connecting them. To help you visualize it, you can think of a graph as a map of “cities” (nodes) and “roads” (edges) connecting them. In an directed graph, the direction of the edge matters – that is, an edge from A to B is not also an edge from B to A. You can read more at Wikipedia: http://en.wikipedia.org/wiki/Graph (mathematics). One way to represent a graph is by assigning each node a unique ID number. Then, for each node ID n, you can store a list of node ID’s to which n has an outgoing edge. This list is called an adjacency list. Write a Graph class that uses STL containers (vectors, maps, etc.) to represent a directed graph. Each node should be represented by a unique integer (an int). Provide the following member functions:
The vectors used to initialize a Graph object representing this graph would be: start: 1 1 1 5 5 4 end: 2 3 4 4 2 2
(Hint: Use the following data type to associate adjacency lists with node ID’s: map<int, vector