Posted on 2005-04-12
Last Modified: 2010-04-01
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
Question by:cuong5985
    LVL 3

    Expert Comment

    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.
    LVL 8

    Expert Comment

    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.

    LVL 39

    Accepted Solution

    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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
    Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now