C++ 32: Type Casiting

Typecasting / Type Conversion with Objects

The C++ compiler knows hoe to convert  a built in value into
another built in data type. But C++ compilers don't know about 
following conversations.

How to convert a built in Data value into another object.
How to convert an Object(i.e user defined data type) into built-in data type.
How to convert an object of one class into object of another class

There are three possible conversions  can be done with object as follows:

Type casting from user defined to built in 
Type casting from built in to user defined data type.
Type casting from, user defined to user defined.



#include <iostream>
#include <string.h>
using namespace std;

class MyString
{
private:
    char N[25];

public:
    MyString(char n[])
    {
        strcpy(N, n);
    }
    void showData()
    {
        cout << "N = " << N << endl;
    }
    operator int()
    {
        int len = strlen(N);
        return len;
    }
};

int main()
{

    MyString a("AYUSH");
    a.showData();

    int x = a;
    cout << "Length of My String is : " << a << endl;
    // E:
    //     \CPP Programming\44Typecastingv.mp4
    return 0;
}

Output:

N = AYUSH
Length of My String is : 5


Design a class Circle with data member  R and objects of the class can be created 
by passing the data. Define converter function to convert a circle object into int 
and float.
#include <iostream>
using namespace std;

class Circle
{
private:
    int R;

public:
    Circle(int r)
    {
        R = r;
    }
    operator int()
    {
        return (R);
    }
    operator float()
    {
        return (3.14 * R * R);
    }
};

int main()
{
    Circle a(30);
    int x = a;
    float p = a;
    cout << "Radious of circle is: " << x << endl;
    cout << "The area is: " << p << endl;

    return 0;
}

Output:

Radious of circle is: 30
The area is: 2826


Typecasting from built in datatype to user defined data type:

To convert built in value to an object the class must contain a constructor with single argument
When you assign a built in value to an object then the constructor will atomically invoke the object and the value of built in data type will be passed as argument to constructor.


#include <iostream>
using namespace std;

class Currency
{
private:
    int rp;

public:
    Currency(int R, int P)
    {
        r = R;
        p = P;
    }
    Currency()
    {
        r = p = 0;
    }
    Currency(int P)
    {
        r = P / 100;
        p = P % 100;
    }
    void showData()
    {
        cout << r << " rupees and " << p << " paise." << endl;
    }
};

int main()
{
    Currency a(510), b;
    a.showData();

    int x = 450;
    b = x;
    b.showData();

    return 0;
}

Output:

5 rupees and 10 paise.
4 rupees and 50 paise.


WAP to convert built in datatype int to user defined datatype circle.
#include <iostream>
using namespace std;

class Circle
{
private:
    int R;

public:
    Circle()
    {
        R = 0;
    }
    Circle(int r)
    {
        R = r;
    }

    void area()
    {
        float A = 3.14 * R * R;
        cout << "Area is: " << A << endl;
    }
};

int main()
{
    Circle a;
    a.area();

    a = 5;
    a.area();

    return 0;
}

Output:

Area is: 0
Area is: 78.5

Type casting from user defined to user defined
To convert an object of different class type, the object to which
another object is assigned must have constructor with single argument.
And the class whoose object assigned must have convertion function.
NOTE : new compiler like Dev C++ does not support this conversion. it is supported in the Turbo C++ compiler.
 
 class1 a = class2 b

class1 must have constructor with single argument
class2 must have the conversion function
Example:

#include <iostream>
using namespace std;

class MyInteger
{
private:
    int N;

public:
    MyInteger(int n)
    {
        N = n;
    }
    operator int()
    {
        return (N);
    }
};
class Currency
{
private:
    int rp;

public:
    Currency()
    {
        r = p = 0;
    }
    Currency(int P)
    {
        r = P / 100;
        p = P % 100;
    }
    void showdata()
    {
        cout << r << " rupees " << p << " paise." << endl;
    }
};
int main()
{
    MyInteger a(510);
    Currency b;

    b.showdata();

    int a = a;
    b = a;
    b.showdata();
    return 0;
}

Output:

Enter your age: 56
You can vote





Comments

Popular posts from this blog

C++ 38: Visibility Modes Public, Private and Protected