-4

I wrote and executed the following code in c++:

    int calc(int x=10,int y=100);int a,b,s,p,m;void main()
{clrscr();cout<<"enter the two numbers:";cin>>a>>b;s=calc(a,b);p=calc(a);`m=calc();cout<<"\n sum:"<<s;cout<<"\n product:"<<p;cout<<"\n subtraction:"<<m;getch();}
int calc(int a)
{int t;b=100;t=a*b;return t;}
int calc(int a,int b)
{int t;t=a+b;return t;}
int calc()
{int t;t=b-a;return t;}

I See that only one function is being called and is giving the correct output. For example: 3 and 4 will give 7, but from multiply it will be 101. I am not very good with c++ concepts. An explanation would be useful. Regards.

3 Answers3

1

Point is

s=calc(a,b); p=calc(a); m=calc(); All match function

int calc(int a, int b)
  {
  int t;
  t=a+b;
 return t;
  }

Because it has defined default values, in case you don's specify input:

int calc(int x=10, int y=100);

It means that if you use

calc(1);

It will use

calc(1, 100);

BTW, this does not even compile on VisualStudio 2015, because with error:

error C2668: 'calc': ambiguous call to overloaded function

MaciekGrynda
  • 583
  • 2
  • 13
  • Thanks a lot. Actually this was asked by one of my students. Thank you for clearing the concept – Sky's On Ur Head Jun 24 '16 at 10:11
  • But from MSDN : _If two candidates are judged to be equally good, preference goes to a candidate that does not have optional parameters for which arguments were omitted in the call. This is a consequence of a general preference in overload resolution for candidates that have fewer parameters_ Which is different than the behaviour described in the question. Do you have any idea why? REference [http://stackoverflow.com/questions/2674417/conflicting-overloaded-methods-with-optional-parameters] – Avantika Saini Jun 24 '16 at 10:16
  • This was not compiled with VS so VS features don't matter – Irminsul Jun 24 '16 at 10:21
  • @Benoit - it was example Edit. I'm not smarter than people designing VS and it's compiler, so If they say it's wrong it's most likely wrong. And they give explanation. – MaciekGrynda Jun 24 '16 at 10:26
  • @AvantikaSaini Link you provided is for C#. C++ has different rules http://stackoverflow.com/questions/9943596/function-overloading-two-functions-only-differ-by-a-default-parameter – MaciekGrynda Jun 24 '16 at 10:26
  • @MaciekGrynda oops! sorry my bad – Avantika Saini Jun 24 '16 at 10:29
  • @MaciekGrynda : my comment was adressed to AvantikaSaini because his link came from msdn. I didn't mean to be agressive. An didnt notice your last remark, neither the fact the link was about c#. My bad. – Irminsul Jun 24 '16 at 10:34
  • I dont see why people have downvoted my question.. :( .. I see so many good answers and explainations.. – Sky's On Ur Head Jun 24 '16 at 10:44
  • I don't know but you should generally at least indent properly your code, and leave one blanck line bewteen your different function/classes if there are some. – Irminsul Jun 24 '16 at 11:03
0

because you gave default values. When you call calc(a) with a=3, your prgramm actually runs calc(3,100)

Irminsul
  • 171
  • 9
0

Code is so strange at my first sight.

First of all, you should know global a = 3, b = 4 all the time. both parameters are provided the first time call calc so the result is 7. the second time, only one paramter provided, so a = 3, b = 100 and the last time no paramater, a = 10, b = 100.

int calc(int x = 10, int y = 100)
{
    int t;
    t = x + y;
    return t;
}

this may be easier to understand?

Z.Ding
  • 7
  • 2
  • Thanks. It was actually written by a guy who actually thought he was quite expert at messing with logic. Before giving any answers to him I just wanted to be sure of the answer myself . That's why i put the question on my behalf and not his.The end result turned out to be pretty good! – Sky's On Ur Head Jun 24 '16 at 12:13