What is conversion constructor?

Answer Posted / sachin mahajan

This is related to typecasting of user defined datatypes ie
Convertion of one class object to other class object.
Ex
I want to type cast REAL class object to COMPLEX Class object
Both REAL class and COMPLEX Class are user defined.

COMPLEX objComplex(6,3); //6 is real and 3 is imagnary
REAL objReal(5);
objComplex=objReal;
//end result of the above statement should be that
objComplex //should have 5 as real part and 0 as imaginary
//There are two solutions to it
//a)write conversion constuctor
//b)Overload assignment operator
// (a) for this add this in the COMPLEX Class
COMPLEX :: COMPLEX(REAL r)
{
real=r.value; // value is the only data member of REAL class
imag=0;
}





Is This Answer Correct ?    14 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Evaluate as true or false: !(1 &&0 || !1) a) True b) False c) Invalid statement

855


What is the use of :: operator in c++?

832


Can we use this pointer in a class specific, operator-overloading function for new operator?

849


What is a multimap c++?

885


What is a dynamic binding in c++?

727


How to declare an array of pointers to integer?

820


What will happen if a pointer is deleted twice?

949


What is the header file for setw?

826


What are arrays c++?

836


Name the operators that cannot be overloaded in C++?

811


If there are two catch statements, one for base and one for derived, which should come first?

781


What is a tuple c++?

761


What is struct c++?

769


What is the type of 'this' pointer? When does it get created?

734


How do I start a c++ project?

802