954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Doubts about constructor

I have a doubt regarding constructor in this program--

include<iostream.h>
class A
{
private:
	int a;
	float b;
	A(int j)
	{	
		a=j;
		cout<<"\n a is"<<a;
	}
public:
	A()
	{
		cout<<"\n Default constructor invoked\n";
		a++;
		b=0;
	}

	A(int j,float i)
	{
		a=j;
		b=i;
		cout<<"\n value"<<a<<"and"<<b<<endl;
	}
public:
	void show()
	{
		int j;
		cout<<"\n Enter value of j";
		cin>>j;
		A(j);
		cout<<"\n new values are"<<a<<"and"<<b;
	}
};
void main()
{
	A a1;
	A a3(8,4.01);
	a1.show();
	a3.show();
}


I tried this program in Visual c++ and turbo c++.This program compiled well in the turbo c++ while in visual it's giving an error " 'j' : redefinition; different basic types" for this --

void show()
	{
		int j;
		cout<<"\n Enter value of j";
		cin>>j;
		A(j);
		cout<<"\n values are"<<a<<"and"<<b;
	}

but if instead of calling this A(j) constructor..I do this

void show()
	{
		int j;
		float i;
		cout<<"\n Enter value of j";
		cout<<"\n Enter value of i";
		cin>>i;
		cin>>j;
		A(j,i);
		cout<<"\n values are"<<a<<"and"<<b;
	}

the program compiles well..
Why does it do this way in visual c++? :?:

My second doubt is that when this program is executed , the values after calling the constructor A(j) do not change.. :confused: Is it because that the constructors are just used to initialise the objects and their second call is not taken into consideration or there is some other reason for it?? :?:

Thanks!

aminura
Light Poster
47 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

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
Team Colleague
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

SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 
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
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
#include<iostream.h>
class A
{
private:
	int a;
	float b;
	A(int j)
	{	
		a=j;
		cout<<"\n a is"<<a;
	}
public:
	A()
	{
		cout<<"\n Default constructor invoked\n";
		a=0;
		b=0;
	}

void show()
	{
		cout<<a;
	}
};
void main()
{
	A a1;
	A();//explicitly calling constructor
	a1.show();
	
}

This code compiles in VC++6.0

SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 

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
Team Colleague
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
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

So basically

int (x);
and int x;



are the same things

SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 
>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.


if i am not pairing it with variable...then what is constructor doing.....who is it initialising....i am not creating any object

SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 

>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
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Thanks for the replies!

Narue>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';
}


[/QUOTE]
Then why does it compile when I call this constructor--

void show()
	{
		int j;
		float i;
		cout<<"\n Enter value of j";
		cin>>j>>i;
		A(j,i);
		cout<<"\n values are"<<a<<"and"<<b;
	}
aminura
Light Poster
47 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

One more thing, I have never ever read about passing the arguments to the main()..

" int main(int argc, char* argv[]) "

I couldn't even find any relevant text regarding this..Can you please tell me what exactly it is used for or refer to me some good site regarding it.

aminura
Light Poster
47 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 

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
Team Colleague
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
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You