In the following code I am giving an int value to class using type casting--

#include<iostream>
using namespace std;
class A
{
	int a;
	float b;
public:
	A()
	{
		a=0;
		b=0;
	}
	A(int m)
	{
		a=m;
		cout<<"\n values are"<<a<<endl<<b;
	}
};
void main()
{
	A a1;
	a1=1;
	
}

The members of this class are being initialised with zero but after assigning 1 to the object a1.. the value of b changes to a garbage value. Why does it do this way?

Recommended Answers

All 12 Replies

Just do this

A(int m)
	{
		a=m;
		b=m;
		cout<<"\n values are"<<a<<endl<<b;
	}

I don't want to assign this value to b. My question is that because of default constructor b is initialized to 0 but after typecasting why doesn't it retain it's value zero..it changes to garbage value.

becoz the initial value given by default constructor(i.e 0,0) is given to object a1....but when u write a statement

a1=1//implicit type conversion from int to user-defined type

a temporary(unnamed) object is formed which calls ur parameterized contructor which initilizes only a and does not initilizes b...that is why b is having garbage value....

Note: Read about explicit keyword

the code you posted should not even compile because class A is missing the overloaded = operator. in main(), the line "a = 1" does NOT call the default constructor as you might think, but will call the = operator. And since you did not define one the compiler should have complained (screamed loudly) to you.

the code you posted should not even compile because class A is missing the overloaded = operator. in main(), the line "a = 1" does NOT call the default constructor as you might think, but will call the = operator. And since you did not define one the compiler should have complained (screamed loudly) to you.

It will compile perfectly fine...but won't give the output...which was expected...

When compiler finds that there is wrong type on the right hand side of = it would look for a conversion function which can convert an int to A type. The argument constructor can meet this requirement. hence the compiler would decide to call it. This is an implicit conversion, one that you may not have intended to make possible......as far as = operator is concerned...compiler will supply that for you...don't worry about that

becoz the initial value given by default constructor(i.e 0,0) is given to object a1....but when u write a statement

a1=1//implicit type conversion from int to user-defined type

a temporary(unnamed) object is formed which calls ur parameterized contructor which initilizes only a and does not initilizes b...that is why b is having garbage value....

Note: Read about explicit keyword

So is it like that the temporary object formed during the calling of parameterised constructor returns the value to the actual object?

#include<iostream>
using namespace std;
class A
{
public:
	int a;
	float b;
public:
	A()
	{
		a=0;
		b=0;
	}
	A(int m)
	{
		a=m;
		cout<<"\n values are"<<a<<endl<<b;
	}
};
void main()
{
	A a1;
	
	a1=1;
	cout<<a1.b;
}

When I make the members public, the output of a1.b is also a garbage value. And if the constructor is returning the value to the object then isn't it contradicting the concept I asked about in one of my previous threads..

See this thread
http://daniweb.com/techtalkforums/thread32689.html

So is it like that the temporary object formed during the calling of parameterised constructor returns the value to the actual object?

Constructors don't return any value(not allowed in C++)....i am saying when u write

a1=1

when compiler finds 1(int type which is not same as user defined type)...it will perform implicit type casting...and by doing that it calls parameterized constructor....which initilizes ur nonamed object....and since u haven't supplied ur own overloaded =operator...compiler will supply it for you

Note:I didn't see the link of the thread u provided

what was the doubt(contradiction) you had in the thread you referred in the last post of yours

you answered that ...regarding returning value.

Now I am little confused...since constructors don't return any value how does type casting using parameterised constructors work. Like in the program a1=1; calls the constructor and value of a and b changes accordingly in the actual object..how does this happen?

If you had coded it like this, it would have worked. The constructor without parameters is never used in this example.

#include<iostream>
using namespace std;
class A
{
	int a;
	float b;
public:
	A()
	{
		a=0;
		b=0;
	}
	A(int m)
	{
		a=m;
		b=0;
		cout<<"values are "<<a<<endl<<b<< endl;
	}
};
int main()
{
	A a1(1);
	return 0;	
}

If you had coded it like this, it would have worked. The constructor without parameters is never used in this example.

As i said earlier it would work earlier also...just try to run that code...but b will have garbage value since you are not assigning any value to it...just read my above replies

Now I am little confused...since constructors don't return any value how does type casting using parameterised constructors work. Like in the program a1=1; calls the constructor and value of a and b changes accordingly in the actual object..how does this happen?

I already answered that in my second reply....

a1=1.....automatically calls argument constructor and assign values to a and b....and then default assignment operator(=) supplied by compiler assigns these value to object a1.....according to your code a get value 1 and b contains garbage value...and the same is assigned to object a1....see this modified code

#include<iostream>
using namespace std;
class A
{
	int a;
	float b;
public:
	A()
	{
		a=0;
		b=0;
	}
	A(int m)
	{
		cout<<"Argument constructor getting called";
		a=b=m;
		cout<<"\n values are"<<a<<endl<<b;
	}
	void display()
	{
		cout<<"\nvalue of a:"<<a<<"  "<<"value of b:"<<b;
	}
};
void main()
{
	A a1;
	a1=1;
	a1.display();
	
}

I suggest you to read a bit more about operator overloading and explicit keyword......and i think you could not understood(fully) the previous thread you started......

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.