• Main Menu
  • Type Conversion – Basic to Class Type


    The conversion from basic to user defined data types can be done using constructors.

    Consider the following constructor:

    .cf { font-family: Lucida Console; font-size: 9pt; color: black; background: white; }
    .cl { margin: 0px; }
    .cb1 { color: green; }
    .cb2 { color: blue; }
    .cb3 { color: maroon; }

     

    String :: String(char *a)
    {
     length = strlen(a);
     s = new char[length+1];
     strcpy(s,a);
    }

     

    The above constructor definition is used to construct a String type object from a char * type variable. The String class has two data members namely length and s. This constructor first finds out the length of the string which is pointed to by a and stores it into length. In the second statement, memory for a new (character) string is dynamically allocated. The third statement copies the contents of the string pointed to by a to the newly created string. Once this constructor has been defined in the String class, it can be used for converting from char* to String.

    Now let us consider another example for converting an int to a class type.

     

    class distance
    {
    private:
     int kilometers;
     int meters;
    public:
     distance(int meters) // constructor
     {
     kilometers = meters / 1000;
     meters = meters % 1000;
     }
    }

     

    The following conversion statements can be used in a function such as main:

     

    distance D1; // object D1 created
    int distance_covered = 10050;
    D1 = distance_covered; // int to class type

     

    After the execution of the 3rd statement, kilometers will hold the value 10 and meters will have the value 50.

    Thus, we see that the constructors used for the type conversion take a single argument whose type is to be converted

    Sample Program

     

    /*
     * Program that converts from miles to kilometers.
     * Illustrates type conversion from built-in types
     * to user-defined types.
     * https://www.tech-faq.com
     */
    
    
    #include<iostream>
    using namespace std;
    
    
    class DistConv
    {
    private:
     int kilometers;
     double meters;
     const static double kilometersPerMile;
    public:
     // This function converts a built-in type (i.e. miles) to the user-defined type (i.e. Distconv)
     DistConv(double mile) // Constructor with one argument
     {
     double km = kilometersPerMile * mile ; // converts mile to Km
     kilometers = int(km); // converts float Km to int and assigns to kilometer
     meters = (km - kilometers) * 1000 ; // converts to meter
     }
     void display(void)
     {
     cout << kilometers << " kilometers and " << meters << " meters n" ;
     }
    }; // End of the Class Definition
    
    
    const double DistConv::kilometersPerMile = 1.609344;
    
    
    
    
    int main(void)
    {
     DistConv d1 = 5.0 ; // Uses the constructor with one argument
     cout << "5.0 miles = " ;
     d1.display( ) ;
    
    
     d1 = 2.0 ; // This form also uses the constructor with one argument
     cout << "2.0 miles = " ;
     d1.display( ) ;
    
    
    }

     

    Output

     

    5.0 miles = 8 Kilometers and 46.72 meters
    2.0 miles = 3 Kilometers and 218.688 meters

     

    In this Series

    Got Something To Say:

    Your email address will not be published. Required fields are marked *

    C
    172 queries in 0.482 seconds.