Type casting

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2004
Posts: 47
Reputation: aminura is an unknown quantity at this point 
Solved Threads: 0
aminura aminura is offline Offline
Light Poster

Type casting

 
0
  #1
Oct 19th, 2005
In the following code I am giving an int value to class using type casting--

  1. #include<iostream>
  2. using namespace std;
  3. class A
  4. {
  5. int a;
  6. float b;
  7. public:
  8. A()
  9. {
  10. a=0;
  11. b=0;
  12. }
  13. A(int m)
  14. {
  15. a=m;
  16. cout<<"\n values are"<<a<<endl<<b;
  17. }
  18. };
  19. void main()
  20. {
  21. A a1;
  22. a1=1;
  23.  
  24. }

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?
Life is not a journey to the grave with the intention of arriving safely in a pretty and well-preserved body, but rather to skid in broadside, thoroughly used up, totally worn out, and loudly proclaiming --WOW -- " What a ride!!! " -James Fineous McBride
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: Type casting

 
0
  #2
Oct 19th, 2005
Just do this
  1. A(int m)
  2. {
  3. a=m;
  4. b=m;
  5. cout<<"\n values are"<<a<<endl<<b;
  6. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 47
Reputation: aminura is an unknown quantity at this point 
Solved Threads: 0
aminura aminura is offline Offline
Light Poster

Re: Type casting

 
0
  #3
Oct 19th, 2005
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.
Life is not a journey to the grave with the intention of arriving safely in a pretty and well-preserved body, but rather to skid in broadside, thoroughly used up, totally worn out, and loudly proclaiming --WOW -- " What a ride!!! " -James Fineous McBride
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: Type casting

 
0
  #4
Oct 19th, 2005
becoz the initial value given by default constructor(i.e 0,0) is given to object a1....but when u write a statement
  1. 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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,340
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Type casting

 
0
  #5
Oct 19th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: Type casting

 
0
  #6
Oct 19th, 2005
Originally Posted by Ancient Dragon
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 47
Reputation: aminura is an unknown quantity at this point 
Solved Threads: 0
aminura aminura is offline Offline
Light Poster

Re: Type casting

 
0
  #7
Oct 19th, 2005
Originally Posted by sunnypalsingh
becoz the initial value given by default constructor(i.e 0,0) is given to object a1....but when u write a statement
  1. 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?

  1. #include<iostream>
  2. using namespace std;
  3. class A
  4. {
  5. public:
  6. int a;
  7. float b;
  8. public:
  9. A()
  10. {
  11. a=0;
  12. b=0;
  13. }
  14. A(int m)
  15. {
  16. a=m;
  17. cout<<"\n values are"<<a<<endl<<b;
  18. }
  19. };
  20. void main()
  21. {
  22. A a1;
  23.  
  24. a1=1;
  25. cout<<a1.b;
  26. }

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
Life is not a journey to the grave with the intention of arriving safely in a pretty and well-preserved body, but rather to skid in broadside, thoroughly used up, totally worn out, and loudly proclaiming --WOW -- " What a ride!!! " -James Fineous McBride
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: Type casting

 
0
  #8
Oct 19th, 2005
Originally Posted by aminura
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
  1. 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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: Type casting

 
0
  #9
Oct 19th, 2005
what was the doubt(contradiction) you had in the thread you referred in the last post of yours
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 47
Reputation: aminura is an unknown quantity at this point 
Solved Threads: 0
aminura aminura is offline Offline
Light Poster

Re: Type casting

 
0
  #10
Oct 19th, 2005
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?
Last edited by aminura; Oct 19th, 2005 at 3:02 pm. Reason: one more question
Life is not a journey to the grave with the intention of arriving safely in a pretty and well-preserved body, but rather to skid in broadside, thoroughly used up, totally worn out, and loudly proclaiming --WOW -- " What a ride!!! " -James Fineous McBride
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC