| | |
Type casting
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
If you had coded it like this, it would have worked. The constructor without parameters is never used in this example.
C++ Syntax (Toggle Plain Text)
#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; }
•
•
•
•
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.
•
•
•
•
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?
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
C++ Syntax (Toggle Plain Text)
#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......
![]() |
Similar Threads
- Problem while Type Casting (C++)
- Type casting and type conversions (C)
- type casting (C++)
- conversion type (C++)
Other Threads in the C++ Forum
- Previous Thread: Coin flipping program, so close to finishing
- Next Thread: What is wrong with my code?
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game generator givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






