Write a function template that accepts an argument and returns its absolute value.
The absolute value of a number is its value with no sign. For example, the absolute
value of -5 is 5, and the absolute value of 2 is 2. Test the template in a simple driver
program.
#include<iostream>
usingnamespace std;
template < class AV >
AV aValue(AV a)
{
return (a > 0 ? a : a * -1);
}
int main()
{
int x,y;
cout << "this program returns the absolute value.\n"
<< "****************************************************\n\n";
cout << "enter a positive number : ";
cin >> x;
cout << "enter a negative number : ";
cin >> y;
cout << "absolute value of " << x << "is : ";
cout << aValue(x) << endl;
cout << "absolute value of " << y << "is : ";
cout << aValue(y) << endl;
cin.get();
cin.get();
return 0;
}
error:
1>------ Build started: Project: DataStructure, Configuration: Debug Win32 ------
1> Source1.cpp
1>Source1.obj : error LNK2005: _main already defined in Source.obj
1>C:\Users\Christian\Documents\DATA STRUCTURE\Excersices\Debug\DataStructure.exe : fatal error LNK1169: one or more multiply defined symbols found
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========