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!

Recommended Answers

All 13 Replies

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;
}
A(j);

How is that we are calling the constructor explicitly...i thought it was not possible

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:

#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

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.

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

So basically

int (x);
and int x;

are the same things

>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

>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?

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

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

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.

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 :(

>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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.