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;
```

Computer Science & Information Technology

You might also like to view...

The messages we can send an object are listed under the Properties tab of the functions area.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology

The addition of a Web camera (also called a ____) enables users to participate in a video chat, in which participants can see and speak to each other.

A. ?webpic B. webcam? C. ?webvid D. ?webeye

Computer Science & Information Technology