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

What is c++ redistributable?

625


Can we get the value of ios format flags?

666


What is a syntax in c++?

609


Is c++ a float?

606


What do you mean by persistent and non persistent objects?

742






What does flush do?

563


What is else if syntax?

681


How much is c++ certification?

589


What are references in c++? What is a local reference?

576


What is abstraction in c++ with example?

564


What problems might the following macro bring to the application?

626


what is upcasting in C++?

722


What are destructors?

577


What is the best free c++ compiler for windows?

592


How would you obtain segment and offset addresses from a far address of a memory location?

622