[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 422
  • Last Modified:


What is this mean???

That we are "blessed" with several absolute value functions is an accident of history. C libraries were already available when C++ arrived; they could be easil used, so they were not rewritten using function overloading. YOu are to find all the absolute value funtions u can, and rewrite all of them overloading the abs function name. At a minimum you should have the int, long, float, and double types represented.

I dont understand what the problem meaning??? Can someone explain it for me please.  Thanks
1 Solution
In the earlier days, the name of the functi could not be reused, so for int, they used the name abs(), for double and float, they used fabs() and for long they used labs(). For complex numbers, they used cabs().
I believe you job is to make several ab() overloads that take int, float, double, long, etc.

so you would have something like this:

long abs(long);
float abs(float);
int abs(int);
double abs(double);

Once you compile, due to magic of c++, namely function overloading (Having several functions with the same name, but different number and/ot types of parameters), the compiler will figure out which function to call depending on the type and/or number of parameters you give it.

That's all.
Hope this helps.
mactep13 explanation is good enough, but I believe it won't stop there, there are quite a lot of functions which can overloaded with specific type. Like for function which take value 'double' is used in prototype. Such functions can also be overloaded.

just my 2 cents.

Actually a template function would do best to meet the problem

template <typename T>
T abs(const T& t)
    return (t < 0)? -t : t;

It could be called using any type where the operator< (int i) -  to provide t < 0 - and operator-() -  to provide -t - is valid. That is for all C non-pointer types. For C++ class types these operators must be defined. E. g a class like the following

class Circle
    int x;
    int y;
    int r;

    bool operator< (int i) const
       return x < i;

    Circle operator-() const
       Circle c= *this;
       c.x = -c.x;
       return c;

would meet these criterias and the following compiles:

   long   l = abs(-123);
   double d = abs(-123.456);
   char c = abs(-20);
   Circle ci  = {  3, 4, 5 };
   Circle ci1 = abs(ci);

I assume you are not allowed to use template functions. So you have to implement overloaded functions for 'abs' function for int, long, float, and double types as Mactep has described. All functions get a similar implementation that you can deduce from my template function above.

Regards, Alex


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now