Chapter 14 - Worked Exercises 2

Exercise 1

Part 1

Create a template function called swap that would swap the values of two variables provided:

#include <iostream>

int main() {
    int a = 10;
    int b = 20;
    swap(a, b);
    std::cout << "a: " << a << std::endl;
    std::cout << "b: " << b << std::endl;
}

The function should work for variables a and b of any type – an std::string, an instance of an object, etc.

Part 2

Create a template function called sum that accepts three parameters:

  • result (by reference)

  • a

  • b

The function's return type is void. The function should sum the values of a and b, and set the reference variable result to the result of the summation.

#include <iostream> 

int main() {
    int result;
    int a = 20;
    int b = 30; 
    
    sum(result, a, b);
    std::cout << result << std::endl;  // This should print 50
}

Part 3

Given the following class definitions:

struct Car {
    public void describe() {
        std::cout << "I am a car" << std::endl;
    }
};

struct Fish {
    public void describe() {
        std::cout << "I am a fish" << std::endl;
    }
};

struct Box {
    public void describe() {
        std::cout << "I am a box" << std::endl;
    }
};

Create a template function called Describe that would invoke the .describe() method on the object passed into it:

int main() {
    Car car;
    Describe(car);  // Prints "I am a car"
    
    Fish fish;
    Describe(fish);  // Prints "I am a fish"
    
    Box box; 
    Describe(box);  // Prints "I am a box" 
}
  • Remove the Box::describe function, does the program now compile? Explain your answer.

  • Now remove lines 8-9 from your main function, does the program now compile? Explain your answer.

Exercise 2

Write a template function called absolute. The function should return the absolute number of a value given. This function should work for all number types: int, long, and float.

int main() {
    int abs_int = absolute(-5);  // Returns 5
    abs_int = absolute(10);  // Returns 10
    
    float abs_float = absolute(-3.2); // Returns 3.2
    abs_float = absolute(4.20); // Returns 4.20
}

You will find that the template function only applies for types for which:

  • the less-than operator (operator<) is defined

  • the unary negation operator (operator-) is defined.

Re-use the Rational class that you have created in Chapter 12 - Worked Exercises 1. Implement the operators above, and show that the template function absolute that you have just created works with instances of Rational.

Exercise 3

You are given the following 3 files:

  • Fruit.h

#include <string>

struct Fruit {
    std::string name;
};
  • Basket.h

#include <vector>
#include "Fruit.h"

struct Basket {
    int capacity;
    std::vector<Fruit> fruits;
};
  • main.cpp

#include <iostream>
#include "Basket.h"
#include "Fruit.h"

int main() {
    Basket basket; 
    for (int i = 0; i < 5; i++) {
        Fruit fruit;
        fruit.name = "Apple";
        basket.fruits.push_back(fruit);
    }
}

Based on your understanding of multiple inclusion, why does the above program not compile? Explain your answer.

Exercise 4

In the template prefix:

template <typename T>

What kind of variable is the parameter T?

  1. T must be a class

  2. T must not be a class

  3. T can only be types built into the C++ language (int, float, double, etc)

  4. T can be any type, whether built into C++, or ones defined by the programmer

Last updated

Was this helpful?