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

Why iomanip is used in c++?

632


When to use “const” reference arguments in a function?

575


What is the difference between #import and #include in c++?

592


What do you mean by const correctness?

625


What is the main purpose of overloading operators?

584






Do you know what are the new features that iso/ansi c++ has added to original c++ specifications?

556


Define Virtual function in C++.

619


What is the first name of c++?

563


What are literals in C++?

590


What does the ios::ate argument do?

659


What happens when the extern "c" char func (char*,waste) executes?

632


What is auto used for in c++?

563


What are the advantages of using pointers in a program?

677


What is the use of typedef?

633


Distinguish between new and malloc and delete and free().

571