How come foo(3,3.14); Doesn't work
but Goo(3,3.14); works?
Please reply.

#include <iostream>
using namespace std;
double foo(double a, double b) {
    cout<<"foo(double a, double b)"<<endl;
}
 int foo(int a, int b){
    cout<<"foo(int a, int b)"<<endl;
 }
double Goo(float a, float b){
    cout<<"Goo(float a, float b)"<<endl;
}
  int Goo(int a, int b){    
    cout<<"Goo(int a, int b)"<<endl; //this one
  }

int main()
{
    foo(3,3.14); // Doesn't work
    Goo(3,3.14); //Works
}

Recommended Answers

All 5 Replies

Your first example has no doubles, just an int and a float. If you were to cast the 3.14 value to a double it would probably work, or use 3.14D. It may also work if you use 3.0 for the first argument.

I didn't get you. the first example has the double 3.14. compiler interpreting the call as foo(int, double);
but why is this happening in the case of Goo call?

Hey,

What is the purpose of the example you are trying to give? Polymorphism is about change "poly" and thus meaning that the expected outputs for each can be changed depending on which object is called.

Look at another example (The one you gave does not really make sense to me).

Assume the follow:

If you have a class Animal:

class Animal {

    Animal(); // constructor 

    void speak();

};

We can assume that the every animal speaks, however, we know that a Dog and a Cat speak differently but we can use polymorphism to change the events or actions depending on the object:

class Animal {

    public:

        Animal(){};
        void speak()
        {

        };
};


class Dog : Animal {

    public:
        Dog() { };
        void speak()
        {
            cout << "Woof";
        }
};

class Cat : Animal {

    public:
        Cat() { };
        void speak()
        {
            cout << "Meow";
        }
};

int main()
{
    Dog d;
    Cat c;

    d.speak(); // "Woof"
    c.speak(); // "Meow"
}

Hope this makes a bit more sense. I just did not understand the example you gave.

P.S. In terms of your initial question regarding the code you submitted; it seems that you are not over-riding the functions correctly. I.e. you declare the first function foo of that as a double but then the second one as an int but you do not use classes. Polymorphism is a type of OO concept and the example you gave is not really OO even though it does have some re-use involved in terms of the functions. You should attempt to read up more on the subject.

Also, it's worth noting that: When you declare a function with type double the compiler expects a DOUBLE to be returned. Just like an int in the example you gave, you do not return anything at all. You just have a cout statement.

Hi Phorce... The polymorphism you said is different, it is in case when we have classes.
My example is having two global functions (not in any class), with same name and difference in type of parameter.
Please answer if the question is clear.

My example is having two global functions (not in any class), with same name and difference in type of parameter.

I kind of understand, check this out:

#include <iostream>
using namespace std;
double foo(double a, double b) {
    cout<<"foo(double a, double b)"<<endl;
    return a;
}
 int foo(int a, int b){
    cout<<"foo(int a, int b)"<<endl;
    return a;
 }
double Goo(float a, float b){
    cout<<"Goo(float a, float b)"<<endl;
    return a;
}
  int Goo(int a, int b){    
    cout<<"Goo(int a, int b)"<<endl; //this one
    return a;
  }

int main()
{
    foo(3.1,3.14); // Doesn't work
    Goo(3,3); //Works
}

Output:

foo(double a, double b)
Goo(int a, int b)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.