Question
During our conversation about Templates, we talked about how template parameters have constraints associated with them, which is to say that we're not always allowed
During our conversation about Templates, we talked about how template parameters have constraints associated with them, which is to say that we're not always allowed to instantiate them with any template parameters we want; the parameters have to be reasonable, relative to what the template requires.
Now consider the Point class template from the Templates notes. Let's explore what constraints are associated with each of its member functions. For each of the member functions listed below, what are the constraints on any template parameters associated with it? List the constraints separately (e.g., with a bulleted list); be as specific as possible about which constraints to include, but no single constraint requires you to write more than a few words.
- The constructor that takes three CoordinateType arguments.
- The copy constructor that initializes a Point of one type from a Point of a potentially different type.
- The assignment operator that assigns a Point of one type from a Point of a potentially different type.
- The x, y, and z member functions. (Note: Both overloads of all three of these will have the same constraints. Do you see why?)
- The distanceFrom member function.
#ifndef POINT_HPP #define POINT_HPP
#include
template class Point { public: Point( const CoordinateType& x, const CoordinateType& y, const CoordinateType& z);
template Point(const Point& other);
template Point& operator=(const Point& other);
CoordinateType& x() noexcept; const CoordinateType& x() const noexcept;
CoordinateType& y() noexcept; const CoordinateType& y() const noexcept;
CoordinateType& z() noexcept; const CoordinateType& z() const noexcept;
double distanceFrom(const Point& p) const;
private: CoordinateType x_; CoordinateType y_; CoordinateType z_; };
template Point::Point( const CoordinateType& x, const CoordinateType& y, const CoordinateType& z) : x_{x}, y_{y}, z_{z} { }
template template Point::Point(const Point& other) : x_{other.x()}, y_{other.y()}, z_{other.z()} { }
template template Point& Point::operator=(const Point& other) { x_ = other.x(); y_ = other.y(); z_ = other.z(); return *this; }
template CoordinateType& Point::x() noexcept { return x_; }
template const CoordinateType& Point::x() const noexcept { return x_; }
template CoordinateType& Point::y() noexcept { return y_; }
template const CoordinateType& Point::y() const noexcept { return y_; }
template CoordinateType& Point::z() noexcept { return z_; }
template const CoordinateType& Point::z() const noexcept { return z_; }
template double Point::distanceFrom(const Point& other) const { return std::sqrt( (x_ - other.x_) * (x_ - other.x_) + (y_ - other.y_) * (y_ - other.y_) + (z_ - other.z_) * (z_ - other.z_)); }
#endif // POINT_HPP
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started