Home / Tips / C++ Tips / C++: What is Data Conversion?

“Chishiki” is Japanese for “knowledge.” e-chishiki.com aims to bring software developers, information security professionals, IT executives and other IT pros a rich body of knowledge in the form of articles, interviews, tutorials and technical discussions. Our contributors are among the biggest names in the Indian IT industry and include noted authors, educators and practitioners.

C++: What is Data Conversion?


What is Data Conversion?


Assignments between types whether they are basic or user-defined, are handled by the compiler. If the variables are of different basic types the compiler calls a special routine to convert the value. But if we want to convert between user-defined data type and basic types we have to write a conversion routine ourselves. A conversion routine to convert user-defined data type string to integer is shown below:

class string
{
   private :
      char str[20] ;
   public :
      string( )
      {
      }
      string ( char *s )
      {
         strcpy ( str, s ) ;
      }
      operator int( )
      {
         return 123 ; // Write logic to convert string to integer
      }
} ;
main( )
{
   string s2 = "123" ;
   int i1 = int ( s2 ) ;
   cout << endl << i1 ;
}

Comments

Log in or create a user account to comment.

On Sale From April 2008

Let Us C
8th Ed.
C programming classic & best seller. 1 million+ copies sold!

Y. Kanetkar

On Sale From April 2008

Introduction to Object Oriented Programming & C++

Y. Kanetkar

On Sale From Fall 2008

Microsoft .NET Framework: Web Application Security

Vijay Mukhi

On Sale From Nolvember 2008

Quest C++ Courseware
12+ hours of instructional audio and animated slides.

Y. Kanetkar Asang Dani

On Sale From November 2008

A Programmer's Guide to Web Application Security

Vijay Mukhi

Latest Forum Posts