Type casting

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

Join Date: Aug 2005
Posts: 15,462
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: 1476
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Type casting

 
0
  #11
Oct 19th, 2005
If you had coded it like this, it would have worked. The constructor without parameters is never used in this example.
  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. b=0;
  17. cout<<"values are "<<a<<endl<<b<< endl;
  18. }
  19. };
  20. int main()
  21. {
  22. A a1(1);
  23. return 0;
  24. }
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
  #12
Oct 19th, 2005
Originally Posted by Ancient Dragon
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
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
  #13
Oct 20th, 2005
Originally Posted by aminura
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
  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. cout<<"Argument constructor getting called";
  16. a=b=m;
  17. cout<<"\n values are"<<a<<endl<<b;
  18. }
  19. void display()
  20. {
  21. cout<<"\nvalue of a:"<<a<<" "<<"value of b:"<<b;
  22. }
  23. };
  24. void main()
  25. {
  26. A a1;
  27. a1=1;
  28. a1.display();
  29.  
  30. }

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