you shouldn't call the constructor directly like in function show(). Do that in a set function. This works ok in VC++ 6.0 compiler. Nearly all c++ programs use set functions similar to below -- none use private constructors to do it.
#include<iostream>
using namespace std;
class A
{
private:
int a;
float b;
public:
A()
{
cout<<"\n Default constructor invoked\n";
a=0;
b=0;
}
A(int j,float i)
{
a=j;
b=i;
cout<<"\n value "<<a<<" and "<<b<<endl;
};
void SetA(int j)
{
a = j;
cout << "a = " << a << endl;
}
public:
void show()
{
int j;
cout<<"\n Enter value of j ";
cin>>j;
SetA(j);
cout<<"\n new values are "<<a<<" and "<<b << endl;
};
};
int main(int argc, char* argv[])
{
A a1;
A a3(8,4.01F);
a1.show();
a3.show();
return 0;
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
A(j);
How is that we are calling the constructor explicitly...i thought it was not possible
It isn't, and that's one of the reasons that VC++ 6.0 won't compile the program. Turbo C++ compiled it because it is just a plain crappy compiler that no self-respecting c++ programmer would admit to using :mrgreen:
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
may compile, but calling the constructor like that is unnecessary because it is called when the object is instantiated. So doing it like that is the same as calling a function twice. In some cases it might even crash the program, depending on what the constructor does.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
>This code compiles in VC++6.0
But it doesn't do what you seem to think it does. You're calling the constructor twice, thus creating two objects. Only one of those objects do you pair with a variable.
>Why does it do this way in visual c++?
Because Turbo C++ is wrong and Visual C++ is right. Visual C++ is correctly parsing A(j) as a declaration of a variable of type A with the identifier j. Since j was already declared, that's an illegal declaration. The problem is more clear when you look at it like this:
#include <iostream>
int main()
{
int(x);
x = 10;
std::cout<< x <<'\n';
}
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>are the same things
Yes, because the syntax inherited from C allows parens in a lot of places. Naturally adding features to the language will create ambiguities, as those of us who have been bitten by the "object declaration is parsed as a function declaration" bug have realized.
>if i am not pairing it with variable...then what is constructor
>doing.....who is it initialising....i am not creating any object
You are creating an object, then you're ignoring it. That doesn't mean that it's not created. What do you think happens when you call a constructor?
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
argc is the number of command-line arguments, argv is an array of strings. Each argument on the command line is separated by one or more spaces, so if you type this in a cmd-prompt
c:>myprog -a something -b something else
The above will cause myprog.exe to receive 6 arguments
argv[0] == always the name of the program, such as myprog.exe
argv[1] == "-a"
argv[2] == "something"
argv[3] == "-b"
argv[4] == "something"
argv[5] == "else"
The arguments can also be passed when one program executes (spawns) another program using functions such as the spawn family of functions (man for them). or in MS-Windows CreateProcess() win32 api function.
Those arguments can get somewhat complicated. *nix compiler often have functions to help -- such as getopt(). I know of no equlivant function in M$ compilers :(
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
>Then why does it compile when I call this constructor
The problem only arises with a single argument constructor. A default constructor or multiple argument constructor is clearly a function call, so the parser can figure out what you're trying to do.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401