What is conversion constructor?

Answers were Sorted based on User's Feedback



What is conversion constructor?..

Answer / swarna sekhar dhar

A constructor that can be called with a single argument is
used for conversions from the type of the argument to the
class type. Such a constructor is called a conversion
constructor. Consider the following example:
/ spec1_conversion_constructors.cpp
class Point
{
public:
Point();
Point( int );
//...
};

int main()
{
}
Sometimes a conversion is required but no conversion
constructor exists in the class. These conversions cannot be
performed by constructors. The compiler does not look for
intermediate types through which to perform the conversion.
For example, suppose a conversion exists from type Point to
type Rect and a conversion exists from type int to type
Point. The compiler does not supply a conversion from type
int to type Rect by constructing an intermediate object of
type Point.

Is This Answer Correct ?    16 Yes 1 No

What is conversion constructor?..

Answer / 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

More C++ General Interview Questions

When should we use container classes instead of arrays?

0 Answers  


Describe the process of creation and destruction of a derived class object?

0 Answers  


What does #define mean in c++?

0 Answers  


What is Namespace?

5 Answers   HCL, Samsung,


How are the features of c++ different from c?

0 Answers  






What is c++ and its features?

0 Answers  


How would you represent an error detected during constructor of an object?

1 Answers  


What is the difference between the compiler and the preprocessor?

0 Answers  


Do we have private destructors?

12 Answers   Symphony, TCS,


What does it mean to declare a member function as static?

0 Answers  


Which of the Standard C++ casts can be used to perform a ?safe? downcast: a) reinterpret_cast b) dynamic_cast c) static_cast d) const_cast

2 Answers   Quark,


What is Object Oriented programming.what is the difference between C++ and C?

8 Answers   Infosys,


Categories