Templated Functions and Classes: Friend Functions, Min Function, Stack, and Graph, Exercises of Data Structures and Algorithms

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

2011/2012

Uploaded on 07/11/2012

dhanvant
dhanvant 🇮🇳

4.9

(9)

89 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
6.096 Problem Set 4
1 Additional Material
1.1 Templates and Header Files
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.
1.2 Templates and friend Functions
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:
1. A templated forward declaration of the class. (A forward declaration is a statement
like class SomeClass;, with no class body, that just alerts the compiler that the class
definition is coming.)
2. A templated function prototype for the friend function (or the entire function defini-
tion).
3. The full templated class definition, including the friend statement. When you write
the name of the function in the friend statement, you need to include an extra <> after
it to indicate that it is a templated function (e.g., friend MyClass operator+<>(const
MyClass &c1, const MyClass &c2;).
4. The operator function definition, if you didn’t include it above.
1
Docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Templated Functions and Classes: Friend Functions, Min Function, Stack, and Graph and more Exercises Data Structures and Algorithms in PDF only on Docsity!

6.096 Problem Set 4

1 Additional Material

1.1 Templates and Header Files

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.

1.2 Templates and friend Functions

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:

  1. A templated forward declaration of the class. (A forward declaration is a statement like class SomeClass;, with no class body, that just alerts the compiler that the class definition is coming.)
  2. A templated function prototype for the friend function (or the entire function defini tion).
  3. The full templated class definition, including the friend statement. When you write the name of the function in the friend statement, you need to include an extra <> after it to indicate that it is a templated function (e.g., friend MyClass operator+<>(const MyClass &c1, const MyClass &c2;).
  4. The operator function definition, if you didn’t include it above.

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:

  • Push – add a new item to the stack
  • Pop – remove the most recently added item that is still in the stack (i.e. that has not yet been popped)

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:

  • Graph::Graph(const vector &starts, const vector &ends) Constructs a Graph with the given set of edges, where starts and ends represent the ordered list of edges’ start and endpoints. For instance, consider the following graph:

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

  • int Graph::numOutgoing(const int nodeID) const Returns the number of outgoing edges from nodeID – that is, edges with nodeID as the start point
  • const vector &Graph::adjacent(const int nodeID) const Returns a reference to the list of nodes to which nodeID has outgoing edges

(Hint: Use the following data type to associate adjacency lists with node ID’s: map<int, vector >. This will also allow for easy lookup of the adjacency list for a given node ID. Note that the [] operator for maps inserts the item if it isn’t already there.) The constructor should throw an invalid argument exception of the two vectors are not the same length. (This standard exception class is defined in header file stdexcept.) The other member functions should do likewise if the nodeID provided does not actually exist in the graph. (Hint: Use the map::find, documented at http://www.cplusplus.com/reference/stl/map/find/.)