2 Argument Conversion Function
i have used single argument conversion function which gets called in below scenario.
Code:
#include<iostream>
using namespace std;
class Demo
{
public:
int a,b;
Demo(int i,int j=0):a(i),b(j)
{
}
void dis()
{
cout<<a <<"::"<<b<<endl;
}
};
int main()
{
Demo d=100;
d.dis();
return 0;
}
the output is 100::0
when i changed few lines in main so that the two argument constructor gets called
Code:
int main()
{
Demo d=100,200;
d.dis();
return 0;
}
It is giving error "test3.cpp: In function `int main()':test3.cpp:18: syntax error before numeric constant"
But it should work as Demo d=100; works
Re: 2 Argument Conversion Function
OK, I'll ask a simple question:
What book, tutorial, etc. shows you that this is the way to construct an object with two arguments?
What does the book you're using now say about constructing an object with more than 1 argument?
Regards,
Paul McKenzie
Re: 2 Argument Conversion Function
Quote:
What book, tutorial, etc. shows you that this is the way to construct an object with two arguments?
This i did not get from any book or tutorial;
I thought logically it should work.
In the first case when Demo d=100 ( constructor with one argument is called)
It is same as Demo d(100) ->the 1 argument constructor is called
likewise Demo d=100,200 should be like Demo d(100,200) and call the 2 argument constructor (it what i thought)
Re: 2 Argument Conversion Function
Quote:
Originally Posted by
Rajesh1978
This i did not get from any book or tutorial;
Sorry, but you cannot learn C++ this way. You are missing the basic fundamentals of C++ if you go on guessing what the language syntax should look like. You must use structured material, whether it is using good books, good tutorials, etc. Otherwise, you'll just throw stuff on the wall, hoping something sticks.
Quote:
I thought logically it should work.
No, it doesn't work this way. Any book will show you right away how to construct objects.
Quote:
In the first case when Demo d=100 ( constructor with one argument is called)
It is same as Demo d(100) ->the 1 argument constructor is called
likewise Demo d=100,200 should be like Demo d(100,200) and call the 2 argument constructor (it what i thought)
Throw all of this out the window, it doesn't work. There is something called the comma operator that makes your version uncompilable.
The way you construct objects is very basic and straightforward:
Regards,
Paul McKenzie