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 the main function c++?

869


What is a dll entry point?

761


What is DlgProc?

810


What is a wchar_t in c++?

843


What is abstract keyword in c++?

863


What do you mean by enumerated data type?

833


How c functions prevents rework and therefore saves the programers time as wel as length of the code ?

887


What are pointer-to-members in C++? Give their syntax.

894


What are the four partitions in which c++ compiler divides the ram?

1015


What is the difference between public and private data members?

930


What is the standard template library (stl)?

1121


What are friend classes? What are advantages of using friend classes?

879


What is private inheritance?

877


Do class declarations end with a semicolon? Do class method definitions?

900


What is the fastest c++ compiler?

813