There are three absolute value functions defined in various header files. These are abs, fabs, and labs. Write a template function that subsumes all three of these functions into one template function.
What will be an ideal response?
This is a solution complete with a test framework and a work-around for
Microsoft VC++ 6.0 before patch level 5.
```
#include
using namespace std;
//requires operator > and unary operator- be defined and
//comparison to the value of the default c-tor be
//meaningful.
//The call to T() is a call to the default constructor
//for the type T. For primitive types such as int or
//double, this is 0 of the type. For other types, it is
//what the default constructor generates.
template
T absolute( T src)
{
if( src > T())//
return src;
return -src;
}
class A
{
public:
A():i(0){}
A(int new_i):i(new_i) {}
A operator=(const A&x);
A operator-() {return A(-i);}
friend bool operator<(const A& lhs, const A& rhs);
friend
ostream& operator<<(ostream& out,const A& rhs);
private:
int i;
};
bool operator>(const A& lhs, const A& rhs)
{
return lhs.i > rhs.i;
}
ostream& operator<<(ostream& out,const A& rhs)
{
out << rhs.i;
return out;
}
A A::operator=(const A& rhs)
{
i = rhs.i;
return A(i);
}
int main()
{
int a = -1;
cout << absolute(a) << endl;
A one(1);
A minusOne(-1);
cout << one << endl;
cout << absolute(one) << endl;
cout << absolute(minusOne) << endl;
```
You might also like to view...
Accessor that returns value of a matrix entry mat[r][c]
What will be an ideal response?
If the list in the accompanying figure was to be searched using a sequential search on an unordered list, how many key comparisons would be made to find the number 44?
A. 1 B. 3 C. 5 D. 6
You install a second video adapter and monitor on your Windows Vista PC, but when you start the system, the second monitor doesn't display anything. You've confirmed that the video driver is correct, and the video card is active in Device Manager. What else do you need to do?
a. Open Control Panel and run the Multiple Monitor configuration applet. b. Open Display properties and check the Extend my Windows desktop onto this monitor checkbox c. Upgrade the PC to Windows Vista Ultimate d. Reinstall Winodws with the /MMSUPPORT parameter switch.
An array is a collection of elements of the same data type.
Answer the following statement true (T) or false (F)