#Previous question #Index of C++ Questions #Next question

Data conversion from standard type to user-defined type is possible through conversion operator and he classís constructor. But some conversion may take place which we donít want. To prevent conversion operator from performing unwanted conversion, we avoid declaring it. But we may need constructor for building the object. Through the explicit keyword we can prevent unwanted conversions that may be done using the constructor.

#include<iostream.h>
class complex
{
  private:
    float real,imag;
  public: complex()
  {
    real=imag=0.0;
  }
  explicit complex(float r, float i = 0.0)
  {
    real = r;
    imag = i;
  }
  complex operator+(complex c)
  {
    complex t;
    t.real=real+c.real;
    t.imag=imag+c.imag;
    return t;
  }
  void display()
  {
    cout << endl << "real " 
          << real << ", imag " << imag;
  }
};
int main (int argc, char *argv[])
{
  complex c1(1.5,3.5);
  c2 = c1 + 1.25;
  c2.display();
}

In the statement: c2=c1+1.25
The compiler would find that here the overloaded operator +() function should get called. When it finds that there is a wrong type on the right hand side of + it would look for a conversion function which can convert float to complex. The two argument constructor can meet this requirement. Hence the compiler would call it. This is an implicit conversion and can be prevented by declaring the constructor as explicit.

You have viewed 1 page out of 62. Your C++ learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

object-oriented, C vs C++, C++ vs Java, encapsulation, constructor, overloaded constructor, destructor, destructor overloaded, copy constructor, copy constructor, deep copy, deep copy vs shallow copy, singleton, default access modifier, default access modifier, default access modifier, abstraction, THIS pointer, static function, THIS pointer, a static function, scope resolution operator, inline function, private vs protected, const function, polymorphism, operator overloading, function overloading, overriding, prefix vs postfix, friend class, friend function, sizeof class with virtual, vfptr and vftable, vfptr and vftable using C, early binding and late binding, inheritance, virtual function call from a constructor/destructor, virtual destructor, virtual base class, virtual base class, exception, stack unwinding, exception handling, try-catch block, namespace, mutable variable and const function, mutable keyword, explicit, access a member function, object slicing, ctor sequence of constructor, dtor sequence of destructor, virtual destructor, print the type, dynamic casting, static casting, dynamic and static casting, const casting, reinterpret casting, static and reinterpret casting, template class, template vs macro,

#Object-Oriented Programming in C++ (4th Edition)
#Programming: Principles and Practice Using C++ (2nd Edition)
#The C++ Programming Language, 4th Edition
#Professional C++ Paperback by Nicholas A.Solter, Scott J.Kleper
#Effective Modern C++: 42 Specific Ways to Improve Your Use of C++11 and C++14