We help IT Professionals succeed at work.

constructor overloading is not working

Naman Goel
Naman Goel asked
on
350 Views
Last Modified: 2010-04-02
I have created two constructors in class with different type of argument but when we create objects of this class in main and compile it gives some error.
ambiguity between arguments
class add
{
int x,y;
float a,b;
public:
add(int x1,int y1){x=x1;y=y1;cout<<x<<y;}
add(float x1,float y1){a=x1;b=y1;cout<<a<<b;}
};
void main()
{
/*here when we create two objects and call these two constructors an error occurs*/
add a1(10,20),a2(2.21,3.31);
}
Comment
Watch Question

Because by default decimals values are "double".
Therefore since the type is not exactly one of the 2 constructors, it could cast to Int or to Float => ambiguity.

You have to cast your 2nd call : a2((float)2.21,(float)3.31)
Naman GoelPrincipal Software engineer
CERTIFIED EXPERT

Author

Commented:
but why this perticular syntax is working in java.
Java is not C++...
It depends on the "precedence" of similarity between types, I guess for Java a double is nearer to a float, than to an int.
In C++ the casting is valid from double->int and double->float.
The compiler does not know which one to chose.

Instead of casting the constructor call, you may simply change the type of your constructorarguments to double :
add(double x1,double y1){a=x1;b=y1;cout<<a<<b;}
efn

Commented:
Or you could write float literals, like 2.21f or 2.21F.

--efn
Naman GoelPrincipal Software engineer
CERTIFIED EXPERT

Author

Commented:
why should we have to cast for a perticular data type when this is not necessary as recommanded in oops concept and it so it gives same error in all polymorphism
polymorphism doesn't mean that a compiler must always know what's best?

if you would define a constructor  add(double x1, double y1){a=(float)x1;b=(float)y1;cout<<a<<b;} it is polymorphism that would choose the right constructor. But if you don't use the exact type most C++ compilers would give an error because of ambiguity.

Regards, Alex




Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Commented:
DanRollins,

Why do you want the questioner to close the question so quickly!?
He may not have been convinced by the comments....!!
For a person who knows what is typecasting and type conversons, this may seem very simple......Otherwise, if the questioner is from a different background, it will be difficult to understand all this.
Do give him enough time to close it.
Also, it is premature (and wrong) to suggest whose answer he has to accept!
More than correctness of the answer, it is the convincingness of the answer, that is going to win!:-)

-ssnkumar
Naman GoelPrincipal Software engineer
CERTIFIED EXPERT

Author

Commented:
yes this is wrte we have a boundation to pass on the type with values in some cases.
but this not a very good idea.
this is not very fine idea to type cast every thing .

Commented:
naman_goel,

If you feel your question has been answered, then go ahead and close the question as DanRollins has suggested.
Otherwise give your feedback and we can continue the discussion.

-ssnkumar
Let's be pragmatic, either
- one of your constructor which will fit the parameters given with no ambiguity - it is not the case here
- or change the type of your constructor parameters (to double)
- or cast the parameters when calling the constructor

Agreed with you casting is the not best solution 1. not nice 2. you may miss the casting by mistake if long code ... or change after a long time

I prefer to change the type of the parameters in the constr to double since it is the default and it has more valid decimals (even if in the case of your example it does not mattermuch :)

So as in my answer above I would have

class add
{
  int x,y;
  float a,b; // could be changed to double as well
  public:
  add(int x1,int y1){x=x1;y=y1;cout<<x<<y;}
  add(double x1, double y1){a=x1;b=y1;cout<<a<<b;}  // here it is changed
};

Regards
CERTIFIED EXPERT
Author of the Year 2009

Commented:
ssnkumar,
I suggested closing the question because it had been answered -- quite completely. My goal was to elicit an acknowledgement from naman_goel, to see if he understood that the question had been thoroughly answered.  I was also hoping to prevent having a bunch of other experts expend time and effort of a question that was finished.

-- Dan

Commented:
Hi Dan,

   Asking the questioner is ok.
   But the way you did it is very distrubing!
   It was as if you are misusing the Administration rights!
   Influencing the questioners decision is very bad.....!
 
-ssnkumar
Personnaly, I'm new to EE (about 2 weeks) and posted quite a lot of stuff.

This is my point of vue:

I feel the administrator message relevant :
- the answer was given in at least 2 different ways
- since the answer was quite easy, if the author does not accept any of them, the thread keeps growing indefinitely.

The way Dan managed that was the right one, and I'd be happy if it would the same in other topics.

Commented:
Atleast one person is supporting Dan!:-)
But, I wonder what will the oldtimers in this site tell about this!?

-ssnkumar
ssnkumar,

i think you can be happy that the author is on your side. So you got the points, though Mercantilum sufficently has answered the initial question.

The author doesn't like the answer(s). However, that's his problem if he won't accept facts and no reason to let all others wait. So, Dan was right suggesting to close the thread. And the author made up his mind and accepted one of the answers. That's ok, also.

BTW, i am a C++ oldtimer but not an EE oldtimer ;-)

Regards, Alex
CERTIFIED EXPERT
Author of the Year 2009

Commented:
ssnkumar,
I invite you to start a thread (post a question) in the Lounge or the Expert Input topic area for this off-topic discussion.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.